@hwp-editor/core
v1.2.0
Published
Framework-free core for hwp-editor: engine interface, typed hwp edit ops, segments, spec types, editor state.
Readme
@hwp-editor/core
The framework-free core of hwp-editor: the HwpEngine contract every document
operation flows through, the typed hwp edit op grammar, segment coordinate
math, generated spec types, and the editor state store. It has zero runtime
dependencies and deliberately contains no React, no Node-only APIs, and no HWP
parsing of its own: platform functions (fetch, invoke) are injected rather
than imported, so the same build runs in a browser, in Node, and in a Tauri
webview. The UI lives in @hwp-editor/react and the binary-spawning
adapter in @hwp-editor/server.
Install
pnpm add @hwp-editor/coreMost hosts do not install this directly: it arrives as a peer of
@hwp-editor/react and @hwp-editor/server, deduped to a single copy. Add it
explicitly only when your own source imports from it — createHttpEngine and
the DocumentHandle type being the usual reasons — because a strict
node_modules layout does not expose an automatically installed peer to the
application.
Usage
import { createHttpEngine, opsToArgv, createStore } from "@hwp-editor/core";
const engine = createHttpEngine("/api/hwp-editor");
const { data } = await engine.read(document);Pick a transport (createHttpEngine or createTauriEngine), or implement
HwpEngine yourself. Host code changes only the factory call; nothing else in
this package or in @hwp-editor/react knows which transport is in use.
Reference
| Export | Type | Notes |
| ------ | ---- | ----- |
| HwpEngine | interface | The six-method document contract: read, render, edit, compose, validate, capabilities. Everything else here exists to build arguments for it or to interpret its results. |
| HwpEngineError, HwpErrorCode | class + union | The error every transport throws, carrying a stable code a host can branch on. isHwpEngineError narrows an unknown catch value; toHwpErrorCode maps a wire code. |
| EditOp, opsToJson, opsToArgv, argvToOps, OP_FLAGS | union + functions | Type-safe mirror of every hwp edit op, pinned to the hwp-cli contract. opsToJson serializes an op list to the published edit-ops-v1 JSON the CLI engine passes to hwp edit --ops; opsToArgv/argvToOps are the separator-delimited argv form the tauri host still crosses with, so a queue survives a round trip. |
| parseCatEnvelope, offsetToRef, segmentAtRef, segmentText | functions | Pure helpers over the hwp cat --with-segments --segments v2 envelope. They bridge rendered markdown offsets and the stable {section, para} source coordinates EditOp targets. |
| DocumentSpecV2, TemplateSpecV1, TemplateDataV1 | types | Generated from the frozen JSON schemas in schemas/. These are the compose inputs; regenerate with pnpm gen:types after a schema change. |
| createStore, reducer, initialState | functions + value | Framework-free reducer store holding the document, pending ops, undo snapshots and status. React hosts bind it with useSyncExternalStore; others wrap it in their own reactivity. |
| createHttpEngine, createTauriEngine | factories | The two shipped transports. Both return an HwpEngine and both send the same JSON, so they are interchangeable. |
Develop
pnpm install
pnpm -r build
pnpm -r test
pnpm -r typecheck