-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCommand.svelte.ts
More file actions
424 lines (366 loc) · 12.5 KB
/
Copy pathCommand.svelte.ts
File metadata and controls
424 lines (366 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import { insert_default_node, break_text_node } from './transforms.svelte.js';
import { can_switch_mark_type, get_selected_range_types } from './doc_utils.js';
import { is_selection_collapsed, get_char_length, char_slice } from './utils.js';
import type Session from './Session.svelte.js';
// Commands operate on sessions with arbitrary application schemas.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type CommandSession = Session<any>;
export type CommandContext = {
session: CommandSession;
editable: boolean;
};
/**
* Base class for commands that can be executed in response to user actions
* like keyboard shortcuts, menu items, or toolbar buttons.
*
* Commands are stateful and UI-aware, unlike transforms which are pure functions.
* They can have derived state (like is_active for toggle commands) or their own
* state (like form inputs for prompt-based commands).
*
* @example
* ```js
* class SaveCommand extends Command {
* is_enabled() {
* return this.context.editable;
* }
*
* async execute() {
* await update_document(this.context.session);
* this.context.editable = false;
* }
* }
* ```
*/
export default class Command {
context: CommandContext;
/**
* Derived state that indicates if the command is disabled.
* Automatically computed from is_enabled().
*/
disabled = $derived(!this.is_enabled());
/**
* Creates a new Command instance.
*
* @param context - The context object providing access to application state
*/
constructor(context: CommandContext) {
this.context = context;
}
/**
* Determines if the command can currently be executed.
* Override this method to implement command-specific logic.
*/
is_enabled(): boolean {
return true;
}
/**
* Executes the command.
* Override this method to implement the command's behavior.
* Can be async for commands that need to perform asynchronous operations.
*/
execute(): void | Promise<void> {
throw new Error('Not implemented');
}
}
/**
* Command that undoes the last change to the document.
*/
export class UndoCommand extends Command {
is_enabled() {
return this.context.editable && this.context.session.can_undo;
}
execute() {
this.context.session.undo();
}
}
/**
* Command that redoes the last undone change to the document.
*/
export class RedoCommand extends Command {
is_enabled() {
return this.context.editable && this.context.session.can_redo;
}
execute() {
this.context.session.redo();
}
}
/**
* Command that selects the parent of the current selection.
* Useful for navigating up the document hierarchy.
*/
export class SelectParentCommand extends Command {
is_enabled() {
return Boolean(
this.context.editable &&
this.context.session.selection &&
this.context.session.selection.path.length > 3
);
}
execute() {
this.context.session.select_parent();
}
}
/**
* Generic command that toggles a mark on the current text or node selection.
*
* Marks are mutually exclusive, so all touched marks compete: same-type marks
* are removed, a single touched property-less mark may switch type, and mixed
* touched types disable the command. Annotations never affect mark toggling.
*/
export class ToggleMarkCommand extends Command {
node_type: string;
constructor(node_type: string, context: CommandContext) {
super(context);
this.node_type = node_type;
}
active = $derived(this.is_active());
is_active() {
const selected_marks = this.context.session.selected_marks;
return (
selected_marks.length > 0 && selected_marks.every(({ node }) => node?.type === this.node_type)
);
}
is_enabled() {
const { session, editable } = this.context;
const selection = session.selection;
const is_valid_selection =
selection?.type === 'text' ||
(selection?.type === 'node' && !is_selection_collapsed(selection));
const mark_type_is_allowed = session.available_mark_types.includes(this.node_type);
const selected_marks = session.selected_marks;
const selected_mark_types = get_selected_range_types(selected_marks);
if (!editable || !is_valid_selection || !mark_type_is_allowed) return false;
if (selected_mark_types.size > 1) return false;
if (selected_marks.length === 0) {
return Boolean(selection && !is_selection_collapsed(selection));
}
const first_selected_mark = selected_marks[0];
const selected_mark_type = first_selected_mark.node.type;
if (selected_mark_type === this.node_type) return true;
if (selected_marks.length !== 1) return false;
return can_switch_mark_type(session.schema, selected_mark_type, this.node_type);
}
execute() {
this.context.session.apply(this.context.session.tr.toggle_mark(this.node_type));
}
}
/**
* Generic command that toggles an annotation on the current text or node selection.
*
* Annotations only compete with touched annotations of the same type: touched
* same-type annotations are removed, otherwise a new annotation is created.
* Marks and other annotation types never block the toggle.
*/
export class ToggleAnnotationCommand extends Command {
node_type: string;
constructor(node_type: string, context: CommandContext) {
super(context);
this.node_type = node_type;
}
active = $derived(this.is_active());
// Mirrors the filter in tr.toggle_annotation, so active/enabled match what
// execute() does.
relevant_annotations() {
return this.context.session.selected_annotations.filter(
({ node }) => node?.type === this.node_type
);
}
is_active() {
return this.relevant_annotations().length > 0;
}
is_enabled() {
const { session, editable } = this.context;
const selection = session.selection;
const is_valid_selection =
selection?.type === 'text' ||
(selection?.type === 'node' && !is_selection_collapsed(selection));
const annotation_type_is_allowed = session.available_annotation_types.includes(this.node_type);
if (!editable || !is_valid_selection || !annotation_type_is_allowed) return false;
// Removing touched same-type annotations also works from a collapsed
// caret inside the annotation; creating a new one requires an expanded
// selection.
if (this.relevant_annotations().length > 0) return true;
return Boolean(selection && !is_selection_collapsed(selection));
}
execute() {
this.context.session.apply(this.context.session.tr.toggle_annotation(this.node_type));
}
}
/**
* Command that adds a new line character at the current caret position.
* Only works in text selections where newlines are allowed.
*/
export class AddNewLineCommand extends Command {
is_enabled() {
const session = this.context.session;
const selection = session.selection;
return (
this.context.editable &&
selection?.type === 'text' &&
session.inspect(selection.path).allow_newlines
);
}
execute() {
const session = this.context.session;
const selection = session.selection;
if (!selection || selection.type !== 'text') return;
if (!session.inspect(selection.path).allow_newlines) return;
const tr = session.tr;
if (selection.anchor_offset !== selection.focus_offset) {
tr.delete_selection();
}
if (tr.selection.type !== 'text') return;
const collapsed_offset = tr.selection.anchor_offset;
const content = tr.get(tr.selection.path);
const text_before_caret = char_slice(content.content, 0, collapsed_offset);
const line_start_index = text_before_caret.lastIndexOf('\n') + 1;
const current_line_prefix = text_before_caret.slice(line_start_index);
const indentation_match = current_line_prefix.match(/^[\t ]*/);
const indentation = indentation_match ? indentation_match[0] : '';
tr.insert_text(`\n${indentation}`);
session.apply(tr);
}
}
/**
* Command that breaks a text node at the caret position.
* Creates a new node and splits the content between the current and new node.
* Only works in text selections.
*/
export class BreakTextNodeCommand extends Command {
is_enabled() {
const session = this.context.session;
const selection = session.selection;
if (!this.context.editable || selection?.type !== 'text') return false;
const owner_node = session.get(selection.path.slice(0, -1));
const owner_node_schema = owner_node ? session.schema[owner_node.type] : null;
if (!owner_node_schema || owner_node_schema.kind !== 'text') return false;
return session.inspect(selection.path.slice(0, -2))?.type === 'node_array';
}
execute() {
const tr = this.context.session.tr;
if (break_text_node(tr)) {
this.context.session.apply(tr);
}
}
}
/**
* Command that selects all content in the current context.
* Progressively expands selection from text → node → parent node array.
*/
export class SelectAllCommand extends Command {
is_enabled() {
return Boolean(this.context.editable && this.context.session.selection);
}
execute() {
const session = this.context.session;
const selection = session.selection;
if (!selection) {
return;
}
if (selection.type === 'text') {
const text_content = session.get(selection.path);
const text_length = get_char_length(text_content.content);
// Check if all text is already selected
const is_all_text_selected =
Math.min(selection.anchor_offset, selection.focus_offset) === 0 &&
Math.max(selection.anchor_offset, selection.focus_offset) === text_length;
if (!is_all_text_selected) {
// Select all text in the current text node
session.selection = {
type: 'text',
path: selection.path,
anchor_offset: 0,
focus_offset: text_length
};
} else {
// All text is selected, move up to select the containing node
const node_path = selection.path.slice(0, -1); // Remove the property name (e.g., 'content')
// Check if we have enough path segments and if we're inside a node_array
if (node_path.length >= 2) {
const is_inside_node_array =
session.inspect(node_path.slice(0, -1))?.type === 'node_array';
if (is_inside_node_array) {
const node_index = node_path.at(-1) as number;
session.selection = {
type: 'node',
path: node_path.slice(0, -1),
anchor_offset: node_index,
focus_offset: node_index + 1
};
}
}
// Stop expanding - text is not in a selectable node_array
}
} else if (selection.type === 'node') {
const node_array_path = selection.path;
const node_array = session.get(node_array_path);
// Check if the entire node_array is already selected
const is_entire_node_array_selected =
Math.min(selection.anchor_offset, selection.focus_offset) === 0 &&
Math.max(selection.anchor_offset, selection.focus_offset) === node_array.nodes.length;
if (!is_entire_node_array_selected) {
// Select the entire node_array
session.selection = {
type: 'node',
path: node_array_path,
anchor_offset: 0,
focus_offset: node_array.nodes.length
};
} else {
// Entire node_array is selected, try to move up to parent node_array
const parent_path = node_array_path.slice(0, -1);
// Check if we have enough path segments and if parent is a valid node_array
if (parent_path.length >= 2) {
const is_parent_node_array =
session.inspect(parent_path.slice(0, -1))?.type === 'node_array';
if (is_parent_node_array) {
const parent_node_index = parent_path.at(-1) as number;
session.selection = {
type: 'node',
path: parent_path.slice(0, -1),
anchor_offset: parent_node_index,
focus_offset: parent_node_index + 1
};
}
}
// Stop expanding - we've reached the top level
}
} else if (selection.type === 'property') {
// For property selections, select the containing node
const node_path = selection.path.slice(0, -1);
// Check if we have enough path segments and if we're inside a node_array
if (node_path.length >= 2) {
const is_inside_node_array = session.inspect(node_path.slice(0, -1))?.type === 'node_array';
if (is_inside_node_array) {
const node_index = node_path.at(-1) as number;
session.selection = {
type: 'node',
path: node_path.slice(0, -1),
anchor_offset: node_index,
focus_offset: node_index + 1
};
}
}
// Stop expanding - property is not in a selectable node_array
}
}
}
/**
* Command that inserts a default node at the current caret position.
* Only works when a collapsed node selection is active.
*/
export class InsertDefaultNodeCommand extends Command {
is_enabled() {
const selection = this.context.session.selection;
return (
this.context.editable &&
selection?.type === 'node' &&
selection.anchor_offset === selection.focus_offset
);
}
execute() {
const tr = this.context.session.tr;
insert_default_node(tr);
this.context.session.apply(tr);
}
}