@dxos/react-ui-editor 0.4.10-main.ef6fbc2 → 0.4.10-main.f635fb0

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.
@@ -7,7 +7,7 @@ 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 { createDocAccessor, create } from '@dxos/echo-schema';
11
11
  import { PublicKey } from '@dxos/keys';
12
12
  import { faker } from '@dxos/random';
13
13
  import { Tooltip, useThemeContext } from '@dxos/react-ui';
@@ -36,21 +36,18 @@ faker.seed(101);
36
36
 
37
37
  const Story: FC<{ content: string }> = ({ content }) => {
38
38
  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);
39
+ const [text] = useState(create(TextV0Type, { content }));
43
40
  const [formattingState, formattingObserver] = useFormattingState();
44
41
  const { parentRef, view } = useTextEditor(() => {
45
42
  return {
46
- id,
47
- doc,
43
+ id: text.id,
44
+ doc: text.content,
48
45
  extensions: [
49
46
  formattingObserver,
50
47
  createBasicExtensions(),
51
48
  createMarkdownExtensions({ themeMode }),
52
49
  createThemeExtensions({ themeMode, slots: { editor: { className: 'p-2' } } }),
53
- createDataExtensions({ id, text: accessor }),
50
+ createDataExtensions({ id: text.id, text: createDocAccessor(text, ['content']) }),
54
51
  comments({
55
52
  onCreate: ({ cursor }) => {
56
53
  const id = PublicKey.random().toHex();
@@ -64,12 +61,12 @@ const Story: FC<{ content: string }> = ({ content }) => {
64
61
  table(),
65
62
  ],
66
63
  };
67
- }, [id, accessor, formattingObserver, themeMode]);
64
+ }, [text, formattingObserver, themeMode]);
68
65
 
69
66
  const handleAction = useActionHandler(view);
70
67
 
71
68
  const [_comments, setComments] = useState<Comment[]>([]);
72
- useComments(view, id, _comments);
69
+ useComments(view, text.id, _comments);
73
70
 
74
71
  return (
75
72
  <Tooltip.Provider>
@@ -8,9 +8,9 @@ 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 { Filter, DocAccessor, create, createDocAccessor, type Expando } from '@dxos/echo-schema';
14
14
  import { type PublicKey } from '@dxos/keys';
15
15
  import { useSpace } from '@dxos/react-client/echo';
16
16
  import { ClientRepeater } from '@dxos/react-client/testing';
@@ -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
  }}
@@ -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';
5
+ import { toCursor, type DocAccessor, fromCursor } from '@dxos/echo-schema';
9
6
  import { log } from '@dxos/log';
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).
@@ -162,14 +162,14 @@ export const createThemeExtensions = ({ theme, themeMode, slots: _slots }: Theme
162
162
 
163
163
  export type DataExtensionsProps = {
164
164
  id: string;
165
- text: DocAccessor;
165
+ text?: DocAccessor;
166
166
  space?: Space;
167
167
  identity?: Identity | null;
168
168
  };
169
169
 
170
170
  // TODO(burdon): Move out of react-ui-editor (remove echo deps).
171
171
  export const createDataExtensions = ({ id, text, space, identity }: DataExtensionsProps): Extension[] => {
172
- const extensions: Extension[] = [automerge(text)];
172
+ const extensions: Extension[] = text ? [automerge(text)] : [];
173
173
 
174
174
  if (space && identity) {
175
175
  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
- };