@dxos/react-ui-editor 0.4.7-main.e015b9e → 0.4.7-main.ea67fec

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.
@@ -3,7 +3,7 @@
3
3
  //
4
4
 
5
5
  import { syntaxTree } from '@codemirror/language';
6
- import { RangeSetBuilder, type EditorState } from '@codemirror/state';
6
+ import { RangeSetBuilder, type EditorState, StateEffect } from '@codemirror/state';
7
7
  import { EditorView, Decoration, type DecorationSet, WidgetType, ViewPlugin, type ViewUpdate } from '@codemirror/view';
8
8
 
9
9
  import { mx } from '@dxos/react-ui-theme';
@@ -81,20 +81,21 @@ const horizontalRule = Decoration.replace({ widget: new HorizontalRuleWidget() }
81
81
  const checkedTask = Decoration.replace({ widget: new CheckboxWidget(true) });
82
82
  const uncheckedTask = Decoration.replace({ widget: new CheckboxWidget(false) });
83
83
 
84
- const editingRange = (state: EditorState, range: { from: number; to: number }) => {
84
+ const editingRange = (state: EditorState, range: { from: number; to: number }, focus: boolean) => {
85
85
  const {
86
86
  readOnly,
87
87
  selection: {
88
88
  main: { head },
89
89
  },
90
90
  } = state;
91
- return !readOnly && head >= range.from && head <= range.to;
91
+ return focus && !readOnly && head >= range.from && head < range.to;
92
92
  };
93
93
 
94
94
  const MarksByParent = new Set(['CodeMark', 'EmphasisMark', 'StrikethroughMark', 'SubscriptMark', 'SuperscriptMark']);
95
95
 
96
- const buildDecorations = (view: EditorView, options: DecorateOptions): DecorationSet => {
97
- const builder = new RangeSetBuilder<Decoration>();
96
+ const buildDecorations = (view: EditorView, options: DecorateOptions, focus: boolean) => {
97
+ const deco = new RangeSetBuilder<Decoration>();
98
+ const atomicDeco = new RangeSetBuilder<Decoration>();
98
99
  const { state } = view;
99
100
 
100
101
  for (const { from, to } of view.visibleRanges) {
@@ -103,7 +104,7 @@ const buildDecorations = (view: EditorView, options: DecorateOptions): Decoratio
103
104
  to,
104
105
  enter: (node) => {
105
106
  if (node.name === 'FencedCode') {
106
- const editing = editingRange(state, node);
107
+ const editing = editingRange(state, node, focus);
107
108
  // FencedCode > CodeMark > [CodeInfo] > CodeText > CodeMark
108
109
  for (const block of view.viewportLineBlocks) {
109
110
  if (block.to < node.from) {
@@ -114,26 +115,22 @@ const buildDecorations = (view: EditorView, options: DecorateOptions): Decoratio
114
115
  }
115
116
  const first = block.from <= node.from;
116
117
  const last = block.to >= node.to;
117
- builder.add(
118
- block.from,
119
- block.from,
120
- first ? fencedCodeLineFirst : last ? fencedCodeLineLast : fencedCodeLine,
121
- );
118
+ deco.add(block.from, block.from, first ? fencedCodeLineFirst : last ? fencedCodeLineLast : fencedCodeLine);
122
119
  if (!editing && (first || last)) {
123
- builder.add(block.from, block.to, hide);
120
+ atomicDeco.add(block.from, block.to, hide);
124
121
  }
125
122
  }
126
123
  return false;
127
124
  } else if (node.name === 'Link') {
128
125
  const marks = node.node.getChildren('LinkMark');
129
126
  const urlNode = node.node.getChild('URL');
130
- const editing = editingRange(state, node);
127
+ const editing = editingRange(state, node, focus);
131
128
  if (urlNode && marks.length >= 2) {
132
129
  const url = state.sliceDoc(urlNode.from, urlNode.to);
133
130
  if (!editing) {
134
- builder.add(node.from, marks[0].to, hide);
131
+ atomicDeco.add(node.from, marks[0].to, hide);
135
132
  }
136
- builder.add(
133
+ deco.add(
137
134
  marks[0].to,
138
135
  marks[1].from,
139
136
  Decoration.mark({
@@ -147,7 +144,7 @@ const buildDecorations = (view: EditorView, options: DecorateOptions): Decoratio
147
144
  }),
148
145
  );
149
146
  if (!editing) {
150
- builder.add(
147
+ atomicDeco.add(
151
148
  marks[1].from,
152
149
  node.to,
153
150
  options.renderLinkButton
@@ -160,54 +157,94 @@ const buildDecorations = (view: EditorView, options: DecorateOptions): Decoratio
160
157
  }
161
158
  } else if (node.name === 'HeaderMark') {
162
159
  const parent = node.node.parent!;
163
- if (/^ATX/.test(parent.name) && !editingRange(state, state.doc.lineAt(node.from))) {
160
+ if (/^ATX/.test(parent.name) && !editingRange(state, state.doc.lineAt(node.from), focus)) {
164
161
  const next = state.doc.sliceString(node.to, node.to + 1);
165
- builder.add(node.from, node.to + (next === ' ' ? 1 : 0), hide);
162
+ atomicDeco.add(node.from, node.to + (next === ' ' ? 1 : 0), hide);
166
163
  }
167
164
  } else if (node.name === 'HorizontalRule') {
168
- if (!editingRange(state, node)) {
169
- builder.add(node.from, node.to, horizontalRule);
165
+ if (!editingRange(state, node, focus)) {
166
+ deco.add(node.from, node.to, horizontalRule);
170
167
  }
171
168
  } else if (node.name === 'TaskMarker') {
172
169
  // Check if cursor is inside text.
173
- if (!editingRange(state, node)) {
170
+ if (!editingRange(state, node, focus)) {
174
171
  const checked = state.doc.sliceString(node.from + 1, node.to - 1) === 'x';
175
- builder.add(node.from - 2, node.from - 1, Decoration.mark({ class: 'cm-task' }));
176
- builder.add(node.from, node.to, checked ? checkedTask : uncheckedTask);
172
+ atomicDeco.add(node.from - 2, node.from - 1, Decoration.mark({ class: 'cm-task' }));
173
+ atomicDeco.add(node.from, node.to, checked ? checkedTask : uncheckedTask);
177
174
  }
178
175
  } else if (MarksByParent.has(node.name)) {
179
- if (!editingRange(state, node.node.parent!)) {
180
- builder.add(node.from, node.to, hide);
176
+ if (!editingRange(state, node.node.parent!, focus)) {
177
+ atomicDeco.add(node.from, node.to, hide);
181
178
  }
182
179
  }
183
180
  },
184
181
  });
185
182
  }
186
183
 
187
- return builder.finish();
184
+ return { deco: deco.finish(), atomicDeco: atomicDeco.finish() };
188
185
  };
189
186
 
190
187
  export interface DecorateOptions {
191
188
  renderLinkButton?: (el: Element, url: string) => void;
189
+ selectionChangeDelay?: number;
192
190
  }
193
191
 
192
+ const forceUpdate = StateEffect.define<null>();
193
+
194
194
  export const decorateMarkdown = (options: DecorateOptions = {}) => {
195
195
  return [
196
196
  ViewPlugin.fromClass(
197
197
  class {
198
- decorations: DecorationSet;
198
+ pendingUpdate = -1;
199
+ deco: DecorationSet;
200
+ atomicDeco: DecorationSet;
201
+
199
202
  constructor(view: EditorView) {
200
- this.decorations = buildDecorations(view, options);
203
+ ({ deco: this.deco, atomicDeco: this.atomicDeco } = buildDecorations(view, options, view.hasFocus));
201
204
  }
202
205
 
203
206
  update(update: ViewUpdate) {
204
- if (update.docChanged || update.selectionSet || update.viewportChanged) {
205
- this.decorations = buildDecorations(update.view, options);
207
+ if (
208
+ update.docChanged ||
209
+ update.viewportChanged ||
210
+ update.focusChanged ||
211
+ update.transactions.some((tr) => tr.effects.some((e) => e.is(forceUpdate)))
212
+ ) {
213
+ ({ deco: this.deco, atomicDeco: this.atomicDeco } = buildDecorations(
214
+ update.view,
215
+ options,
216
+ update.view.hasFocus,
217
+ ));
218
+ this.clearUpdate();
219
+ } else if (update.selectionSet) {
220
+ this.scheduleUpdate(update.view);
221
+ }
222
+ }
223
+
224
+ clearUpdate() {
225
+ if (this.pendingUpdate > -1) {
226
+ window.clearTimeout(this.pendingUpdate);
227
+ this.pendingUpdate = -1;
206
228
  }
207
229
  }
230
+
231
+ scheduleUpdate(view: EditorView) {
232
+ this.clearUpdate();
233
+ this.pendingUpdate = window.setTimeout(() => {
234
+ view.dispatch({ effects: forceUpdate.of(null) });
235
+ }, options.selectionChangeDelay ?? 400);
236
+ }
237
+
238
+ destroy() {
239
+ this.clearUpdate();
240
+ }
208
241
  },
209
242
  {
210
- decorations: (value) => value.decorations,
243
+ provide: (plugin) => [
244
+ EditorView.atomicRanges.of((view) => view.plugin(plugin)?.atomicDeco ?? Decoration.none),
245
+ EditorView.decorations.of((view) => view.plugin(plugin)?.deco ?? Decoration.none),
246
+ EditorView.decorations.of((view) => view.plugin(plugin)?.atomicDeco ?? Decoration.none),
247
+ ],
211
248
  },
212
249
  ),
213
250
  formattingStyles,
@@ -11,12 +11,13 @@ import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
11
11
  import { invariant } from '@dxos/invariant';
12
12
  import { isAutomergeObject, type Space, type TextObject } from '@dxos/react-client/echo';
13
13
  import { type Identity } from '@dxos/react-client/halo';
14
+ import { hueTokens } from '@dxos/react-ui-theme';
15
+ import { hexToHue } from '@dxos/util';
14
16
 
15
17
  import { SpaceAwarenessProvider } from './awareness-provider';
16
18
  import { type EditorModel, modelState } from './defs';
17
19
  import { automerge, awareness } from '../extensions';
18
20
  import { type DocAccessor } from '../extensions/automerge/defs';
19
- import { cursorColor } from '../styles';
20
21
 
21
22
  export type useTextExtensionsProps = {
22
23
  id: string;
@@ -29,6 +30,9 @@ export type useTextExtensionsProps = {
29
30
  export const useTextExtensions = ({ id, text, space, identity }: useTextExtensionsProps): Extension[] => {
30
31
  const extensions: Extension[] = [automerge(text)];
31
32
 
33
+ const peerId = identity?.identityKey.toHex();
34
+ const { cursorLightValue, cursorDarkValue } = hueTokens[hexToHue(peerId ?? '0')];
35
+
32
36
  if (space && identity) {
33
37
  const awarenessProvider = new SpaceAwarenessProvider({
34
38
  space,
@@ -36,8 +40,8 @@ export const useTextExtensions = ({ id, text, space, identity }: useTextExtensio
36
40
  peerId: identity.identityKey.toHex(),
37
41
  info: {
38
42
  displayName: identity.profile?.displayName ?? generateName(identity.identityKey.toHex()),
39
- color: cursorColor.color,
40
- lightColor: cursorColor.light,
43
+ color: cursorDarkValue,
44
+ lightColor: cursorLightValue,
41
45
  },
42
46
  });
43
47
 
@@ -84,6 +88,9 @@ const createModel = ({ space, identity, text }: UseTextModelProps) => {
84
88
  const obj = text as any as AutomergeTextCompat;
85
89
  const doc = getRawDoc(obj, [obj.field]);
86
90
 
91
+ const peerId = identity?.identityKey.toHex();
92
+ const { cursorLightValue, cursorDarkValue } = hueTokens[hexToHue(peerId ?? '0')];
93
+
87
94
  const awarenessProvider =
88
95
  space &&
89
96
  new SpaceAwarenessProvider({
@@ -91,8 +98,8 @@ const createModel = ({ space, identity, text }: UseTextModelProps) => {
91
98
  channel: `automerge.awareness.${obj.id}`,
92
99
  info: {
93
100
  displayName: identity ? identity.profile?.displayName ?? generateName(identity.identityKey.toHex()) : undefined,
94
- color: cursorColor.color,
95
- lightColor: cursorColor.light,
101
+ color: cursorLightValue,
102
+ lightColor: cursorDarkValue,
96
103
  },
97
104
  peerId: identity?.identityKey.toHex() ?? 'Anonymous',
98
105
  });
@@ -2,7 +2,6 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './cursors';
6
5
  export * from './markdown';
7
6
  export * from './tokens';
8
7
  export * from './layout';
@@ -1,5 +0,0 @@
1
- export declare const cursorColor: {
2
- color: string;
3
- light: string;
4
- };
5
- //# sourceMappingURL=cursors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cursors.d.ts","sourceRoot":"","sources":["../../../../src/styles/cursors.ts"],"names":[],"mappings":"AAoBA,eAAO,MAAM,WAAW;;;CAAsD,CAAC"}
@@ -1,21 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- // Copied from https://github.com/yjs/y-codemirror.next#example.
6
-
7
- import * as random from 'lib0/random';
8
-
9
- const cursorColors = [
10
- { color: '#30bced', light: '#30bced33' },
11
- { color: '#6eeb83', light: '#6eeb8333' },
12
- { color: '#ffbc42', light: '#ffbc4233' },
13
- { color: '#ecd444', light: '#ecd44433' },
14
- { color: '#ee6352', light: '#ee635233' },
15
- { color: '#9ac2c9', light: '#9ac2c933' },
16
- { color: '#8acb88', light: '#8acb8833' },
17
- { color: '#1be7ff', light: '#1be7ff33' },
18
- ];
19
-
20
- // Select a random color for this user.
21
- export const cursorColor = cursorColors[random.uint32() % cursorColors.length];