@dxos/react-ui-editor 0.4.10-main.0302e13 → 0.4.10-main.308cc0e

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.
@@ -2302,261 +2302,255 @@ var List;
2302
2302
  List2[List2["Bullet"] = 1] = "Bullet";
2303
2303
  List2[List2["Task"] = 2] = "Task";
2304
2304
  })(List || (List = {}));
2305
- var setHeading = (level) => {
2306
- return ({ state: state2, dispatch }) => {
2307
- const { selection: { ranges }, doc } = state2;
2308
- const changes = [];
2309
- let prevBlock = -1;
2310
- for (const range of ranges) {
2311
- let sawBlock = false;
2312
- syntaxTree(state2).iterate({
2313
- from: range.from,
2314
- to: range.to,
2315
- enter: (node) => {
2316
- if (!Object.hasOwn(Textblocks, node.name) || prevBlock === node.from) {
2317
- return;
2318
- }
2319
- sawBlock = true;
2320
- prevBlock = node.from;
2321
- const blockType = Textblocks[node.name];
2322
- const isHeading = /heading(\d)/.exec(blockType);
2323
- const curLevel = isHeading ? +isHeading[1] : node.name === "Paragraph" ? 0 : -1;
2324
- if (curLevel < 0 || curLevel === level) {
2325
- return;
2326
- }
2327
- if (curLevel === 0) {
2305
+ var setHeading = (level) => ({ state: state2, dispatch }) => {
2306
+ const { selection: { ranges }, doc } = state2;
2307
+ const changes = [];
2308
+ let prevBlock = -1;
2309
+ for (const range of ranges) {
2310
+ let sawBlock = false;
2311
+ syntaxTree(state2).iterate({
2312
+ from: range.from,
2313
+ to: range.to,
2314
+ enter: (node) => {
2315
+ if (!Object.hasOwn(Textblocks, node.name) || prevBlock === node.from) {
2316
+ return;
2317
+ }
2318
+ sawBlock = true;
2319
+ prevBlock = node.from;
2320
+ const blockType = Textblocks[node.name];
2321
+ const isHeading = /heading(\d)/.exec(blockType);
2322
+ const curLevel = isHeading ? +isHeading[1] : node.name === "Paragraph" ? 0 : -1;
2323
+ if (curLevel < 0 || curLevel === level) {
2324
+ return;
2325
+ }
2326
+ if (curLevel === 0) {
2327
+ changes.push({
2328
+ from: node.from,
2329
+ insert: "#".repeat(level) + " "
2330
+ });
2331
+ } else if (node.name === "SetextHeading1" || node.name === "SetextHeading2") {
2332
+ const nextLine = doc.lineAt(node.to);
2333
+ if (level) {
2328
2334
  changes.push({
2329
2335
  from: node.from,
2330
2336
  insert: "#".repeat(level) + " "
2331
2337
  });
2332
- } else if (node.name === "SetextHeading1" || node.name === "SetextHeading2") {
2333
- const nextLine = doc.lineAt(node.to);
2334
- if (level) {
2335
- changes.push({
2336
- from: node.from,
2337
- insert: "#".repeat(level) + " "
2338
- });
2339
- }
2338
+ }
2339
+ changes.push({
2340
+ from: nextLine.from - 1,
2341
+ to: nextLine.to
2342
+ });
2343
+ } else {
2344
+ if (level === 0) {
2345
+ changes.push({
2346
+ from: node.from,
2347
+ to: Math.min(node.to, node.from + curLevel + 1)
2348
+ });
2349
+ } else if (level < curLevel) {
2340
2350
  changes.push({
2341
- from: nextLine.from - 1,
2342
- to: nextLine.to
2351
+ from: node.from,
2352
+ to: node.from + (curLevel - level)
2343
2353
  });
2344
2354
  } else {
2345
- if (level === 0) {
2346
- changes.push({
2347
- from: node.from,
2348
- to: Math.min(node.to, node.from + curLevel + 1)
2349
- });
2350
- } else if (level < curLevel) {
2351
- changes.push({
2352
- from: node.from,
2353
- to: node.from + (curLevel - level)
2354
- });
2355
- } else {
2356
- changes.push({
2357
- from: node.from,
2358
- insert: "#".repeat(level - curLevel)
2359
- });
2360
- }
2355
+ changes.push({
2356
+ from: node.from,
2357
+ insert: "#".repeat(level - curLevel)
2358
+ });
2361
2359
  }
2362
2360
  }
2363
- });
2364
- let line;
2365
- if (!sawBlock && range.empty && level > 0 && !/\S/.test((line = state2.doc.lineAt(range.from)).text)) {
2366
- changes.push({
2367
- from: line.from,
2368
- to: line.to,
2369
- insert: "#".repeat(level) + " "
2370
- });
2371
2361
  }
2362
+ });
2363
+ let line;
2364
+ if (!sawBlock && range.empty && level > 0 && !/\S/.test((line = state2.doc.lineAt(range.from)).text)) {
2365
+ changes.push({
2366
+ from: line.from,
2367
+ to: line.to,
2368
+ insert: "#".repeat(level) + " "
2369
+ });
2372
2370
  }
2373
- if (!changes.length) {
2374
- return false;
2375
- }
2376
- const changeSet = state2.changes(changes);
2377
- dispatch(state2.update({
2378
- changes: changeSet,
2379
- selection: state2.selection.map(changeSet, 1),
2380
- userEvent: "format.setHeading",
2381
- scrollIntoView: true
2382
- }));
2383
- return true;
2384
- };
2371
+ }
2372
+ if (!changes.length) {
2373
+ return false;
2374
+ }
2375
+ const changeSet = state2.changes(changes);
2376
+ dispatch(state2.update({
2377
+ changes: changeSet,
2378
+ selection: state2.selection.map(changeSet, 1),
2379
+ userEvent: "format.setHeading",
2380
+ scrollIntoView: true
2381
+ }));
2382
+ return true;
2385
2383
  };
2386
- var setStyle = (type, enable) => {
2387
- return ({ state: state2, dispatch }) => {
2388
- const marker = inlineMarkerText(type);
2389
- const changes = state2.changeByRange((range) => {
2390
- if (!enable && range.empty) {
2391
- const after = state2.doc.sliceString(range.head, range.head + 6);
2392
- const found = after.indexOf(marker);
2393
- if (found >= 0 && /^[*~`]*$/.test(after.slice(0, found))) {
2394
- const before = state2.doc.sliceString(range.head - 6, range.head);
2395
- if (before.slice(before.length - found - marker.length, before.length - found) === marker && [
2396
- ...before.slice(before.length - found)
2397
- ].reverse().join("") === after.slice(0, found)) {
2398
- return {
2399
- changes: [
2400
- {
2401
- from: range.head - marker.length - found,
2402
- to: range.head - found
2403
- },
2404
- {
2405
- from: range.head + found,
2406
- to: range.head + found + marker.length
2407
- }
2408
- ],
2409
- range: EditorSelection.cursor(range.from - marker.length)
2410
- };
2411
- }
2384
+ var setStyle = (type, enable) => ({ state: state2, dispatch }) => {
2385
+ const marker = inlineMarkerText(type);
2386
+ const changes = state2.changeByRange((range) => {
2387
+ if (!enable && range.empty) {
2388
+ const after = state2.doc.sliceString(range.head, range.head + 6);
2389
+ const found = after.indexOf(marker);
2390
+ if (found >= 0 && /^[*~`]*$/.test(after.slice(0, found))) {
2391
+ const before = state2.doc.sliceString(range.head - 6, range.head);
2392
+ if (before.slice(before.length - found - marker.length, before.length - found) === marker && [
2393
+ ...before.slice(before.length - found)
2394
+ ].reverse().join("") === after.slice(0, found)) {
2395
+ return {
2396
+ changes: [
2397
+ {
2398
+ from: range.head - marker.length - found,
2399
+ to: range.head - found
2400
+ },
2401
+ {
2402
+ from: range.head + found,
2403
+ to: range.head + found + marker.length
2404
+ }
2405
+ ],
2406
+ range: EditorSelection.cursor(range.from - marker.length)
2407
+ };
2412
2408
  }
2413
2409
  }
2414
- const changes2 = [];
2415
- const changesAtEnd = [];
2416
- let blockStart = -1;
2417
- let blockEnd = -1;
2418
- let startCovered = false;
2419
- let endCovered = false;
2420
- let { from, to } = range;
2421
- syntaxTree(state2).iterate({
2422
- from,
2423
- to,
2424
- enter: (node) => {
2425
- const { name } = node;
2426
- if (Object.hasOwn(Textblocks, name) && Textblocks[name] !== "codeblock") {
2427
- blockStart = blockContentStart(node);
2428
- blockEnd = blockContentEnd(node, state2.doc);
2429
- startCovered = endCovered = false;
2430
- } else if (name === "Link" || name === "Image" && enable) {
2431
- if (from < node.from && to > node.from && to <= node.to) {
2410
+ }
2411
+ const changes2 = [];
2412
+ const changesAtEnd = [];
2413
+ let blockStart = -1;
2414
+ let blockEnd = -1;
2415
+ let startCovered = false;
2416
+ let endCovered = false;
2417
+ let { from, to } = range;
2418
+ syntaxTree(state2).iterate({
2419
+ from,
2420
+ to,
2421
+ enter: (node) => {
2422
+ const { name } = node;
2423
+ if (Object.hasOwn(Textblocks, name) && Textblocks[name] !== "codeblock") {
2424
+ blockStart = blockContentStart(node);
2425
+ blockEnd = blockContentEnd(node, state2.doc);
2426
+ startCovered = endCovered = false;
2427
+ } else if (name === "Link" || name === "Image" && enable) {
2428
+ if (from < node.from && to > node.from && to <= node.to) {
2429
+ to = node.to;
2430
+ } else if (to > node.to && from >= node.from && from < node.to) {
2431
+ from = node.from;
2432
+ }
2433
+ } else if (IgnoreInline.has(name) && enable) {
2434
+ if (node.from < from && node.to > from) {
2435
+ if (to === from) {
2432
2436
  to = node.to;
2433
- } else if (to > node.to && from >= node.from && from < node.to) {
2434
- from = node.from;
2435
- }
2436
- } else if (IgnoreInline.has(name) && enable) {
2437
- if (node.from < from && node.to > from) {
2438
- if (to === from) {
2439
- to = node.to;
2440
- }
2441
- from = node.to;
2442
2437
  }
2443
- if (node.from < to && node.to > to) {
2444
- to = node.from;
2438
+ from = node.to;
2439
+ }
2440
+ if (node.from < to && node.to > to) {
2441
+ to = node.from;
2442
+ }
2443
+ } else if (Object.hasOwn(InlineMarker, name)) {
2444
+ const markType = InlineMarker[name];
2445
+ const size = inlineMarkerText(markType).length;
2446
+ const openEnd = node.from + size;
2447
+ const closeStart = node.to - size;
2448
+ if (markType === type) {
2449
+ if (openEnd <= from && closeStart >= from) {
2450
+ startCovered = !enable && openEnd === skipMarkers(from, node.node, -1, openEnd) ? {
2451
+ from: node.from,
2452
+ to: openEnd
2453
+ } : true;
2445
2454
  }
2446
- } else if (Object.hasOwn(InlineMarker, name)) {
2447
- const markType = InlineMarker[name];
2448
- const size = inlineMarkerText(markType).length;
2449
- const openEnd = node.from + size;
2450
- const closeStart = node.to - size;
2451
- if (markType === type) {
2452
- if (openEnd <= from && closeStart >= from) {
2453
- startCovered = !enable && openEnd === skipMarkers(from, node.node, -1, openEnd) ? {
2454
- from: node.from,
2455
- to: openEnd
2456
- } : true;
2457
- }
2458
- if (openEnd <= to && closeStart >= to) {
2459
- endCovered = !enable && closeStart === skipMarkers(to, node.node, 1, closeStart) ? {
2460
- from: closeStart,
2461
- to: node.to
2462
- } : true;
2463
- }
2455
+ if (openEnd <= to && closeStart >= to) {
2456
+ endCovered = !enable && closeStart === skipMarkers(to, node.node, 1, closeStart) ? {
2457
+ from: closeStart,
2458
+ to: node.to
2459
+ } : true;
2464
2460
  }
2465
- if (markType === type || type === 3 && enable) {
2466
- if (node.from >= from && openEnd <= to) {
2467
- changes2.push({
2468
- from: node.from,
2469
- to: openEnd
2461
+ }
2462
+ if (markType === type || type === 3 && enable) {
2463
+ if (node.from >= from && openEnd <= to) {
2464
+ changes2.push({
2465
+ from: node.from,
2466
+ to: openEnd
2467
+ });
2468
+ if (markType !== type && closeStart >= to) {
2469
+ changesAtEnd.push({
2470
+ from: skipSpaces(Math.min(to, blockEnd), state2.doc, 1, blockEnd),
2471
+ insert: inlineMarkerText(markType)
2470
2472
  });
2471
- if (markType !== type && closeStart >= to) {
2472
- changesAtEnd.push({
2473
- from: skipSpaces(Math.min(to, blockEnd), state2.doc, 1, blockEnd),
2474
- insert: inlineMarkerText(markType)
2475
- });
2476
- }
2477
2473
  }
2478
- if (closeStart >= from && node.to <= to) {
2474
+ }
2475
+ if (closeStart >= from && node.to <= to) {
2476
+ changes2.push({
2477
+ from: closeStart,
2478
+ to: node.to
2479
+ });
2480
+ if (markType !== type && openEnd <= from) {
2479
2481
  changes2.push({
2480
- from: closeStart,
2481
- to: node.to
2482
+ from: skipSpaces(Math.max(from, blockStart), state2.doc, -1, blockStart),
2483
+ insert: inlineMarkerText(markType)
2482
2484
  });
2483
- if (markType !== type && openEnd <= from) {
2484
- changes2.push({
2485
- from: skipSpaces(Math.max(from, blockStart), state2.doc, -1, blockStart),
2486
- insert: inlineMarkerText(markType)
2487
- });
2488
- }
2489
2485
  }
2490
2486
  }
2491
2487
  }
2492
- },
2493
- leave: (node) => {
2494
- if (Object.hasOwn(Textblocks, node.name) && Textblocks[node.name] !== "codeblock") {
2495
- const rangeStart = Math.max(from, blockStart);
2496
- const rangeEnd = Math.min(to, blockEnd);
2497
- if (enable) {
2498
- if (!startCovered) {
2499
- changes2.push({
2500
- from: rangeStart,
2501
- insert: marker
2502
- });
2503
- }
2504
- if (!endCovered) {
2505
- changes2.push({
2506
- from: rangeEnd,
2507
- insert: marker
2508
- });
2509
- }
2510
- } else {
2511
- if (typeof startCovered === "object") {
2512
- changes2.push(startCovered);
2513
- } else if (startCovered) {
2514
- changes2.push({
2515
- from: skipSpaces(rangeStart, state2.doc, -1, blockStart),
2516
- insert: marker
2517
- });
2518
- }
2519
- if (typeof endCovered === "object") {
2520
- changes2.push(endCovered);
2521
- } else if (endCovered) {
2522
- changes2.push({
2523
- from: skipSpaces(rangeEnd, state2.doc, 1, blockEnd),
2524
- insert: marker
2525
- });
2526
- }
2488
+ }
2489
+ },
2490
+ leave: (node) => {
2491
+ if (Object.hasOwn(Textblocks, node.name) && Textblocks[node.name] !== "codeblock") {
2492
+ const rangeStart = Math.max(from, blockStart);
2493
+ const rangeEnd = Math.min(to, blockEnd);
2494
+ if (enable) {
2495
+ if (!startCovered) {
2496
+ changes2.push({
2497
+ from: rangeStart,
2498
+ insert: marker
2499
+ });
2500
+ }
2501
+ if (!endCovered) {
2502
+ changes2.push({
2503
+ from: rangeEnd,
2504
+ insert: marker
2505
+ });
2506
+ }
2507
+ } else {
2508
+ if (typeof startCovered === "object") {
2509
+ changes2.push(startCovered);
2510
+ } else if (startCovered) {
2511
+ changes2.push({
2512
+ from: skipSpaces(rangeStart, state2.doc, -1, blockStart),
2513
+ insert: marker
2514
+ });
2515
+ }
2516
+ if (typeof endCovered === "object") {
2517
+ changes2.push(endCovered);
2518
+ } else if (endCovered) {
2519
+ changes2.push({
2520
+ from: skipSpaces(rangeEnd, state2.doc, 1, blockEnd),
2521
+ insert: marker
2522
+ });
2527
2523
  }
2528
2524
  }
2529
2525
  }
2530
- });
2531
- if (blockStart < 0 && range.empty && enable && !/\S/.test(state2.doc.lineAt(range.from).text)) {
2532
- return {
2533
- changes: {
2534
- from: range.head,
2535
- insert: marker + marker
2536
- },
2537
- range: EditorSelection.cursor(range.head + marker.length)
2538
- };
2539
2526
  }
2540
- const changeSet = state2.changes(changes2.concat(changesAtEnd));
2527
+ });
2528
+ if (blockStart < 0 && range.empty && enable && !/\S/.test(state2.doc.lineAt(range.from).text)) {
2541
2529
  return {
2542
- changes: changeSet,
2543
- range: range.empty && !changeSet.empty ? EditorSelection.cursor(range.head + marker.length) : EditorSelection.range(changeSet.mapPos(range.from, 1), changeSet.mapPos(range.to, -1))
2530
+ changes: {
2531
+ from: range.head,
2532
+ insert: marker + marker
2533
+ },
2534
+ range: EditorSelection.cursor(range.head + marker.length)
2544
2535
  };
2545
- });
2546
- dispatch(state2.update(changes, {
2547
- userEvent: enable ? "format.style.add" : "format.style.remove",
2548
- scrollIntoView: true
2549
- }));
2550
- return true;
2551
- };
2536
+ }
2537
+ const changeSet = state2.changes(changes2.concat(changesAtEnd));
2538
+ return {
2539
+ changes: changeSet,
2540
+ range: range.empty && !changeSet.empty ? EditorSelection.cursor(range.head + marker.length) : EditorSelection.range(changeSet.mapPos(range.from, 1), changeSet.mapPos(range.to, -1))
2541
+ };
2542
+ });
2543
+ dispatch(state2.update(changes, {
2544
+ userEvent: enable ? "format.style.add" : "format.style.remove",
2545
+ scrollIntoView: true
2546
+ }));
2547
+ return true;
2552
2548
  };
2553
2549
  var addStyle = (style) => setStyle(style, true);
2554
2550
  var removeStyle = (style) => setStyle(style, false);
2555
- var toggleStyle = (style) => {
2556
- return (arg) => {
2557
- const form = getFormatting(arg.state);
2558
- return setStyle(style, style === 0 ? !form.strong : style === 1 ? !form.emphasis : style === 2 ? !form.strikethrough : !form.code)(arg);
2559
- };
2551
+ var toggleStyle = (style) => (arg) => {
2552
+ const form = getFormatting(arg.state);
2553
+ return setStyle(style, style === 0 ? !form.strong : style === 1 ? !form.emphasis : style === 2 ? !form.strikethrough : !form.code)(arg);
2560
2554
  };
2561
2555
  var toggleStrong = toggleStyle(0);
2562
2556
  var toggleEmphasis = toggleStyle(1);
@@ -2660,305 +2654,296 @@ var removeLink = ({ state: state2, dispatch }) => {
2660
2654
  }));
2661
2655
  return true;
2662
2656
  };
2663
- var addLink = ({ url, image: image2 } = {}) => {
2664
- return ({ state: state2, dispatch }) => {
2665
- const changes = state2.changeByRange((range) => {
2666
- let { from, to } = range;
2667
- const cutStyles = [];
2668
- let okay = null;
2669
- syntaxTree(state2).iterate({
2670
- from,
2671
- to,
2672
- enter: (node) => {
2673
- if (Object.hasOwn(Textblocks, node.name)) {
2674
- okay = Textblocks[node.name] !== "codeblock" && from >= blockContentStart(node) && to <= blockContentEnd(node, state2.doc);
2675
- } else if (Object.hasOwn(InlineMarker, node.name)) {
2676
- const sNode = node.node;
2677
- if (node.from < from && node.to <= to) {
2678
- if (sNode.firstChild.to === from) {
2679
- from = node.from;
2680
- } else {
2681
- cutStyles.push(sNode);
2682
- }
2683
- } else if (node.from >= from && node.to > to) {
2684
- if (sNode.lastChild.from === to) {
2685
- to = node.to;
2686
- } else {
2687
- cutStyles.push(sNode);
2688
- }
2657
+ var addLink = ({ url, image: image2 } = {}) => ({ state: state2, dispatch }) => {
2658
+ const changes = state2.changeByRange((range) => {
2659
+ let { from, to } = range;
2660
+ const cutStyles = [];
2661
+ let okay = null;
2662
+ syntaxTree(state2).iterate({
2663
+ from,
2664
+ to,
2665
+ enter: (node) => {
2666
+ if (Object.hasOwn(Textblocks, node.name)) {
2667
+ okay = Textblocks[node.name] !== "codeblock" && from >= blockContentStart(node) && to <= blockContentEnd(node, state2.doc);
2668
+ } else if (Object.hasOwn(InlineMarker, node.name)) {
2669
+ const sNode = node.node;
2670
+ if (node.from < from && node.to <= to) {
2671
+ if (sNode.firstChild.to === from) {
2672
+ from = node.from;
2673
+ } else {
2674
+ cutStyles.push(sNode);
2675
+ }
2676
+ } else if (node.from >= from && node.to > to) {
2677
+ if (sNode.lastChild.from === to) {
2678
+ to = node.to;
2679
+ } else {
2680
+ cutStyles.push(sNode);
2689
2681
  }
2690
2682
  }
2691
2683
  }
2692
- });
2693
- if (okay === null) {
2694
- const line = state2.doc.lineAt(from);
2695
- okay = to <= line.to && !/\S/.test(line.text.slice(from - line.from));
2696
- }
2697
- if (!okay) {
2698
- return {
2699
- range
2700
- };
2701
- }
2702
- const changes2 = [];
2703
- const changesAfter = [];
2704
- removeLinkInner(from, to, changesAfter, state2);
2705
- let cursorOffset = 1;
2706
- for (const style of cutStyles) {
2707
- const type = InlineMarker[style.name];
2708
- const mark2 = inlineMarkerText(type);
2709
- if (style.from < from) {
2710
- changes2.push({
2711
- from: skipSpaces(from, state2.doc, -1),
2712
- insert: mark2
2713
- });
2714
- changesAfter.push({
2715
- from: skipSpaces(from, state2.doc, 1, to),
2716
- insert: mark2
2717
- });
2718
- } else {
2719
- changes2.push({
2720
- from: skipSpaces(to, state2.doc, -1, from),
2721
- insert: mark2
2722
- });
2723
- const after = skipSpaces(to, state2.doc, 1);
2724
- if (after === to) {
2725
- cursorOffset += mark2.length;
2726
- }
2727
- changesAfter.push({
2728
- from: after,
2729
- insert: mark2
2730
- });
2731
- }
2732
2684
  }
2733
- changes2.push({
2734
- from,
2735
- insert: image2 ? "![" : "["
2736
- }, {
2737
- from: to,
2738
- insert: `](${url ?? ""})`
2739
- });
2740
- const changeSet = state2.changes(changes2.concat(changesAfter));
2685
+ });
2686
+ if (okay === null) {
2687
+ const line = state2.doc.lineAt(from);
2688
+ okay = to <= line.to && !/\S/.test(line.text.slice(from - line.from));
2689
+ }
2690
+ if (!okay) {
2741
2691
  return {
2742
- changes: changeSet,
2743
- range: EditorSelection.cursor(changeSet.mapPos(to, 1) - cursorOffset - (url ? url.length + 2 : 0))
2692
+ range
2744
2693
  };
2745
- });
2746
- if (changes.changes.empty) {
2747
- return false;
2748
2694
  }
2749
- dispatch(state2.update(changes, {
2750
- userEvent: "format.link.add",
2751
- scrollIntoView: true
2752
- }));
2753
- return true;
2754
- };
2695
+ const changes2 = [];
2696
+ const changesAfter = [];
2697
+ removeLinkInner(from, to, changesAfter, state2);
2698
+ let cursorOffset = 1;
2699
+ for (const style of cutStyles) {
2700
+ const type = InlineMarker[style.name];
2701
+ const mark2 = inlineMarkerText(type);
2702
+ if (style.from < from) {
2703
+ changes2.push({
2704
+ from: skipSpaces(from, state2.doc, -1),
2705
+ insert: mark2
2706
+ });
2707
+ changesAfter.push({
2708
+ from: skipSpaces(from, state2.doc, 1, to),
2709
+ insert: mark2
2710
+ });
2711
+ } else {
2712
+ changes2.push({
2713
+ from: skipSpaces(to, state2.doc, -1, from),
2714
+ insert: mark2
2715
+ });
2716
+ const after = skipSpaces(to, state2.doc, 1);
2717
+ if (after === to) {
2718
+ cursorOffset += mark2.length;
2719
+ }
2720
+ changesAfter.push({
2721
+ from: after,
2722
+ insert: mark2
2723
+ });
2724
+ }
2725
+ }
2726
+ changes2.push({
2727
+ from,
2728
+ insert: image2 ? "![" : "["
2729
+ }, {
2730
+ from: to,
2731
+ insert: `](${url ?? ""})`
2732
+ });
2733
+ const changeSet = state2.changes(changes2.concat(changesAfter));
2734
+ return {
2735
+ changes: changeSet,
2736
+ range: EditorSelection.cursor(changeSet.mapPos(to, 1) - cursorOffset - (url ? url.length + 2 : 0))
2737
+ };
2738
+ });
2739
+ if (changes.changes.empty) {
2740
+ return false;
2741
+ }
2742
+ dispatch(state2.update(changes, {
2743
+ userEvent: "format.link.add",
2744
+ scrollIntoView: true
2745
+ }));
2746
+ return true;
2755
2747
  };
2756
- var addList = (type) => {
2757
- return ({ state: state2, dispatch }) => {
2758
- let lastBlock = -1;
2759
- let counter = 1;
2760
- let first = true;
2761
- let parentColumn = null;
2762
- const blocks = [];
2763
- for (const { from, to } of state2.selection.ranges) {
2764
- syntaxTree(state2).iterate({
2765
- from,
2766
- to,
2767
- enter: (node) => {
2768
- if (Object.hasOwn(Textblocks, node.name) && node.name !== "TableCell" || node.name === "Table") {
2769
- if (first) {
2770
- let before = node.node.prevSibling;
2771
- while (before && /Mark$/.test(before.name)) {
2772
- before = before.prevSibling;
2773
- }
2774
- if (before?.name === (type === 0 ? "OrderedList" : "BulletList")) {
2775
- const item = before.lastChild;
2776
- const itemLine = state2.doc.lineAt(item.from);
2777
- const itemText = itemLine.text.slice(item.from - itemLine.from);
2778
- parentColumn = item.from - itemLine.from + /^\s*/.exec(itemText)[0].length;
2779
- if (type === 0) {
2780
- const mark2 = /^\s*(\d+)[.)]/.exec(itemText);
2781
- if (mark2) {
2782
- parentColumn += mark2[1].length;
2783
- counter = +mark2[1] + 1;
2784
- }
2748
+ var addList = (type) => ({ state: state2, dispatch }) => {
2749
+ let lastBlock = -1;
2750
+ let counter = 1;
2751
+ let first = true;
2752
+ let parentColumn = null;
2753
+ const blocks = [];
2754
+ for (const { from, to } of state2.selection.ranges) {
2755
+ syntaxTree(state2).iterate({
2756
+ from,
2757
+ to,
2758
+ enter: (node) => {
2759
+ if (Object.hasOwn(Textblocks, node.name) && node.name !== "TableCell" || node.name === "Table") {
2760
+ if (first) {
2761
+ let before = node.node.prevSibling;
2762
+ while (before && /Mark$/.test(before.name)) {
2763
+ before = before.prevSibling;
2764
+ }
2765
+ if (before?.name === (type === 0 ? "OrderedList" : "BulletList")) {
2766
+ const item = before.lastChild;
2767
+ const itemLine = state2.doc.lineAt(item.from);
2768
+ const itemText = itemLine.text.slice(item.from - itemLine.from);
2769
+ parentColumn = item.from - itemLine.from + /^\s*/.exec(itemText)[0].length;
2770
+ if (type === 0) {
2771
+ const mark2 = /^\s*(\d+)[.)]/.exec(itemText);
2772
+ if (mark2) {
2773
+ parentColumn += mark2[1].length;
2774
+ counter = +mark2[1] + 1;
2785
2775
  }
2786
2776
  }
2787
- first = false;
2788
- }
2789
- if (node.from === lastBlock) {
2790
- return;
2791
2777
  }
2792
- lastBlock = node.from;
2793
- blocks.push({
2794
- node: node.node,
2795
- counter,
2796
- parentColumn
2797
- });
2798
- counter++;
2799
- return false;
2778
+ first = false;
2800
2779
  }
2801
- },
2802
- leave: (node) => {
2803
- if (node.name === "BulletList" || node.name === "OrderedList" || node.name === "Blockquote") {
2804
- counter = 1;
2805
- parentColumn = null;
2780
+ if (node.from === lastBlock) {
2781
+ return;
2806
2782
  }
2783
+ lastBlock = node.from;
2784
+ blocks.push({
2785
+ node: node.node,
2786
+ counter,
2787
+ parentColumn
2788
+ });
2789
+ counter++;
2790
+ return false;
2791
+ }
2792
+ },
2793
+ leave: (node) => {
2794
+ if (node.name === "BulletList" || node.name === "OrderedList" || node.name === "Blockquote") {
2795
+ counter = 1;
2796
+ parentColumn = null;
2807
2797
  }
2808
- });
2809
- }
2810
- if (!blocks.length) {
2811
- const { from, to } = state2.doc.lineAt(state2.selection.main.anchor);
2812
- if (from === to) {
2813
- const insert = type === 1 ? "- " : type === 0 ? "1. " : "- [ ] ";
2814
- dispatch(state2.update({
2815
- changes: [
2816
- {
2817
- from,
2818
- insert
2819
- }
2820
- ],
2821
- selection: {
2822
- anchor: from + insert.length
2823
- },
2824
- userEvent: "format.list.add",
2825
- scrollIntoView: true
2826
- }));
2827
- return true;
2828
- }
2829
- return false;
2830
- }
2831
- const changes = [];
2832
- for (let i = 0; i < blocks.length; i++) {
2833
- const { node, counter: counter2, parentColumn: parentColumn2 } = blocks[i];
2834
- const nodeFrom = node.name === "CodeBlock" ? node.from - 4 : node.from;
2835
- let padding = nodeFrom > 0 && !/\s/.test(state2.doc.sliceString(nodeFrom - 1, nodeFrom)) ? 1 : 0;
2836
- if (type === 0) {
2837
- padding += String(counter2).length;
2838
- }
2839
- let line = state2.doc.lineAt(nodeFrom);
2840
- const column = nodeFrom - line.from;
2841
- if (parentColumn2 !== null && parentColumn2 > column) {
2842
- padding = Math.max(padding, parentColumn2 - column);
2843
2798
  }
2844
- let mark2;
2845
- if (type === 0) {
2846
- let max = counter2;
2847
- for (let j = i + 1; j < blocks.length; j++) {
2848
- if (blocks[j].counter !== max + 1) {
2849
- break;
2799
+ });
2800
+ }
2801
+ if (!blocks.length) {
2802
+ const { from, to } = state2.doc.lineAt(state2.selection.main.anchor);
2803
+ if (from === to) {
2804
+ const insert = type === 1 ? "- " : type === 0 ? "1. " : "- [ ] ";
2805
+ dispatch(state2.update({
2806
+ changes: [
2807
+ {
2808
+ from,
2809
+ insert
2850
2810
  }
2851
- max++;
2811
+ ],
2812
+ selection: {
2813
+ anchor: from + insert.length
2814
+ },
2815
+ userEvent: "format.list.add",
2816
+ scrollIntoView: true
2817
+ }));
2818
+ return true;
2819
+ }
2820
+ return false;
2821
+ }
2822
+ const changes = [];
2823
+ for (let i = 0; i < blocks.length; i++) {
2824
+ const { node, counter: counter2, parentColumn: parentColumn2 } = blocks[i];
2825
+ const nodeFrom = node.name === "CodeBlock" ? node.from - 4 : node.from;
2826
+ let padding = nodeFrom > 0 && !/\s/.test(state2.doc.sliceString(nodeFrom - 1, nodeFrom)) ? 1 : 0;
2827
+ if (type === 0) {
2828
+ padding += String(counter2).length;
2829
+ }
2830
+ let line = state2.doc.lineAt(nodeFrom);
2831
+ const column = nodeFrom - line.from;
2832
+ if (parentColumn2 !== null && parentColumn2 > column) {
2833
+ padding = Math.max(padding, parentColumn2 - column);
2834
+ }
2835
+ let mark2;
2836
+ if (type === 0) {
2837
+ let max = counter2;
2838
+ for (let j = i + 1; j < blocks.length; j++) {
2839
+ if (blocks[j].counter !== max + 1) {
2840
+ break;
2852
2841
  }
2853
- const num = String(counter2);
2854
- padding = Math.max(String(max).length, padding);
2855
- mark2 = " ".repeat(Math.max(0, padding - num.length)) + num + ". ";
2856
- } else {
2857
- mark2 = " ".repeat(padding) + "- " + (type === 2 ? "[ ] " : "");
2842
+ max++;
2858
2843
  }
2844
+ const num = String(counter2);
2845
+ padding = Math.max(String(max).length, padding);
2846
+ mark2 = " ".repeat(Math.max(0, padding - num.length)) + num + ". ";
2847
+ } else {
2848
+ mark2 = " ".repeat(padding) + "- " + (type === 2 ? "[ ] " : "");
2849
+ }
2850
+ changes.push({
2851
+ from: nodeFrom,
2852
+ insert: mark2
2853
+ });
2854
+ while (line.to < node.to) {
2855
+ line = state2.doc.lineAt(line.to + 1);
2856
+ const open = /^[\s>]*/.exec(line.text)[0].length;
2859
2857
  changes.push({
2860
- from: nodeFrom,
2861
- insert: mark2
2858
+ from: line.from + Math.min(open, column),
2859
+ insert: " ".repeat(mark2.length)
2862
2860
  });
2863
- while (line.to < node.to) {
2864
- line = state2.doc.lineAt(line.to + 1);
2865
- const open = /^[\s>]*/.exec(line.text)[0].length;
2866
- changes.push({
2867
- from: line.from + Math.min(open, column),
2868
- insert: " ".repeat(mark2.length)
2869
- });
2870
- }
2871
2861
  }
2872
- if (type === 0) {
2873
- const last = blocks[blocks.length - 1];
2874
- let next = last.node.nextSibling;
2875
- while (next && /Mark$/.test(next.name)) {
2876
- next = next.nextSibling;
2877
- }
2878
- if (next?.name === "OrderedList") {
2879
- renumberListItems(next.firstChild, last.counter + 1, changes, state2.doc);
2880
- }
2862
+ }
2863
+ if (type === 0) {
2864
+ const last = blocks[blocks.length - 1];
2865
+ let next = last.node.nextSibling;
2866
+ while (next && /Mark$/.test(next.name)) {
2867
+ next = next.nextSibling;
2881
2868
  }
2882
- "Oeswe";
2883
- const changeSet = state2.changes(changes);
2884
- dispatch(state2.update({
2885
- changes: changeSet,
2886
- selection: state2.selection.map(changeSet, 1),
2887
- userEvent: "format.list.add",
2888
- scrollIntoView: true
2889
- }));
2890
- return true;
2891
- };
2869
+ if (next?.name === "OrderedList") {
2870
+ renumberListItems(next.firstChild, last.counter + 1, changes, state2.doc);
2871
+ }
2872
+ }
2873
+ const changeSet = state2.changes(changes);
2874
+ dispatch(state2.update({
2875
+ changes: changeSet,
2876
+ selection: state2.selection.map(changeSet, 1),
2877
+ userEvent: "format.list.add",
2878
+ scrollIntoView: true
2879
+ }));
2880
+ return true;
2892
2881
  };
2893
- var removeList = (type) => {
2894
- return ({ state: state2, dispatch }) => {
2895
- let lastBlock = -1;
2896
- const changes = [];
2897
- const stack = [];
2898
- const targetNodeType = type === 0 ? "OrderedList" : type === 1 ? "BulletList" : "TaskList";
2899
- for (const { from, to } of state2.selection.ranges) {
2900
- syntaxTree(state2).iterate({
2901
- from,
2902
- to,
2903
- enter: (node) => {
2904
- const { name } = node;
2905
- if (name === "BulletList" || name === "OrderedList" || name === "Blockquote") {
2906
- stack.push(name);
2907
- } else if (name === "Task" && stack[stack.length - 1] === "BulletList") {
2908
- stack[stack.length - 1] = "TaskList";
2882
+ var removeList = (type) => ({ state: state2, dispatch }) => {
2883
+ let lastBlock = -1;
2884
+ const changes = [];
2885
+ const stack = [];
2886
+ const targetNodeType = type === 0 ? "OrderedList" : type === 1 ? "BulletList" : "TaskList";
2887
+ for (const { from, to } of state2.selection.ranges) {
2888
+ syntaxTree(state2).iterate({
2889
+ from,
2890
+ to,
2891
+ enter: (node) => {
2892
+ const { name } = node;
2893
+ if (name === "BulletList" || name === "OrderedList" || name === "Blockquote") {
2894
+ stack.push(name);
2895
+ } else if (name === "Task" && stack[stack.length - 1] === "BulletList") {
2896
+ stack[stack.length - 1] = "TaskList";
2897
+ }
2898
+ },
2899
+ leave: (node) => {
2900
+ const { name } = node;
2901
+ if (name === "BulletList" || name === "OrderedList" || name === "Blockquote") {
2902
+ stack.pop();
2903
+ } else if (name === "ListItem" && stack[stack.length - 1] === targetNodeType && node.from !== lastBlock) {
2904
+ lastBlock = node.from;
2905
+ let line = state2.doc.lineAt(node.from);
2906
+ const mark2 = /^\s*(\d+[.)] |[-*+] (\[[ x]\] )?)/.exec(line.text.slice(node.from - line.from));
2907
+ if (!mark2) {
2908
+ return false;
2909
2909
  }
2910
- },
2911
- leave: (node) => {
2912
- const { name } = node;
2913
- if (name === "BulletList" || name === "OrderedList" || name === "Blockquote") {
2914
- stack.pop();
2915
- } else if (name === "ListItem" && stack[stack.length - 1] === targetNodeType && node.from !== lastBlock) {
2916
- lastBlock = node.from;
2917
- let line = state2.doc.lineAt(node.from);
2918
- const mark2 = /^\s*(\d+[.)] |[-*+] (\[[ x]\] )?)/.exec(line.text.slice(node.from - line.from));
2919
- if (!mark2) {
2920
- return false;
2921
- }
2922
- const column = node.from - line.from;
2923
- changes.push({
2924
- from: node.from,
2925
- to: node.from + mark2[0].length
2926
- });
2927
- while (line.to < node.to) {
2928
- line = state2.doc.lineAt(line.to + 1);
2929
- const open = /^[\s>]*/.exec(line.text)[0].length;
2930
- if (open > column) {
2931
- changes.push({
2932
- from: line.from + column,
2933
- to: line.from + Math.min(column + mark2[0].length, open)
2934
- });
2935
- }
2936
- }
2937
- if (node.to >= to) {
2938
- renumberListItems(node.node.nextSibling, 1, changes, state2.doc);
2910
+ const column = node.from - line.from;
2911
+ changes.push({
2912
+ from: node.from,
2913
+ to: node.from + mark2[0].length
2914
+ });
2915
+ while (line.to < node.to) {
2916
+ line = state2.doc.lineAt(line.to + 1);
2917
+ const open = /^[\s>]*/.exec(line.text)[0].length;
2918
+ if (open > column) {
2919
+ changes.push({
2920
+ from: line.from + column,
2921
+ to: line.from + Math.min(column + mark2[0].length, open)
2922
+ });
2939
2923
  }
2940
- return false;
2941
2924
  }
2925
+ if (node.to >= to) {
2926
+ renumberListItems(node.node.nextSibling, 1, changes, state2.doc);
2927
+ }
2928
+ return false;
2942
2929
  }
2943
- });
2944
- }
2945
- if (!changes.length) {
2946
- return false;
2947
- }
2948
- dispatch(state2.update({
2949
- changes,
2950
- userEvent: "format.list.remove",
2951
- scrollIntoView: true
2952
- }));
2953
- return true;
2954
- };
2930
+ }
2931
+ });
2932
+ }
2933
+ if (!changes.length) {
2934
+ return false;
2935
+ }
2936
+ dispatch(state2.update({
2937
+ changes,
2938
+ userEvent: "format.list.remove",
2939
+ scrollIntoView: true
2940
+ }));
2941
+ return true;
2955
2942
  };
2956
- var toggleList = (type) => {
2957
- return (target) => {
2958
- const formatting = getFormatting(target.state);
2959
- const active = formatting.listStyle === (type === 1 ? "bullet" : type === 0 ? "ordered" : "task");
2960
- return (active ? removeList(type) : addList(type))(target);
2961
- };
2943
+ var toggleList = (type) => (target) => {
2944
+ const formatting = getFormatting(target.state);
2945
+ const active = formatting.listStyle === (type === 1 ? "bullet" : type === 0 ? "ordered" : "task");
2946
+ return (active ? removeList(type) : addList(type))(target);
2962
2947
  };
2963
2948
  var renumberListItems = (item, counter, changes, doc) => {
2964
2949
  for (; item; item = item.nextSibling) {
@@ -2978,81 +2963,79 @@ var renumberListItems = (item, counter, changes, doc) => {
2978
2963
  }
2979
2964
  }
2980
2965
  };
2981
- var setBlockquote = (enable) => {
2982
- return ({ state: state2, dispatch }) => {
2983
- const lines = [];
2984
- let lastBlock = -1;
2985
- for (const { from, to } of state2.selection.ranges) {
2986
- const sawBlock = false;
2987
- syntaxTree(state2).iterate({
2988
- from,
2989
- to,
2990
- enter: (node) => {
2991
- if (Object.hasOwn(Textblocks, node.name) || node.name === "Table") {
2992
- if (node.from === lastBlock) {
2993
- return false;
2994
- }
2995
- lastBlock = node.from;
2996
- let line2 = state2.doc.lineAt(node.from);
2997
- if (line2.number > 1) {
2998
- const prevLine = state2.doc.line(line2.number - 1);
2999
- if (/^[>\s]*$/.test(prevLine.text)) {
3000
- if (!enable || lines.length && lines[lines.length - 1].number === prevLine.number - 1) {
3001
- lines.push(prevLine);
3002
- }
2966
+ var setBlockquote = (enable) => ({ state: state2, dispatch }) => {
2967
+ const lines = [];
2968
+ let lastBlock = -1;
2969
+ for (const { from, to } of state2.selection.ranges) {
2970
+ const sawBlock = false;
2971
+ syntaxTree(state2).iterate({
2972
+ from,
2973
+ to,
2974
+ enter: (node) => {
2975
+ if (Object.hasOwn(Textblocks, node.name) || node.name === "Table") {
2976
+ if (node.from === lastBlock) {
2977
+ return false;
2978
+ }
2979
+ lastBlock = node.from;
2980
+ let line2 = state2.doc.lineAt(node.from);
2981
+ if (line2.number > 1) {
2982
+ const prevLine = state2.doc.line(line2.number - 1);
2983
+ if (/^[>\s]*$/.test(prevLine.text)) {
2984
+ if (!enable || lines.length && lines[lines.length - 1].number === prevLine.number - 1) {
2985
+ lines.push(prevLine);
3003
2986
  }
3004
2987
  }
3005
- for (; ; ) {
3006
- lines.push(line2);
3007
- if (line2.to >= node.to) {
3008
- break;
3009
- }
3010
- line2 = state2.doc.line(line2.number + 1);
2988
+ }
2989
+ for (; ; ) {
2990
+ lines.push(line2);
2991
+ if (line2.to >= node.to) {
2992
+ break;
3011
2993
  }
3012
- if (!enable && line2.number < state2.doc.lines) {
3013
- const nextLine = state2.doc.line(line2.number + 1);
3014
- if (/^[>\s]*$/.test(nextLine.text)) {
3015
- lines.push(nextLine);
3016
- }
2994
+ line2 = state2.doc.line(line2.number + 1);
2995
+ }
2996
+ if (!enable && line2.number < state2.doc.lines) {
2997
+ const nextLine = state2.doc.line(line2.number + 1);
2998
+ if (/^[>\s]*$/.test(nextLine.text)) {
2999
+ lines.push(nextLine);
3017
3000
  }
3018
- return false;
3019
3001
  }
3002
+ return false;
3020
3003
  }
3021
- });
3022
- let line;
3023
- if (!sawBlock && enable && from === to && !/\S/.test((line = state2.doc.lineAt(from)).text)) {
3024
- lines.push(line);
3025
3004
  }
3005
+ });
3006
+ let line;
3007
+ if (!sawBlock && enable && from === to && !/\S/.test((line = state2.doc.lineAt(from)).text)) {
3008
+ lines.push(line);
3026
3009
  }
3027
- const changes = [];
3028
- for (const line of lines) {
3029
- if (enable) {
3010
+ }
3011
+ const changes = [];
3012
+ for (const line of lines) {
3013
+ if (enable) {
3014
+ changes.push({
3015
+ from: line.from,
3016
+ insert: /\S/.test(line.text) ? "> " : ">"
3017
+ });
3018
+ } else {
3019
+ const quote = /((?:[\s>\-+*]|\d+[.)])*?)> ?/.exec(line.text);
3020
+ if (quote) {
3030
3021
  changes.push({
3031
- from: line.from,
3032
- insert: /\S/.test(line.text) ? "> " : ">"
3022
+ from: line.from + quote[1].length,
3023
+ to: line.from + quote[0].length
3033
3024
  });
3034
- } else {
3035
- const quote = /((?:[\s>\-+*]|\d+[.)])*?)> ?/.exec(line.text);
3036
- if (quote) {
3037
- changes.push({
3038
- from: line.from + quote[1].length,
3039
- to: line.from + quote[0].length
3040
- });
3041
- }
3042
3025
  }
3043
3026
  }
3044
- if (!changes.length) {
3045
- return false;
3046
- }
3047
- const changeSet = state2.changes(changes);
3048
- dispatch(state2.update({
3049
- changes: changeSet,
3050
- selection: state2.selection.map(changeSet, 1),
3051
- userEvent: enable ? "format.blockquote.add" : "format.blockquote.remove",
3052
- scrollIntoView: true
3053
- }));
3054
- return true;
3055
- };
3027
+ }
3028
+ if (!changes.length) {
3029
+ return false;
3030
+ }
3031
+ const changeSet = state2.changes(changes);
3032
+ dispatch(state2.update({
3033
+ changes: changeSet,
3034
+ selection: state2.selection.map(changeSet, 1),
3035
+ userEvent: enable ? "format.blockquote.add" : "format.blockquote.remove",
3036
+ scrollIntoView: true
3037
+ }));
3038
+ return true;
3056
3039
  };
3057
3040
  var addBlockquote = setBlockquote(true);
3058
3041
  var removeBlockquote = setBlockquote(false);
@@ -3122,8 +3105,8 @@ var addCodeblock = (target) => {
3122
3105
  return true;
3123
3106
  };
3124
3107
  var removeCodeblock = ({ state: state2, dispatch }) => {
3125
- let lastBlock = -1;
3126
3108
  const changes = [];
3109
+ let lastBlock = -1;
3127
3110
  for (const { from, to } of state2.selection.ranges) {
3128
3111
  syntaxTree(state2).iterate({
3129
3112
  from,
@@ -3778,88 +3761,68 @@ var buildDecorations = (view, options, focus) => {
3778
3761
  from,
3779
3762
  to,
3780
3763
  enter: (node) => {
3781
- switch (node.name) {
3782
- case "FencedCode": {
3783
- const editing = editingRange(state2, node, focus);
3784
- for (const block of view.viewportLineBlocks) {
3785
- if (block.to < node.from) {
3786
- continue;
3787
- }
3788
- if (block.from > node.to) {
3789
- break;
3790
- }
3791
- const first = block.from <= node.from;
3792
- const last = block.to >= node.to && /^(\s>)*```$/.test(state2.doc.sliceString(block.from, block.to));
3793
- deco.add(block.from, block.from, first ? fencedCodeLineFirst : last ? fencedCodeLineLast : fencedCodeLine);
3794
- if (!editing && (first || last)) {
3795
- atomicDeco.add(block.from, block.to, hide);
3796
- }
3764
+ if (node.name === "FencedCode") {
3765
+ const editing = editingRange(state2, node, focus);
3766
+ for (const block of view.viewportLineBlocks) {
3767
+ if (block.to < node.from) {
3768
+ continue;
3797
3769
  }
3798
- return false;
3799
- }
3800
- case "Link": {
3801
- const marks = node.node.getChildren("LinkMark");
3802
- const urlNode = node.node.getChild("URL");
3803
- const editing = editingRange(state2, node, focus);
3804
- if (urlNode && marks.length >= 2) {
3805
- const url = state2.sliceDoc(urlNode.from, urlNode.to);
3806
- if (!editing) {
3807
- atomicDeco.add(node.from, marks[0].to, hide);
3808
- }
3809
- deco.add(marks[0].to, marks[1].from, Decoration5.mark({
3810
- tagName: "a",
3811
- attributes: {
3812
- class: "cm-link",
3813
- href: url,
3814
- rel: "noreferrer",
3815
- target: "_blank"
3816
- }
3817
- }));
3818
- if (!editing) {
3819
- atomicDeco.add(marks[1].from, node.to, options.renderLinkButton ? Decoration5.replace({
3820
- widget: new LinkButton(url, options.renderLinkButton)
3821
- }) : hide);
3822
- }
3770
+ if (block.from > node.to) {
3771
+ break;
3823
3772
  }
3824
- break;
3825
- }
3826
- case "HeaderMark": {
3827
- const parent = node.node.parent;
3828
- if (/^ATX/.test(parent.name) && !editingRange(state2, state2.doc.lineAt(node.from), focus)) {
3829
- const next = state2.doc.sliceString(node.to, node.to + 1);
3830
- atomicDeco.add(node.from, node.to + (next === " " ? 1 : 0), hide);
3773
+ const first = block.from <= node.from;
3774
+ const last = block.to >= node.to && /^(\s>)*```$/.test(state2.doc.sliceString(block.from, block.to));
3775
+ deco.add(block.from, block.from, first ? fencedCodeLineFirst : last ? fencedCodeLineLast : fencedCodeLine);
3776
+ if (!editing && (first || last)) {
3777
+ atomicDeco.add(block.from, block.to, hide);
3831
3778
  }
3832
- break;
3833
3779
  }
3834
- case "HorizontalRule": {
3835
- if (!editingRange(state2, node, focus)) {
3836
- deco.add(node.from, node.to, horizontalRule);
3780
+ return false;
3781
+ } else if (node.name === "Link") {
3782
+ const marks = node.node.getChildren("LinkMark");
3783
+ const urlNode = node.node.getChild("URL");
3784
+ const editing = editingRange(state2, node, focus);
3785
+ if (urlNode && marks.length >= 2) {
3786
+ const url = state2.sliceDoc(urlNode.from, urlNode.to);
3787
+ if (!editing) {
3788
+ atomicDeco.add(node.from, marks[0].to, hide);
3837
3789
  }
3838
- break;
3839
- }
3840
- case "TaskMarker": {
3841
- if (!editingRange(state2, node, focus)) {
3842
- const checked = state2.doc.sliceString(node.from + 1, node.to - 1) === "x";
3843
- atomicDeco.add(node.from - 2, node.from - 1, Decoration5.mark({
3844
- class: "cm-task"
3845
- }));
3846
- atomicDeco.add(node.from, node.to, checked ? checkedTask : uncheckedTask);
3790
+ deco.add(marks[0].to, marks[1].from, Decoration5.mark({
3791
+ tagName: "a",
3792
+ attributes: {
3793
+ class: "cm-link",
3794
+ href: url,
3795
+ rel: "noreferrer",
3796
+ target: "_blank"
3797
+ }
3798
+ }));
3799
+ if (!editing) {
3800
+ atomicDeco.add(marks[1].from, node.to, options.renderLinkButton ? Decoration5.replace({
3801
+ widget: new LinkButton(url, options.renderLinkButton)
3802
+ }) : hide);
3847
3803
  }
3848
- break;
3849
3804
  }
3850
- case "ListItem": {
3851
- const start = state2.doc.lineAt(node.from);
3852
- deco.add(start.from, start.from, Decoration5.line({
3853
- class: "cm-list-item"
3805
+ } else if (node.name === "HeaderMark") {
3806
+ const parent = node.node.parent;
3807
+ if (/^ATX/.test(parent.name) && !editingRange(state2, state2.doc.lineAt(node.from), focus)) {
3808
+ const next = state2.doc.sliceString(node.to, node.to + 1);
3809
+ atomicDeco.add(node.from, node.to + (next === " " ? 1 : 0), hide);
3810
+ }
3811
+ } else if (node.name === "HorizontalRule") {
3812
+ if (!editingRange(state2, node, focus)) {
3813
+ deco.add(node.from, node.to, horizontalRule);
3814
+ }
3815
+ } else if (node.name === "TaskMarker") {
3816
+ if (!editingRange(state2, node, focus)) {
3817
+ const checked = state2.doc.sliceString(node.from + 1, node.to - 1) === "x";
3818
+ atomicDeco.add(node.from - 2, node.from - 1, Decoration5.mark({
3819
+ class: "cm-task"
3854
3820
  }));
3855
- break;
3821
+ atomicDeco.add(node.from, node.to, checked ? checkedTask : uncheckedTask);
3856
3822
  }
3857
- default: {
3858
- if (MarksByParent.has(node.name)) {
3859
- if (!editingRange(state2, node.node.parent, focus)) {
3860
- atomicDeco.add(node.from, node.to, hide);
3861
- }
3862
- }
3823
+ } else if (MarksByParent.has(node.name)) {
3824
+ if (!editingRange(state2, node.node.parent, focus)) {
3825
+ atomicDeco.add(node.from, node.to, hide);
3863
3826
  }
3864
3827
  }
3865
3828
  }
@@ -3885,6 +3848,8 @@ var decorateMarkdown = (options = {}) => {
3885
3848
  this.scheduleUpdate(update2.view);
3886
3849
  }
3887
3850
  }
3851
+ // TODO(burdon): BUG: If the cursor is at the end of a link at the end of a line,
3852
+ // the cursor will float in space or be in the wrong position after the decoration is applied.
3888
3853
  scheduleUpdate(view) {
3889
3854
  this.clearUpdate();
3890
3855
  this.pendingUpdate = setTimeout(() => {
@@ -4129,17 +4094,15 @@ var update = (state2, options) => {
4129
4094
  }
4130
4095
  });
4131
4096
  tables.forEach((table2) => {
4132
- const replace = state2.readOnly || cursor < table2.from || cursor > table2.to;
4133
- if (replace) {
4097
+ const hide2 = state2.readOnly || cursor < table2.from || cursor > table2.to;
4098
+ if (hide2) {
4134
4099
  builder.add(table2.from, table2.to, Decoration7.replace({
4135
- block: true,
4136
4100
  widget: new TableWidget(table2)
4137
4101
  }));
4138
- } else {
4139
- builder.add(table2.from, table2.to, Decoration7.mark({
4140
- class: "cm-table"
4141
- }));
4142
4102
  }
4103
+ builder.add(table2.from, table2.to, Decoration7.mark({
4104
+ class: "cm-table"
4105
+ }));
4143
4106
  });
4144
4107
  return builder.finish();
4145
4108
  };