Skip to content

npm: expose FindByText / FindAllByText / FindByRegex / FindByKind / Exists / ReplaceMatch in the TypeScript wrapper #171

@JSv4

Description

@JSv4

Context

PR #168 landed JSExport bridge methods for six previously-unreachable DocxSession methods, restoring parity at the bridge layer. The TypeScript wrapper at npm/src/session.ts does not yet expose them — so they're callable from the raw WASM exports but not from the typed DocxSession TS API consumers actually use.

Scope

Add typed TS wrappers + types for the methods now exposed on DocxSessionBridge:

  • session.exists(anchorId: string): boolean
  • session.findByText(needle: string, options?: FindOptions): AnchorTarget | null
  • session.findAllByText(needle: string, options?: FindOptions): AnchorTarget[]
  • session.findByRegex(pattern: string, regexOptions?: number, options?: FindOptions): AnchorTarget[]
  • session.findByKind(kind: string, scope?: string): AnchorTarget[]
  • session.replaceMatch(match: TextMatch, replace: string): EditResult — client-side helper that calls replaceTextAtSpan(match.enclosingAnchor.id, match.span.start, match.span.length, replace) (no new wire op needed — see PR feat(pyhost): shared DocxSession bridge core + stdio NDJSON host + parity ops for python-docxodus #168 rationale)

Note: getAnchorInfo and getAnchorInfos already landed via #162 / #169 — no action needed on those.

What to mirror

Use the existing wrapper methods that match this shape as templates:

  • session.findByAnnotation / findByLabel / findByBookmark / listAnnotations in npm/src/session.ts — same passthrough-with-JSON-parse pattern.
  • The new FindByText etc. take an optionsJson string arg on the WASM side (same shape as Grep's options): {ignoreCase?, ignoreWhitespace?, kindFilter?, scopeFilter?}. Serialize an empty string when no options.
  • findByText returns nullable single → handle null in the TS layer.

Type additions to npm/src/types.ts

  • FindOptions { ignoreCase?: boolean; ignoreWhitespace?: boolean; kindFilter?: string; scopeFilter?: string } — matches the C# FindOptions record. Wire keys already camelCase.
  • Extend the DocxSessionBridge exports interface (around lines 700-970) with the new method signatures.

Tests

Playwright spec at npm/tests/find-by.spec.ts (or extend an existing session spec):

  • findByText / findAllByText with and without ignoreCase
  • findByRegex with a pattern that matches multiple anchors
  • findByKind('p') returns body paragraphs
  • findByKind('h', 'body') filter by scope
  • exists true/false for known + bogus anchor ids
  • replaceMatch workflow: grep('foo')replaceMatch(match, 'bar') → verify text updated

Use existing test fixtures (HC001-5DayTourPlanTemplate.docx is a good starting point — has both paragraphs and headings).

Acceptance

  • All new methods callable from the typed TS API
  • npm run build clean
  • npm test green (new specs + no regressions)
  • Wire shapes byte-identical to what tools/python-host would consume — preserves the cross-transport parity invariant

Out of scope

  • Reworking the React hook layer (npm/src/react.ts) — separate concern.
  • Worker proxy plumbing — these are session methods, not stateless ones; the worker doesn't currently expose DocxSession (per the comment in npm/src/docxodus.worker.ts).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions