Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 425 | 5x 5x 5x 5x 5x 324x 24x 24x 324x 324x 324x 324x 324x 210x 210x 210x 210x 324x 612x 612x 612x 18x 18x 612x 612x 324x 3162x 3162x 3162x 3162x 2610x 2610x 30x 30x 30x 30x 30x 30x 2550x 1674x 1674x 1674x 876x 876x 876x 2610x 552x 552x 138x 138x 414x 402x 402x 24x 12x 402x 12x 12x 12x 324x 6x 6x 6x 6x 6x 6x 6x 6x 324x 102x 102x 102x 102x 102x 102x 324x 324x | import {CursorAnchor} from '../../../json-crdt-extensions/peritext/slice/constants'; import {placeCursor} from './annals'; import type {Range} from '../../../json-crdt-extensions/peritext/rga/Range'; import type {PeritextDataTransfer} from '../../../json-crdt-extensions/peritext/transfer/PeritextDataTransfer'; import type {PeritextEventHandlerMap, PeritextEventTarget} from '../PeritextEventTarget'; import type {Peritext} from '../../../json-crdt-extensions/peritext'; import type {EditorSlices} from '../../../json-crdt-extensions/peritext/editor/EditorSlices'; import type * as events from '../types'; import type {PeritextClipboard, PeritextClipboardData} from '../clipboard/types'; import type {UndoCollector} from '../../types'; import type {UiHandle} from './ui/UiHandle'; import type {Point} from '../../../json-crdt-extensions/peritext/rga/Point'; import {Anchor} from '../../../json-crdt-extensions/peritext/rga/constants'; import type {EditorUi} from '../../../json-crdt-extensions/peritext/editor/types'; const toText = (buf: Uint8Array) => new TextDecoder().decode(buf); export interface PeritextEventDefaultsOpts { clipboard?: PeritextClipboard; transfer?: PeritextDataTransfer; } /** * Implementation of default handlers for Peritext events, such as "insert", * "delete", "cursor", etc. These implementations are used by the * {@link PeritextEventTarget} to provide default behavior for each event type. * If `event.preventDefault()` is called on a Peritext event, the default handler * will not be executed. */ export class PeritextEventDefaults implements PeritextEventHandlerMap { public undo?: UndoCollector; public ui?: UiHandle; protected editorUi: EditorUi = { eol: (point: Point, steps: number): Point | undefined => { const ui = this.ui; if (!ui) return; const res = ui.getLineEnd(point, steps > 0); return res ? res[0] : void 0; }, vert: (point: Point, steps: number): Point | undefined => { const ui = this.ui; Iif (!ui) return; const pos = ui.pointX(point); Iif (!pos) return; const currLine = ui.getLineInfo(point); Iif (!currLine) return; const x = pos[0]; const iterations = Math.abs(steps); let nextPoint = point; for (let i = 0; i < iterations; i++) { const nextLine = steps > 0 ? ui.getNextLineInfo(currLine) : ui.getNextLineInfo(currLine, -1); Iif (!nextLine) break; nextPoint = ui.findPointAtX(x, nextLine); Iif (!nextPoint) break; if (point.anchor === Anchor.Before) nextPoint.refBefore(); else nextPoint.refAfter(); } return nextPoint; }, }; public constructor( public readonly txt: Peritext, public readonly et: PeritextEventTarget, public readonly opts: PeritextEventDefaultsOpts = {}, ) {} public readonly change = (event: CustomEvent<events.ChangeDetail>) => {}; public readonly insert = (event: CustomEvent<events.InsertDetail>) => { const text = event.detail.text; const editor = this.txt.editor; editor.insert(text); this.undo?.capture(); }; public readonly delete = (event: CustomEvent<events.DeleteDetail>) => { const {len = -1, unit = 'char', at} = event.detail; const editor = this.txt.editor; if (at !== undefined) { const point = editor.point(at); editor.cursor.set(point); } editor.delete(len, unit); this.undo?.capture(); }; public readonly cursor = (event: CustomEvent<events.CursorDetail>) => { const {at, edge, len, unit} = event.detail; const txt = this.txt; const editor = txt.editor; // If `at` is specified, it represents the absolute position. We move the // cursor to that position, and leave only one active cursor. All other // are automatically removed when `editor.cursor` getter is accessed. if ((typeof at === 'number' && at >= 0) || typeof at === 'object') { const point = editor.point(at); switch (edge) { case 'focus': case 'anchor': { const cursor = editor.cursor; cursor.setEndpoint(point, edge === 'focus' ? 0 : 1); Iif (cursor.isCollapsed()) { const start = cursor.start; start.refAfter(); cursor.set(start); } break; } case 'new': { editor.addCursor(txt.range(point)); break; } // both default: { // Select a range from the "at" position to the specified length. if (!!len && typeof len === 'number') { const point2 = editor.skip(point, len, unit ?? 'char', this.editorUi); const range = txt.rangeFromPoints(point, point2); // Sorted range. editor.cursor.set(range.start, range.end, len < 0 ? CursorAnchor.End : CursorAnchor.Start); } // Set caret (a collapsed cursor) at the specified position. else { point.refAfter(); editor.cursor.set(point); if (unit) editor.select(unit, this.editorUi); } } } return; } // If `edge` is specified. const isSpecificEdgeSelected = edge === 'focus' || edge === 'anchor'; if (isSpecificEdgeSelected) { editor.move(len ?? 0, unit ?? 'char', edge === 'focus' ? 0 : 1, false, this.editorUi); return; } // If `len` is specified. if (len) { const cursor = editor.cursor; if (cursor.isCollapsed()) editor.move(len, unit ?? 'char', void 0, void 0, this.editorUi); else { if (len > 0) cursor.collapseToEnd(); else cursor.collapseToStart(); } return; } // If `unit` is specified. if (unit) { editor.select(unit, this.editorUi); return; } }; public readonly format = (event: CustomEvent<events.FormatDetail>) => { const {type, store = 'saved', behavior = 'one', data} = event.detail; const editor = this.txt.editor; const slices: EditorSlices = store === 'saved' ? editor.saved : store === 'extra' ? editor.extra : editor.local; switch (behavior) { case 'many': { Iif (type === undefined) throw new Error('TYPE_REQUIRED'); slices.insStack(type, data); break; } case 'one': { Iif (type === undefined) throw new Error('TYPE_REQUIRED'); editor.toggleExclFmt(type, data, slices); break; } case 'erase': { if (type === undefined) editor.eraseFormatting(slices); else slices.insErase(type, data); break; } case 'clear': { editor.clearFormatting(slices); break; } } this.undo?.capture(); }; public readonly marker = (event: CustomEvent<events.MarkerDetail>) => { const {action, type, data} = event.detail; const editor = this.txt.editor; switch (action) { case 'ins': { editor.split(type, data); break; } case 'tog': { Iif (type !== void 0) editor.tglMarker(type, data); break; } case 'upd': { Iif (type !== void 0) editor.updMarker(type, data); break; } case 'del': { editor.delMarker(); break; } } this.undo?.capture(); }; public readonly buffer = async (event: CustomEvent<events.BufferDetail>) => { const opts = this.opts; const clipboard = opts.clipboard; Iif (!clipboard) return; const detail = event.detail; const {action, format} = detail; let range: undefined | Range<any>; const txt = this.txt; const editor = txt.editor; if (detail.range) { const p1 = editor.point(detail.range[0]); const p2 = editor.point(detail.range[1]); range = txt.rangeFromPoints(p1, p2); } else { range = editor.getCursor()?.range(); Iif (!range) range = txt.rangeAll(); } Iif (!range) return; switch (action) { case 'cut': case 'copy': { const copyStyle = () => { Iif (!range) return; Iif (range.length() < 1) { range.end.step(1); Iif (range.length() < 1) range.start.step(-1); } const data = opts.transfer?.toFormat?.(range); clipboard.write(data as unknown as PeritextClipboardData<string>)?.catch((err) => console.error(err)); }; switch (format) { case 'text': { const text = range.text(); clipboard.writeText(text)?.catch((err) => console.error(err)); Iif (action === 'cut') editor.collapseCursors(); break; } case 'style': { copyStyle(); break; } case 'html': case 'hast': case 'json': case 'jsonml': case 'mdast': case 'md': case 'fragment': { const transfer = opts.transfer; Iif (!transfer) return; let text = ''; switch (format) { case 'html': { text = transfer.toHtml(range); break; } case 'hast': { text = JSON.stringify(transfer.toHast(range), null, 2); break; } case 'jsonml': { text = JSON.stringify(transfer.toJson(range), null, 2); break; } case 'json': { text = JSON.stringify(transfer.toView(range), null, 2); break; } case 'mdast': { text = JSON.stringify(transfer.toMdast(range), null, 2); break; } case 'md': { text = transfer.toMarkdown(range); break; } case 'fragment': { text = transfer.toFragment(range) + ''; break; } } clipboard.writeText(text)?.catch((err) => console.error(err)); Iif (action === 'cut') editor.collapseCursors(); break; } default: { // `auto' const transfer = opts.transfer; Iif (!transfer) return; if (range.length() < 1) { copyStyle(); } else { const data = transfer.toClipboard(range); clipboard.write(data as unknown as PeritextClipboardData<string>)?.catch((err) => console.error(err)); Iif (action === 'cut') editor.collapseCursors(); } } } break; } case 'paste': { switch (format) { case 'text': { const data = await clipboard.read(['text/plain', 'text/html']); let buffer: Uint8Array | undefined; if ((buffer = data['text/plain'])) { const text = toText(buffer); this.et.insert(text); } else Iif ((buffer = data['text/html'])) { const html = toText(buffer); const text = opts.transfer?.textFromHtml?.(html) ?? html; this.et.insert(text); } break; } case 'style': { const transfer = opts.transfer; Iif (transfer) { const {html} = detail.data || (await clipboard.readData()); Iif (html) { transfer.fromStyle(range, html); this.et.change(); } } break; } case 'html': case 'hast': case 'json': case 'jsonml': case 'mdast': case 'md': { const data = detail.data; const transfer = opts.transfer; Iif (!transfer) return; let text: string = data?.text || ''; Iif (!text) { const clipboardData = await clipboard.read(['text/plain']); const buffer = clipboardData['text/plain']; Iif (buffer) text = toText(buffer); } Iif (!range.isCollapsed()) editor.delRange(range); range.collapseToStart(); const start = range.start; const pos = start.viewPos(); let inserted: number = 0; switch (format) { case 'html': { inserted = transfer.fromHtml(pos, text); break; } case 'hast': { const json = JSON.parse(text); inserted = transfer.fromHast(pos, json); break; } case 'jsonml': { const json = JSON.parse(text); inserted = transfer.fromJson(pos, json); break; } case 'json': { const json = JSON.parse(text); inserted = transfer.fromView(pos, json); break; } case 'mdast': { const json = JSON.parse(text); inserted = transfer.fromMdast(pos, json); break; } case 'md': { inserted = transfer.fromMarkdown(pos, text); break; } } Iif (inserted) this.et.move(inserted, 'char'); this.et.change(); break; } default: { // 'auto' let data = detail.data; const transfer = opts.transfer; Iif (!transfer) { let text: string = data?.text || ''; Iif (!text) { const clipboardData = await clipboard.read(['text/plain']); const buffer = clipboardData['text/plain']; Iif (buffer) text = toText(buffer); } Iif (text) this.et.insert(text); return; } Iif (!data) data = await clipboard.readData(); const inserted = transfer.fromClipboard(range, data); Iif (inserted) this.et.move(inserted, 'char'); this.et.change(); } } break; } } this.undo?.capture(); }; public readonly annals = (event: CustomEvent<events.AnnalsDetail>) => { const {batch} = event.detail; this.txt.model.applyBatch(batch); const txt = this.txt; const cursor = placeCursor(txt, batch); Iif (cursor) txt.editor.cursor.setRange(cursor); }; } |