@dxos/react-ui-editor 0.3.10-main.8fd45da → 0.3.10-main.90b298e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/lib/browser/index.mjs +117 -31
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
  5. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  6. package/dist/types/src/components/TextEditor/extensions/comments.d.ts +10 -6
  7. package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +1 -1
  8. package/dist/types/src/components/TextEditor/extensions/demo.d.ts +10 -0
  9. package/dist/types/src/components/TextEditor/extensions/demo.d.ts.map +1 -0
  10. package/dist/types/src/components/TextEditor/extensions/index.d.ts +1 -0
  11. package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +1 -1
  12. package/dist/types/src/components/TextEditor/extensions/listener.d.ts +4 -2
  13. package/dist/types/src/components/TextEditor/extensions/listener.d.ts.map +1 -1
  14. package/dist/types/src/hooks/automerge/PatchSemaphore.d.ts +3 -0
  15. package/dist/types/src/hooks/automerge/PatchSemaphore.d.ts.map +1 -1
  16. package/dist/types/src/hooks/automerge/index.d.ts +1 -1
  17. package/dist/types/src/hooks/automerge/index.d.ts.map +1 -1
  18. package/dist/types/src/hooks/automerge/plugin.d.ts.map +1 -1
  19. package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts +5 -0
  20. package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts.map +1 -0
  21. package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts +6 -0
  22. package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts.map +1 -0
  23. package/dist/types/src/hooks/automerge/translation/index.d.ts +3 -0
  24. package/dist/types/src/hooks/automerge/translation/index.d.ts.map +1 -0
  25. package/package.json +20 -19
  26. package/src/components/TextEditor/MarkdownEditor.stories.tsx +4 -2
  27. package/src/components/TextEditor/TextEditor.tsx +18 -4
  28. package/src/components/TextEditor/extensions/comments.ts +48 -31
  29. package/src/components/TextEditor/extensions/demo.ts +66 -0
  30. package/src/components/TextEditor/extensions/index.ts +1 -0
  31. package/src/components/TextEditor/extensions/listener.ts +2 -2
  32. package/src/hooks/automerge/PatchSemaphore.ts +12 -9
  33. package/src/hooks/automerge/index.ts +1 -1
  34. package/src/hooks/automerge/plugin.ts +1 -0
  35. package/src/hooks/automerge/{amToCodemirror.ts → translation/automerge-to-codemirror.ts} +10 -2
  36. package/src/hooks/automerge/{codeMirrorToAm.ts → translation/codemirror-to-automerge.ts} +7 -4
  37. package/src/hooks/automerge/translation/index.ts +6 -0
  38. package/dist/types/src/hooks/automerge/amToCodemirror.d.ts +0 -6
  39. package/dist/types/src/hooks/automerge/amToCodemirror.d.ts.map +0 -1
  40. package/dist/types/src/hooks/automerge/codeMirrorToAm.d.ts +0 -7
  41. package/dist/types/src/hooks/automerge/codeMirrorToAm.d.ts.map +0 -1
@@ -0,0 +1,66 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { keymap } from '@codemirror/view';
6
+
7
+ export type DemoOptions = {
8
+ delay?: number;
9
+ items?: string[];
10
+ };
11
+
12
+ const defaultItems = ['hello world!', 'this is a test.', 'this is [DXOS](https://dxos.org)'];
13
+
14
+ /**
15
+ * Demo script.
16
+ * Configurable plugin that let's user cycle through pre-configured input script.
17
+ */
18
+ export const demo = ({ delay = 75, items = defaultItems }: DemoOptions = {}) => {
19
+ let t: any;
20
+ let idx = 0;
21
+
22
+ return [
23
+ keymap.of([
24
+ {
25
+ // Reset.
26
+ key: "alt-meta-'",
27
+ run: (view) => {
28
+ clearTimeout(t);
29
+ idx = 0;
30
+ return true;
31
+ },
32
+ },
33
+ {
34
+ // Next prompt.
35
+ // TODO(burdon): Press 1-9 to select prompt?
36
+ key: "shift-meta-'",
37
+ run: (view) => {
38
+ clearTimeout(t);
39
+ // TODO(burdon): Add space if needed.
40
+ const text = items[idx++];
41
+ if (idx === items?.length) {
42
+ idx = 0;
43
+ }
44
+
45
+ let i = 0;
46
+ const insert = (d = 0) => {
47
+ t = setTimeout(() => {
48
+ const pos = view.state.selection.main.head;
49
+ view.dispatch({
50
+ changes: { from: pos, insert: text[i++] },
51
+ selection: { anchor: pos + 1 },
52
+ });
53
+
54
+ if (i < text.length) {
55
+ insert(Math.random() * delay * (text[i] === ' ' ? 2 : 1));
56
+ }
57
+ }, d);
58
+ };
59
+
60
+ insert();
61
+ return true;
62
+ },
63
+ },
64
+ ]),
65
+ ];
66
+ };
@@ -4,6 +4,7 @@
4
4
 
5
5
  export * from './autocomplete';
6
6
  export * from './basic';
7
+ export * from './demo';
7
8
  export * from './comments';
8
9
  export * from './link';
9
10
  export * from './listener';
@@ -4,13 +4,13 @@
4
4
 
5
5
  import { StateField } from '@codemirror/state';
6
6
 
7
- export type TextListener = (text: string) => void;
7
+ export type ListenerOptions = { onChange: (text: string) => void };
8
8
 
9
9
  /**
10
10
  * Based on https://github.com/codemirror/dev/issues/44#issuecomment-789093799
11
11
  */
12
12
  // TODO(burdon): Expose options.
13
- export const listener = (onChange: TextListener) =>
13
+ export const listener = ({ onChange }: ListenerOptions) =>
14
14
  StateField.define({
15
15
  create: () => null,
16
16
  update: (_value, transaction) => {
@@ -2,20 +2,24 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ // TODO(burdon): Additional copyright?
6
+
5
7
  import { type EditorView } from '@codemirror/view';
6
8
 
7
9
  import { next as automerge } from '@dxos/automerge/automerge';
8
10
 
9
- import amToCodemirror from './amToCodemirror';
10
- import codeMirrorToAm from './codeMirrorToAm';
11
11
  import { type IDocHandle } from './handle';
12
12
  import { type Field, isReconcileTx, getPath, reconcileAnnotationType, updateHeads, getLastHeads } from './plugin';
13
+ import { automergeToCodemirror, codemirrorToAutomerge } from './translation';
13
14
 
14
15
  type Doc<T> = automerge.Doc<T>;
15
16
  type Heads = automerge.Heads;
16
17
 
17
18
  type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads | undefined;
18
19
 
20
+ /**
21
+ * TODO(burdon): Comment.
22
+ */
19
23
  export class PatchSemaphore {
20
24
  _field!: Field;
21
25
  _inReconcile = false;
@@ -39,7 +43,7 @@ export class PatchSemaphore {
39
43
 
40
44
  const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
41
45
 
42
- // First undo all the unreconciled transactions
46
+ // First undo all the unreconciled transactions.
43
47
  const toInvert = transactions.slice().reverse();
44
48
  for (const tx of toInvert) {
45
49
  const inverted = tx.changes.invert(tx.startState.doc);
@@ -50,19 +54,18 @@ export class PatchSemaphore {
50
54
  });
51
55
  }
52
56
 
53
- // now apply the unreconciled transactions to the document
54
- let newHeads = codeMirrorToAm(this._field, handle, transactions, view.state);
57
+ // Apply the unreconciled transactions to the document.
58
+ let newHeads = codemirrorToAutomerge(this._field, handle, transactions, view.state);
55
59
 
56
- // NOTE: null and undefined each come from automerge and repo respectively
60
+ // NOTE: null and undefined each come from automerge and repo respectively.
57
61
  if (newHeads === null || newHeads === undefined) {
58
62
  // TODO: @alexjg this is the call that's resetting the editor state on click
59
63
  newHeads = automerge.getHeads(handle.docSync()!);
60
64
  }
61
65
 
62
- // now get the diff between the updated state of the document and the heads
63
- // and apply that to the codemirror doc
66
+ // Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
64
67
  const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.docSync()!, oldHeads, newHeads);
65
- amToCodemirror(view, selection, path, diff);
68
+ automergeToCodemirror(view, selection, path, diff);
66
69
 
67
70
  view.dispatch({
68
71
  effects: updateHeads(newHeads),
@@ -2,5 +2,5 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export { automergePlugin, type AutomergePlugin } from './plugin';
6
5
  export { type IDocHandle } from './handle';
6
+ export { automergePlugin, type AutomergePlugin } from './plugin';
@@ -78,6 +78,7 @@ export const automergePlugin = (handle: IDocHandle, path: Prop[]): AutomergePlug
78
78
  return result;
79
79
  },
80
80
  });
81
+
81
82
  const semaphore = new PatchSemaphore(stateField);
82
83
 
83
84
  const viewPlugin = ViewPlugin.fromClass(
@@ -2,6 +2,8 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ // TODO(burdon): Additional copyright?
6
+
5
7
  import { ChangeSet, type ChangeSpec, type EditorSelection, type EditorState } from '@codemirror/state';
6
8
  import { type EditorView } from '@codemirror/view';
7
9
 
@@ -14,9 +16,14 @@ import {
14
16
  type SpliceTextPatch,
15
17
  } from '@dxos/automerge/automerge';
16
18
 
17
- import { reconcileAnnotationType } from './plugin';
19
+ import { reconcileAnnotationType } from '../plugin';
18
20
 
19
- export default (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
21
+ export const automergeToCodemirror = (
22
+ view: EditorView,
23
+ selection: EditorSelection,
24
+ target: Prop[],
25
+ patches: Patch[],
26
+ ) => {
20
27
  for (const patch of patches) {
21
28
  const changeSpec = handlePatch(patch, target, view.state);
22
29
  if (changeSpec != null) {
@@ -28,6 +35,7 @@ export default (view: EditorView, selection: EditorSelection, target: Prop[], pa
28
35
  });
29
36
  }
30
37
  }
38
+
31
39
  view.dispatch({
32
40
  selection,
33
41
  annotations: reconcileAnnotationType.of({}),
@@ -2,14 +2,16 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ // TODO(burdon): Additional copyright?
6
+
5
7
  import { type EditorState, type Text, type Transaction } from '@codemirror/state';
6
8
 
7
9
  import { next as am, type Heads } from '@dxos/automerge/automerge';
8
10
 
9
- import { type IDocHandle } from './handle';
10
- import { type Field } from './plugin';
11
+ import { type IDocHandle } from '../handle';
12
+ import { type Field } from '../plugin';
11
13
 
12
- export default (
14
+ export const codemirrorToAutomerge = (
13
15
  field: Field,
14
16
  handle: IDocHandle,
15
17
  transactions: Transaction[],
@@ -18,7 +20,7 @@ export default (
18
20
  const { lastHeads, path } = state.field(field);
19
21
 
20
22
  // We don't want to call `automerge.updateAt` if there are no changes.
21
- // Otherwise later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
23
+ // Otherwise, later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
22
24
  let hasChanges = false;
23
25
  for (const tr of transactions) {
24
26
  tr.changes.iterChanges(() => {
@@ -37,5 +39,6 @@ export default (
37
39
  });
38
40
  }
39
41
  });
42
+
40
43
  return newHeads ?? undefined;
41
44
  };
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './automerge-to-codemirror';
6
+ export * from './codemirror-to-automerge';
@@ -1,6 +0,0 @@
1
- import { type EditorSelection } from '@codemirror/state';
2
- import { type EditorView } from '@codemirror/view';
3
- import { type Patch, type Prop } from '@dxos/automerge/automerge';
4
- declare const _default: (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => void;
5
- export default _default;
6
- //# sourceMappingURL=amToCodemirror.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"amToCodemirror.d.ts","sourceRoot":"","sources":["../../../../../src/hooks/automerge/amToCodemirror.ts"],"names":[],"mappings":"AAIA,OAAO,EAA8B,KAAK,eAAe,EAAoB,MAAM,mBAAmB,CAAC;AACvG,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,OAAO,EAGL,KAAK,KAAK,EACV,KAAK,IAAI,EAGV,MAAM,2BAA2B,CAAC;+BAIb,UAAU,aAAa,eAAe,UAAU,IAAI,EAAE,WAAW,KAAK,EAAE;AAA9F,wBAgBE"}
@@ -1,7 +0,0 @@
1
- import { type EditorState, type Transaction } from '@codemirror/state';
2
- import { type Heads } from '@dxos/automerge/automerge';
3
- import { type IDocHandle } from './handle';
4
- import { type Field } from './plugin';
5
- declare const _default: (field: Field, handle: IDocHandle, transactions: Transaction[], state: EditorState) => Heads | undefined;
6
- export default _default;
7
- //# sourceMappingURL=codeMirrorToAm.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"codeMirrorToAm.d.ts","sourceRoot":"","sources":["../../../../../src/hooks/automerge/codeMirrorToAm.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,WAAW,EAAa,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAElF,OAAO,EAAc,KAAK,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAEnE,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,UAAU,CAAC;+CAI5B,UAAU,gBACJ,WAAW,EAAE,SACpB,WAAW,KACjB,KAAK,GAAG,SAAS;AALpB,wBA6BE"}