@dxos/react-ui-editor 0.4.10-main.fd4f2a3 → 0.4.10-main.fd8ea31

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 (36) hide show
  1. package/dist/lib/browser/index.mjs +69 -93
  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/TextEditor.d.ts.map +1 -1
  5. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
  6. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
  7. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -1
  8. package/dist/types/src/extensions/automerge/automerge.d.ts +1 -1
  9. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  10. package/dist/types/src/extensions/automerge/automerge.stories.d.ts +1 -1
  11. package/dist/types/src/extensions/automerge/automerge.stories.d.ts.map +1 -1
  12. package/dist/types/src/extensions/automerge/cursor.d.ts +2 -2
  13. package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -1
  14. package/dist/types/src/extensions/automerge/sync.d.ts +1 -1
  15. package/dist/types/src/extensions/automerge/sync.d.ts.map +1 -1
  16. package/dist/types/src/extensions/automerge/update-automerge.d.ts +1 -1
  17. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
  18. package/dist/types/src/extensions/factories.d.ts +2 -3
  19. package/dist/types/src/extensions/factories.d.ts.map +1 -1
  20. package/dist/types/src/hooks/index.d.ts +0 -1
  21. package/dist/types/src/hooks/index.d.ts.map +1 -1
  22. package/package.json +29 -29
  23. package/src/components/TextEditor/TextEditor.stories.tsx +8 -6
  24. package/src/components/TextEditor/TextEditor.tsx +6 -3
  25. package/src/components/Toolbar/Toolbar.stories.tsx +8 -10
  26. package/src/extensions/automerge/automerge.stories.tsx +7 -7
  27. package/src/extensions/automerge/automerge.ts +1 -1
  28. package/src/extensions/automerge/cursor.ts +4 -37
  29. package/src/extensions/automerge/sync.ts +1 -1
  30. package/src/extensions/automerge/update-automerge.ts +1 -1
  31. package/src/extensions/factories.ts +3 -4
  32. package/src/hooks/index.ts +0 -1
  33. package/src/hooks/useTextEditor.ts +1 -1
  34. package/dist/types/src/hooks/useDocAccessor.d.ts +0 -9
  35. package/dist/types/src/hooks/useDocAccessor.d.ts.map +0 -1
  36. package/src/hooks/useDocAccessor.ts +0 -21
@@ -11,11 +11,12 @@ import React, { type FC, type KeyboardEvent, StrictMode, useMemo, useRef, useSta
11
11
  import { createRoot } from 'react-dom/client';
12
12
 
13
13
  import { TextV0Type } from '@braneframe/types';
14
- import * as E from '@dxos/echo-schema';
14
+ import { create } from '@dxos/echo-schema';
15
15
  import { keySymbols, parseShortcut } from '@dxos/keyboard';
16
16
  import { PublicKey } from '@dxos/keys';
17
17
  import { log } from '@dxos/log';
18
18
  import { faker } from '@dxos/random';
19
+ import { createDocAccessor, createEchoObject } from '@dxos/react-client/echo';
19
20
  import { Button, DensityProvider, Input, ThemeProvider, useThemeContext } from '@dxos/react-ui';
20
21
  import { baseSurface, defaultTx, getSize, mx, textBlockWidth } from '@dxos/react-ui-theme';
21
22
  import { withTheme } from '@dxos/storybook-utils';
@@ -241,7 +242,7 @@ type StoryProps = {
241
242
  comments?: Comment[];
242
243
  readonly?: boolean;
243
244
  placeholder?: string;
244
- } & Pick<TextEditorProps, 'extensions'>;
245
+ } & Pick<TextEditorProps, 'selection' | 'extensions'>;
245
246
 
246
247
  const Story = ({
247
248
  id = 'editor-' + PublicKey.random().toHex().slice(0, 8),
@@ -252,8 +253,7 @@ const Story = ({
252
253
  placeholder = 'New document.',
253
254
  ...props
254
255
  }: StoryProps) => {
255
- const [object] = useState(E.object(TextV0Type, { content: text ?? '' }));
256
- const accessor = E.getRawDoc(object, ['content']);
256
+ const [object] = useState(createEchoObject(create(TextV0Type, { content: text ?? '' })));
257
257
 
258
258
  const viewRef = useRef<EditorView>(null);
259
259
  useComments(viewRef.current, id, comments);
@@ -269,10 +269,10 @@ const Story = ({
269
269
  editor: { className: 'min-bs-dvh px-8 bg-white dark:bg-black' },
270
270
  },
271
271
  }),
272
- createDataExtensions({ id, text: accessor }),
272
+ createDataExtensions({ id, text: createDocAccessor(object, ['content']) }),
273
273
  _extensions,
274
274
  ],
275
- [_extensions],
275
+ [_extensions, object],
276
276
  );
277
277
 
278
278
  return (
@@ -295,6 +295,8 @@ export default {
295
295
  parameters: { translations, layout: 'fullscreen' },
296
296
  };
297
297
 
298
+ // TODO(burdon): Test invalid inputs (e.g., selection).
299
+
298
300
  const defaults = [
299
301
  autocomplete({
300
302
  onSearch: (text) => links.filter(({ label }) => label.toLowerCase().includes(text.toLowerCase())),
@@ -16,6 +16,7 @@ import React, {
16
16
  } from 'react';
17
17
 
18
18
  import { log } from '@dxos/log';
19
+ import { useDefaultValue } from '@dxos/react-ui';
19
20
  import { isNotFalsy } from '@dxos/util';
20
21
 
21
22
  import { documentId, editorMode, focusEvent } from '../../extensions';
@@ -51,12 +52,13 @@ export const TextEditor = forwardRef<EditorView | null, TextEditorProps>(
51
52
  (
52
53
  {
53
54
  id,
55
+ // TODO(wittjosiah): Rename initialText?
54
56
  doc,
55
57
  selection,
56
58
  extensions,
57
59
  className,
58
60
  autoFocus,
59
- scrollTo = EditorView.scrollIntoView(0, { yMargin: 0 }),
61
+ scrollTo: propsScrollTo,
60
62
  moveToEndOfLine,
61
63
  debug,
62
64
  dataTestId,
@@ -65,6 +67,7 @@ export const TextEditor = forwardRef<EditorView | null, TextEditorProps>(
65
67
  ) => {
66
68
  // NOTE: Increments by 2 in strict mode.
67
69
  const [instanceId] = useState(() => `text-editor-${++instanceCount}`);
70
+ const scrollTo = useDefaultValue(propsScrollTo, EditorView.scrollIntoView(0, { yMargin: 0 }));
68
71
 
69
72
  // TODO(burdon): Make tabster optional.
70
73
  const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
@@ -90,10 +93,10 @@ export const TextEditor = forwardRef<EditorView | null, TextEditorProps>(
90
93
  //
91
94
  // EditorState
92
95
  // https://codemirror.net/docs/ref/#state.EditorStateConfig
96
+ // NOTE: Don't set selection here in case it is invalid (and crashes the state); dispatch below.
93
97
  //
94
98
  const state = EditorState.create({
95
99
  doc,
96
- selection,
97
100
  extensions: [
98
101
  id && documentId.of(id),
99
102
  // TODO(burdon): NOTE: Doesn't catch errors in keymap functions.
@@ -150,7 +153,7 @@ export const TextEditor = forwardRef<EditorView | null, TextEditorProps>(
150
153
  log('destroy', { id, instanceId });
151
154
  view?.destroy();
152
155
  };
153
- }, [doc, selection, extensions]);
156
+ }, [id, selection, scrollTo, editorMode, extensions]);
154
157
 
155
158
  // Focus editor on Enter (e.g., when tabbing to this component).
156
159
  const handleKeyUp = useCallback<KeyboardEventHandler<HTMLDivElement>>(
@@ -7,9 +7,10 @@ import '@dxosTheme';
7
7
  import React, { type FC, useState } from 'react';
8
8
 
9
9
  import { TextV0Type } from '@braneframe/types';
10
- import * as E from '@dxos/echo-schema';
10
+ import { create } from '@dxos/echo-schema';
11
11
  import { PublicKey } from '@dxos/keys';
12
12
  import { faker } from '@dxos/random';
13
+ import { createDocAccessor } from '@dxos/react-client/echo';
13
14
  import { Tooltip, useThemeContext } from '@dxos/react-ui';
14
15
  import { textBlockWidth } from '@dxos/react-ui-theme';
15
16
  import { withTheme } from '@dxos/storybook-utils';
@@ -36,21 +37,18 @@ faker.seed(101);
36
37
 
37
38
  const Story: FC<{ content: string }> = ({ content }) => {
38
39
  const { themeMode } = useThemeContext();
39
- const [text] = useState(E.object(TextV0Type, { content }));
40
- const id = text.id;
41
- const doc = text.content;
42
- const accessor = E.getRawDoc(text);
40
+ const [text] = useState(create(TextV0Type, { content }));
43
41
  const [formattingState, formattingObserver] = useFormattingState();
44
42
  const { parentRef, view } = useTextEditor(() => {
45
43
  return {
46
- id,
47
- doc,
44
+ id: text.id,
45
+ doc: text.content,
48
46
  extensions: [
49
47
  formattingObserver,
50
48
  createBasicExtensions(),
51
49
  createMarkdownExtensions({ themeMode }),
52
50
  createThemeExtensions({ themeMode, slots: { editor: { className: 'p-2' } } }),
53
- createDataExtensions({ id, text: accessor }),
51
+ createDataExtensions({ id: text.id, text: createDocAccessor(text, ['content']) }),
54
52
  comments({
55
53
  onCreate: ({ cursor }) => {
56
54
  const id = PublicKey.random().toHex();
@@ -64,12 +62,12 @@ const Story: FC<{ content: string }> = ({ content }) => {
64
62
  table(),
65
63
  ],
66
64
  };
67
- }, [id, accessor, formattingObserver, themeMode]);
65
+ }, [text, formattingObserver, themeMode]);
68
66
 
69
67
  const handleAction = useActionHandler(view);
70
68
 
71
69
  const [_comments, setComments] = useState<Comment[]>([]);
72
- useComments(view, id, _comments);
70
+ useComments(view, text.id, _comments);
73
71
 
74
72
  return (
75
73
  <Tooltip.Provider>
@@ -8,11 +8,11 @@ import { BroadcastChannelNetworkAdapter } from '@automerge/automerge-repo-networ
8
8
  import '@preact/signals-react';
9
9
  import React, { useEffect, useMemo, useState } from 'react';
10
10
 
11
+ import { TextV0Type } from '@braneframe/types';
11
12
  import { Repo } from '@dxos/automerge/automerge-repo';
12
- import { Filter, DocAccessor, TextCompatibilitySchema } from '@dxos/echo-schema';
13
- import * as E from '@dxos/echo-schema';
13
+ import { create, type Expando } from '@dxos/echo-schema';
14
14
  import { type PublicKey } from '@dxos/keys';
15
- import { useSpace } from '@dxos/react-client/echo';
15
+ import { Filter, DocAccessor, createDocAccessor, useSpace } from '@dxos/react-client/echo';
16
16
  import { ClientRepeater } from '@dxos/react-client/testing';
17
17
  import { useThemeContext } from '@dxos/react-ui';
18
18
  import { withTheme } from '@dxos/storybook-utils';
@@ -98,9 +98,9 @@ const EchoStory = ({ spaceKey }: { spaceKey: PublicKey }) => {
98
98
  // const identity = useIdentity();
99
99
  const space = useSpace(spaceKey);
100
100
  const source = useMemo<DocAccessor | undefined>(() => {
101
- const { objects = [] } = space?.db.query<E.ExpandoType>(Filter.from({ type: 'test' })) ?? {};
101
+ const { objects = [] } = space?.db.query<Expando>(Filter.from({ type: 'test' })) ?? {};
102
102
  if (objects.length) {
103
- return E.getRawDoc(objects[0].content, ['content']);
103
+ return createDocAccessor(objects[0].content, ['content']);
104
104
  }
105
105
  }, [space]);
106
106
 
@@ -125,9 +125,9 @@ export const WithEcho = {
125
125
  createSpace
126
126
  onCreateSpace={async (space) => {
127
127
  space.db.add(
128
- E.object({
128
+ create({
129
129
  type: 'test',
130
- content: E.object(TextCompatibilitySchema, { content: initialContent }),
130
+ content: create(TextV0Type, { content: initialContent }),
131
131
  }),
132
132
  );
133
133
  }}
@@ -8,7 +8,7 @@ import { StateField, type Extension } from '@codemirror/state';
8
8
  import { EditorView, ViewPlugin } from '@codemirror/view';
9
9
 
10
10
  import { next as A } from '@dxos/automerge/automerge';
11
- import { type DocAccessor } from '@dxos/echo-schema';
11
+ import { type DocAccessor } from '@dxos/react-client/echo';
12
12
 
13
13
  import { cursorConverter } from './cursor';
14
14
  import { updateHeadsEffect, isReconcile, type State } from './defs';
@@ -2,30 +2,16 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import get from 'lodash.get';
6
-
7
- import { next as A } from '@dxos/automerge/automerge';
8
- import { type DocAccessor } from '@dxos/echo-schema';
9
5
  import { log } from '@dxos/log';
6
+ import { toCursor, type DocAccessor, fromCursor } from '@dxos/react-client/echo';
10
7
 
11
8
  import { type CursorConverter } from '../cursor';
12
9
 
13
- export const cursorConverter = ({ handle, path }: DocAccessor): CursorConverter => ({
10
+ export const cursorConverter = (accessor: DocAccessor): CursorConverter => ({
14
11
  // TODO(burdon): Handle assoc to associate with a previous character.
15
12
  toCursor: (pos) => {
16
- const doc = handle.docSync();
17
- if (!doc) {
18
- return '';
19
- }
20
-
21
- const value = get(doc, path);
22
- if (typeof value === 'string' && value.length <= pos) {
23
- return 'end';
24
- }
25
-
26
13
  try {
27
- // NOTE: Slice is needed because getCursor mutates the array.
28
- return A.getCursor(doc, path.slice(), pos);
14
+ return toCursor(accessor, pos);
29
15
  } catch (err) {
30
16
  log.catch(err);
31
17
  return ''; // In case of invalid request (e.g., wrong document).
@@ -33,27 +19,8 @@ export const cursorConverter = ({ handle, path }: DocAccessor): CursorConverter
33
19
  },
34
20
 
35
21
  fromCursor: (cursor) => {
36
- if (cursor === '') {
37
- return 0;
38
- }
39
-
40
- const doc = handle.docSync();
41
- if (!doc) {
42
- return 0;
43
- }
44
-
45
- if (cursor === 'end') {
46
- const value = get(doc, path);
47
- if (typeof value === 'string') {
48
- return value.length;
49
- } else {
50
- return 0;
51
- }
52
- }
53
-
54
22
  try {
55
- // NOTE: Slice is needed because getCursor mutates the array.
56
- return A.getCursorPosition(doc, path.slice(), cursor);
23
+ return fromCursor(accessor, cursor);
57
24
  } catch (err) {
58
25
  log.catch(err);
59
26
  return 0; // In case of invalid request (e.g., wrong document).
@@ -8,7 +8,7 @@ import { type StateField } from '@codemirror/state';
8
8
  import { type EditorView } from '@codemirror/view';
9
9
 
10
10
  import { next as A } from '@dxos/automerge/automerge';
11
- import { type IDocHandle } from '@dxos/echo-schema';
11
+ import { type IDocHandle } from '@dxos/react-client/echo';
12
12
 
13
13
  import { getLastHeads, getPath, type State, reconcileAnnotation, updateHeads, isReconcile } from './defs';
14
14
  import { updateAutomerge } from './update-automerge';
@@ -7,7 +7,7 @@
7
7
  import { type EditorState, type StateField, type Transaction, type Text } from '@codemirror/state';
8
8
 
9
9
  import { next as A, type Heads } from '@dxos/automerge/automerge';
10
- import { type IDocHandle } from '@dxos/echo-schema';
10
+ import { type IDocHandle } from '@dxos/react-client/echo';
11
11
 
12
12
  import { type State } from './defs';
13
13
 
@@ -21,9 +21,8 @@ import {
21
21
  import defaultsDeep from 'lodash.defaultsdeep';
22
22
 
23
23
  import { generateName } from '@dxos/display-name';
24
- import { type DocAccessor } from '@dxos/echo-schema';
25
24
  import { log } from '@dxos/log';
26
- import { type Space } from '@dxos/react-client/echo';
25
+ import { type DocAccessor, type Space } from '@dxos/react-client/echo';
27
26
  import { type Identity } from '@dxos/react-client/halo';
28
27
  import { type ThemeMode } from '@dxos/react-ui';
29
28
  import { type HuePalette, hueTokens } from '@dxos/react-ui-theme';
@@ -162,14 +161,14 @@ export const createThemeExtensions = ({ theme, themeMode, slots: _slots }: Theme
162
161
 
163
162
  export type DataExtensionsProps = {
164
163
  id: string;
165
- text: DocAccessor;
164
+ text?: DocAccessor;
166
165
  space?: Space;
167
166
  identity?: Identity | null;
168
167
  };
169
168
 
170
169
  // TODO(burdon): Move out of react-ui-editor (remove echo deps).
171
170
  export const createDataExtensions = ({ id, text, space, identity }: DataExtensionsProps): Extension[] => {
172
- const extensions: Extension[] = [automerge(text)];
171
+ const extensions: Extension[] = text ? [automerge(text)] : [];
173
172
 
174
173
  if (space && identity) {
175
174
  const peerId = identity?.identityKey.toHex();
@@ -4,4 +4,3 @@
4
4
 
5
5
  export * from './useActionHandler';
6
6
  export * from './useTextEditor';
7
- export * from './useDocAccessor';
@@ -36,9 +36,9 @@ export const useTextEditor = (cb: () => UseTextEditorProps = () => ({}), deps: D
36
36
  log('create', { id });
37
37
 
38
38
  // https://codemirror.net/docs/ref/#state.EditorStateConfig
39
+ // NOTE: Don't set selection here in case it is invalid (and crashes the state); dispatch below.
39
40
  const state = EditorState.create({
40
41
  doc,
41
- selection,
42
42
  extensions: [
43
43
  id && documentId.of(id),
44
44
  // TODO(burdon): Doesn't catch errors in keymap functions.
@@ -1,9 +0,0 @@
1
- import { type DocAccessor, type EchoReactiveObject } from '@dxos/echo-schema';
2
- export declare const useDocAccessor: <T = any>(text: EchoReactiveObject<{
3
- content: string;
4
- }>) => {
5
- id: string;
6
- doc: string | undefined;
7
- accessor: DocAccessor<T>;
8
- };
9
- //# sourceMappingURL=useDocAccessor.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useDocAccessor.d.ts","sourceRoot":"","sources":["../../../../src/hooks/useDocAccessor.ts"],"names":[],"mappings":"AAMA,OAAO,EAAqB,KAAK,WAAW,EAAkB,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAGjH,eAAO,MAAM,cAAc,kBACnB,mBAAmB;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;QACtC,MAAM;SAAO,MAAM,GAAG,SAAS;;CASvC,CAAC"}
@@ -1,21 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { useMemo } from 'react';
6
-
7
- import { createDocAccessor, type DocAccessor, getTextContent, type EchoReactiveObject } from '@dxos/echo-schema';
8
-
9
- // TODO(burdon): Remove.
10
- export const useDocAccessor = <T = any>(
11
- text: EchoReactiveObject<{ content: string }>,
12
- ): { id: string; doc: string | undefined; accessor: DocAccessor<T> } => {
13
- return useMemo(
14
- () => ({
15
- id: text.id,
16
- doc: getTextContent(text),
17
- accessor: createDocAccessor<T>(text),
18
- }),
19
- [text],
20
- );
21
- };