diff --git a/README.md b/README.md index e220b91..465d66f 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ In order to edit rich text we need to know how to map from the [rich text schema Example setup ```javascript -import {basicSchemaAdapter, syncPlugin, docFromSpans} from "@automerge/prosemirror" +import {basicSchemaAdapter, syncPlugin, pmDocFromSpans} from "@automerge/prosemirror" import { next as am } from "@automerge/automerge" const handle = repo.find("some-doc-url") @@ -46,7 +46,7 @@ let editorConfig = { path: ["text"] }) ], - doc: docFromSpans(adapter, am.spans(handle.docSync()!, ["text"])) + doc: pmDocFromSpans(adapter, am.spans(handle.docSync()!, ["text"])) } let state = EditorState.create(editorConfig) diff --git a/examples/react/src/App.tsx b/examples/react/src/App.tsx index 0de591c..8fe0d13 100644 --- a/examples/react/src/App.tsx +++ b/examples/react/src/App.tsx @@ -6,7 +6,7 @@ import { EditorView } from "prosemirror-view" import { exampleSetup } from "prosemirror-example-setup" import { syncPlugin, - docFromSpans, + pmDocFromSpans, basicSchemaAdapter, } from "@automerge/prosemirror" import { next as am } from "@automerge/automerge" @@ -41,7 +41,7 @@ function App({ docUrl }: { docUrl: AutomergeUrl }) { syncPlugin({ adapter, handle, path: ["text"] }), ], // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - doc: docFromSpans(adapter, am.spans(handle.docSync()!, ["text"])), + doc: pmDocFromSpans(adapter, am.spans(handle.docSync()!, ["text"])), }), dispatchTransaction: (tx: Transaction) => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/examples/vanilla/src/main.ts b/examples/vanilla/src/main.ts index 2c1b3ce..9608146 100644 --- a/examples/vanilla/src/main.ts +++ b/examples/vanilla/src/main.ts @@ -4,7 +4,7 @@ import { exampleSetup } from "prosemirror-example-setup" import { syncPlugin, basicSchemaAdapter, - docFromSpans, + pmDocFromSpans, } from "@automerge/prosemirror" import { DocHandle, Repo, isValidAutomergeUrl } from "@automerge/automerge-repo" import { IndexedDBStorageAdapter } from "@automerge/automerge-repo-storage-indexeddb" @@ -38,7 +38,7 @@ const adapter = basicSchemaAdapter const view = new EditorView(document.querySelector("#editor"), { state: EditorState.create({ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - doc: docFromSpans(adapter, am.spans(handle.docSync()!, ["text"])), + doc: pmDocFromSpans(adapter, am.spans(handle.docSync()!, ["text"])), plugins: [ ...exampleSetup({ schema: adapter.schema }), syncPlugin({ adapter, handle, path: ["text"] }), diff --git a/playground/src/Editor.tsx b/playground/src/Editor.tsx index 6b4d94a..b9af970 100644 --- a/playground/src/Editor.tsx +++ b/playground/src/Editor.tsx @@ -23,7 +23,7 @@ import { } from "prosemirror-inputrules" import "prosemirror-view/style/prosemirror.css" import { Prop, next as am } from "@automerge/automerge" -import { docFromSpans, SchemaAdapter } from "../../src" +import { pmDocFromSpans, SchemaAdapter } from "../../src" import { DocHandle } from "@automerge/automerge-repo" import { wrapInList, @@ -116,7 +116,7 @@ export function Editor({ handle, path, schemaAdapter }: EditorProps) { } const adapter = schemaAdapter - const doc = docFromSpans(adapter, am.spans(handle.docSync(), path)) + const doc = pmDocFromSpans(adapter, am.spans(handle.docSync(), path)) const state = EditorState.create({ schema: adapter.schema, plugins: [ diff --git a/src/amToPm.ts b/src/amToPm.ts index 1bca1f1..ba8880d 100644 --- a/src/amToPm.ts +++ b/src/amToPm.ts @@ -1,7 +1,7 @@ import { next as am, DelPatch, Patch, type Prop } from "@automerge/automerge" import { Fragment, Slice, Mark } from "prosemirror-model" import { Transaction } from "prosemirror-state" -import { amSpliceIdxToPmIdx, docFromSpans } from "./traversal" +import { amSpliceIdxToPmIdx, pmDocFromSpans } from "./traversal" import { findBlockAtCharIdx, patchSpans } from "./maintainSpans" import { isPrefixOfArray, isArrayEqual } from "./utils" import { ReplaceStep } from "prosemirror-transform" @@ -144,7 +144,7 @@ export function handleBlockChange( patchSpans(atPath, spans, patch) } //console.log("spans after block change", spans) - const docAfter = docFromSpans(adapter, spans) + const docAfter = pmDocFromSpans(adapter, spans) //console.log("doc after block change", docAfter) const change = findDiff(tx.doc.content, docAfter.content) if (change == null) return tx diff --git a/src/index.ts b/src/index.ts index b1caa68..98a26cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,5 +7,5 @@ export { type BlockMappingSpec, } from "./schema" export { basicSchemaAdapter } from "./basicSchema" -export { docFromSpans, blocksFromNode } from "./traversal" +export { pmDocFromSpans, pmNodeToSpans } from "./traversal" export { syncPlugin, syncPluginKey } from "./syncPlugin" diff --git a/src/pmToAm.ts b/src/pmToAm.ts index 98fed04..fccf495 100644 --- a/src/pmToAm.ts +++ b/src/pmToAm.ts @@ -7,7 +7,7 @@ import { } from "prosemirror-transform" import { Mark, MarkType, Node } from "prosemirror-model" import { Prop, next as automerge } from "@automerge/automerge" -import { blocksFromNode, pmRangeToAmRange } from "./traversal" +import { pmNodeToSpans, pmRangeToAmRange } from "./traversal" import { next as am } from "@automerge/automerge" import { SchemaAdapter, amMarksFromPmMarks } from "./schema" @@ -118,10 +118,8 @@ function replaceStep( if (applied == null) { throw new Error("Could not apply step to document") } - //console.log(JSON.stringify(applied, null, 2)) - const newBlocks = blocksFromNode(adapter, applied) - //console.log(JSON.stringify(newBlocks, null, 2)) - automerge.updateSpans(doc, field, newBlocks) + const newSpans = pmNodeToSpans(adapter, applied) + automerge.updateSpans(doc, field, newSpans) } function replaceAroundStep( @@ -134,13 +132,10 @@ function replaceAroundStep( ) { const applied = step.apply(pmDoc).doc if (applied == null) { - //console.log(step) throw new Error("Could not apply step to document") } - //console.log(applied) - const newBlocks = blocksFromNode(adapter, applied) - //console.log(newBlocks) - automerge.updateSpans(doc, field, newBlocks) + const newSpans = pmNodeToSpans(adapter, applied) + automerge.updateSpans(doc, field, newSpans) } function applyAddMarkSteps( diff --git a/src/syncPlugin.ts b/src/syncPlugin.ts index 35f6602..81893c6 100644 --- a/src/syncPlugin.ts +++ b/src/syncPlugin.ts @@ -3,7 +3,7 @@ import { next as am } from "@automerge/automerge" import { DocHandle, DocHandleChangePayload } from "@automerge/automerge-repo" import pmToAm from "./pmToAm" import amToPm from "./amToPm" -import { docFromSpans } from "./traversal" +import { pmDocFromSpans } from "./traversal" import { patchesToTr } from "./patchesToTr" import { ChangeSet } from "prosemirror-changeset" import { SchemaAdapter } from "./schema" @@ -112,7 +112,7 @@ export const syncPlugin = ({ ) // Replace the diff range in ProseMirror doc from the AutoMerge doc. - const doc = docFromSpans(adapter, spansAfter) + const doc = pmDocFromSpans(adapter, spansAfter) const slice = doc.slice(diff.from, diff.to) const tr = state.tr tr.replace(diff.from, diff.to, slice) diff --git a/src/traversal.ts b/src/traversal.ts index df87843..eaa66e2 100644 --- a/src/traversal.ts +++ b/src/traversal.ts @@ -44,7 +44,7 @@ export type TraversalEvent = * @param spans * @returns */ -export function docFromSpans(adapter: SchemaAdapter, spans: am.Span[]): Node { +export function pmDocFromSpans(adapter: SchemaAdapter, spans: am.Span[]): Node { const events = traverseSpans(adapter, spans) type StackItem = { tag: string @@ -934,7 +934,7 @@ export function blockAtIdx( return block } -export function blocksFromNode( +export function pmNodeToSpans( adapter: SchemaAdapter, node: Node, ): ( diff --git a/test/pmToAm.spec.ts b/test/pmToAm.spec.ts index 868dd6a..23e9ae1 100644 --- a/test/pmToAm.spec.ts +++ b/test/pmToAm.spec.ts @@ -4,7 +4,7 @@ import { AddMarkStep, ReplaceStep, Step } from "prosemirror-transform" import { default as pmToAm } from "../src/pmToAm" import { next as am } from "@automerge/automerge" import { assert } from "chai" -import { docFromSpans } from "../src/traversal" +import { pmDocFromSpans } from "../src/traversal" import { EditorState } from "prosemirror-state" import { basicSchemaAdapter } from "../src/basicSchema" @@ -203,7 +203,7 @@ describe("when converting a ReplaceStep to a change", () => { ) }) const spans = am.spans(doc, ["text"]) - const pmDoc = docFromSpans(basicSchemaAdapter, spans) + const pmDoc = pmDocFromSpans(basicSchemaAdapter, spans) const editor = EditorState.create({ schema, doc: pmDoc }) updateDoc(doc, editor.doc, [ new ReplaceStep( diff --git a/test/traversal.spec.ts b/test/traversal.spec.ts index ecfb307..6856980 100644 --- a/test/traversal.spec.ts +++ b/test/traversal.spec.ts @@ -6,8 +6,8 @@ import { pmRangeToAmRange, traverseSpans, amIdxToPmBlockIdx, - docFromSpans, - blocksFromNode, + pmDocFromSpans, + pmNodeToSpans, traverseNode, eventsWithIndexChanges, } from "../src/traversal" @@ -2699,7 +2699,7 @@ describe("the traversal API", () => { assert.equal(amIdxToPmBlockIdx(basicSchemaAdapter, spans, 6), 2) }) }) - describe("the docFromSpans function", () => { + describe("the pmDocFromSpans function", () => { const schema = basicSchemaAdapter.schema it("should construct a documnt with extra render-only paragraphs for nested list items", () => { const spans: am.Span[] = [ @@ -2713,7 +2713,7 @@ describe("the traversal API", () => { }, { type: "text", value: "item 1" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -2753,7 +2753,7 @@ describe("the traversal API", () => { }, { type: "text", value: "item 2" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -2800,7 +2800,7 @@ describe("the traversal API", () => { }, { type: "text", value: "item 3" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( @@ -2854,7 +2854,7 @@ describe("the traversal API", () => { }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( @@ -2918,7 +2918,7 @@ describe("the traversal API", () => { }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -2961,7 +2961,7 @@ describe("the traversal API", () => { }, { type: "text", value: "item 2" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -2999,7 +2999,7 @@ describe("the traversal API", () => { }, { type: "text", value: "item 2" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( @@ -3026,7 +3026,7 @@ describe("the traversal API", () => { value: { type: new am.RawString("aside"), parents: [], attrs: {} }, }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -3064,7 +3064,7 @@ describe("the traversal API", () => { { type: "text", value: "next line" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -3102,7 +3102,7 @@ describe("the traversal API", () => { { type: "text", value: "world" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -3141,7 +3141,7 @@ describe("the traversal API", () => { }, }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -3184,7 +3184,7 @@ describe("the traversal API", () => { }, { type: "text", value: "world" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -3225,7 +3225,7 @@ describe("the traversal API", () => { }, { type: "text", value: "more quote" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -3279,7 +3279,7 @@ describe("the traversal API", () => { }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -3316,7 +3316,7 @@ describe("the traversal API", () => { }, { type: "text", value: "hello" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) assert.isTrue( doc.eq( schema.node("doc", null, [ @@ -3350,7 +3350,7 @@ describe("the traversal API", () => { }, { type: "text", value: "hello" }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) const expected = schema.node("doc", null, [ schema.node("unknownBlock", { unknownParentBlock: "unknown" }, [ schema.node( @@ -3373,7 +3373,7 @@ describe("the traversal API", () => { }) }) - describe("the blocksFromNode function", () => { + describe("the pmNodeToSpans function", () => { const schema = basicSchemaAdapter.schema it("should return the correct blocks for a document with a list containing a paragraph", () => { const doc = schema.node("doc", null, [ @@ -3386,7 +3386,7 @@ describe("the traversal API", () => { schema.node("list_item", null, [schema.node("paragraph", null, [])]), ]), ]) - const blocks = Array.from(blocksFromNode(basicSchemaAdapter, doc)) + const blocks = Array.from(pmNodeToSpans(basicSchemaAdapter, doc)) assert.deepStrictEqual(blocks, [ { type: "block", @@ -3428,7 +3428,7 @@ describe("the traversal API", () => { ]), ]), ]) - const blocks = Array.from(blocksFromNode(basicSchemaAdapter, doc)) + const blocks = Array.from(pmNodeToSpans(basicSchemaAdapter, doc)) assert.deepStrictEqual(blocks, [ { type: "block", @@ -3475,7 +3475,7 @@ describe("the traversal API", () => { ]), ]), ]) - const blocks = Array.from(blocksFromNode(basicSchemaAdapter, doc)) + const blocks = Array.from(pmNodeToSpans(basicSchemaAdapter, doc)) assert.deepStrictEqual(blocks, [ { type: "block", @@ -3513,9 +3513,9 @@ describe("the traversal API", () => { }, { type: "text", value: "hello", marks: {} }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) const blocks: am.Span[] = Array.from( - blocksFromNode(basicSchemaAdapter, doc), + pmNodeToSpans(basicSchemaAdapter, doc), ) assert.deepStrictEqual(blocks, spans) }) @@ -3533,9 +3533,9 @@ describe("the traversal API", () => { }, { type: "text", value: "hello", marks: {} }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) const blocks: am.Span[] = Array.from( - blocksFromNode(basicSchemaAdapter, doc), + pmNodeToSpans(basicSchemaAdapter, doc), ) assert.deepStrictEqual(blocks, spans) }) @@ -3553,9 +3553,9 @@ describe("the traversal API", () => { }, { type: "text", value: "hello", marks: {} }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) const blocks: am.Span[] = Array.from( - blocksFromNode(basicSchemaAdapter, doc), + pmNodeToSpans(basicSchemaAdapter, doc), ) assert.deepStrictEqual(blocks, spans) }) @@ -3577,9 +3577,9 @@ describe("the traversal API", () => { }, { type: "text", value: "hello", marks: {} }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) const blocks: am.Span[] = Array.from( - blocksFromNode(basicSchemaAdapter, doc), + pmNodeToSpans(basicSchemaAdapter, doc), ) assert.deepStrictEqual(blocks, spans) }) @@ -3601,9 +3601,9 @@ describe("the traversal API", () => { }, { type: "text", value: "hello", marks: {} }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) const blocks: am.Span[] = Array.from( - blocksFromNode(basicSchemaAdapter, doc), + pmNodeToSpans(basicSchemaAdapter, doc), ) assert.deepStrictEqual(blocks, spans) }) @@ -3624,9 +3624,9 @@ describe("the traversal API", () => { }, }, ] - const doc = docFromSpans(basicSchemaAdapter, spans) + const doc = pmDocFromSpans(basicSchemaAdapter, spans) const blocks: am.Span[] = Array.from( - blocksFromNode(basicSchemaAdapter, doc), + pmNodeToSpans(basicSchemaAdapter, doc), ) assert.deepStrictEqual(blocks, spans) }) diff --git a/test/utils.ts b/test/utils.ts index f928c88..46292ac 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,7 +1,7 @@ import { assert } from "chai" import { next as automerge } from "@automerge/automerge" import { EditorState } from "prosemirror-state" -import { docFromSpans } from "../src/traversal" +import { pmDocFromSpans } from "../src/traversal" import { Node } from "prosemirror-model" import { AssertionError } from "assert" import { applyBlockPatch } from "../src/maintainSpans" @@ -50,7 +50,7 @@ export function makeDoc( editor: EditorState } { const { spans, doc } = docFromBlocksNotation(defs) - const pmDoc = docFromSpans(adapter, spans) + const pmDoc = pmDocFromSpans(adapter, spans) const editor = EditorState.create({ schema: adapter.schema, doc: pmDoc }) return { spans, doc, editor } }