Skip to content

Commit

Permalink
rename docFromSpans to pmDocToSpans, blocksFromNode to pmNodeToSpans
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHung committed Oct 1, 2024
1 parent 36d08cc commit 97bdc61
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 64 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/vanilla/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"] }),
Expand Down
4 changes: 2 additions & 2 deletions playground/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: [
Expand Down
4 changes: 2 additions & 2 deletions src/amToPm.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 5 additions & 10 deletions src/pmToAm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/syncPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -112,7 +112,7 @@ export const syncPlugin = <T>({
)

// 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)
Expand Down
4 changes: 2 additions & 2 deletions src/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -934,7 +934,7 @@ export function blockAtIdx(
return block
}

export function blocksFromNode(
export function pmNodeToSpans(
adapter: SchemaAdapter,
node: Node,
): (
Expand Down
4 changes: 2 additions & 2 deletions test/pmToAm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit 97bdc61

Please sign in to comment.