@dou-ui/code 0.1.0-alpha.3 → 0.1.0-alpha.5
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.
- package/dist/code-diff.js.map +1 -1
- package/dist/code-diff.vue_vue_type_script_setup_true_lang.js +422 -16
- package/dist/code-diff.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/code-editor.js.map +1 -1
- package/dist/code-editor.vue_vue_type_script_setup_true_lang.js +109 -48
- package/dist/code-editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/extension.js +6 -1
- package/dist/extension.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/style.css +1 -1
- package/dist/types/code-diff.vue.d.ts +4 -2
- package/dist/types/extension.d.ts +21 -3
- package/dist/types/index.d.ts +4 -2
- package/dist/types/types.d.ts +55 -3
- package/package.json +3 -3
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { splitLines } from "./core/text.js";
|
|
2
2
|
import { format, useCodeMessages } from "./locale.js";
|
|
3
|
-
import { defineCodeExtension } from "./extension.js";
|
|
3
|
+
import { WIDGET_HEIGHT_KEY, defineCodeExtension } from "./extension.js";
|
|
4
|
+
import render_part_default from "./render-part.js";
|
|
4
5
|
import code_editor_default from "./code-editor.js";
|
|
5
6
|
import { diffText, lineStarts } from "./core/diff.js";
|
|
6
7
|
import { DouButton, DouTooltip, createScrollLink, useNamespace } from "dou-ui";
|
|
7
|
-
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, h, nextTick, normalizeClass, onBeforeUnmount, onMounted, openBlock, reactive, ref, toDisplayString, unref, watch, withCtx } from "vue";
|
|
8
|
+
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, h, nextTick, normalizeClass, normalizeStyle, onBeforeUnmount, onMounted, openBlock, provide, reactive, ref, renderList, toDisplayString, unref, watch, withCtx } from "vue";
|
|
8
9
|
import { ArrowDownIcon, ArrowUpIcon, DiffSplitIcon, DiffUnifiedIcon, FoldVerticalIcon, UnfoldVerticalIcon } from "@dou-ui/icons";
|
|
9
10
|
// src/code-diff.vue?vue&type=script&setup=true&lang.ts
|
|
10
11
|
var _hoisted_1 = ["aria-label"];
|
|
12
|
+
var _hoisted_2 = ["role", "aria-label"];
|
|
11
13
|
/**
|
|
12
14
|
* Two texts compared: side by side, or the changes in one column. Changed
|
|
13
15
|
* lines are tinted, the words that changed inside them marked, and long
|
|
@@ -22,6 +24,11 @@ var Kind = /* @__PURE__ */ function(Kind) {
|
|
|
22
24
|
Kind[Kind["Added"] = 2] = "Added";
|
|
23
25
|
return Kind;
|
|
24
26
|
}(Kind || {});
|
|
27
|
+
var LINE = 20;
|
|
28
|
+
/**
|
|
29
|
+
* Where a spot beside line `line` of `side` is on the other side: beside
|
|
30
|
+
* a line there, or inside the filler of a change.
|
|
31
|
+
*/
|
|
25
32
|
var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defineComponent({
|
|
26
33
|
name: "DouCodeDiff",
|
|
27
34
|
__name: "code-diff",
|
|
@@ -51,6 +58,8 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
51
58
|
default: false
|
|
52
59
|
},
|
|
53
60
|
extensions: { default: void 0 },
|
|
61
|
+
gutters: { default: void 0 },
|
|
62
|
+
widgets: { default: void 0 },
|
|
54
63
|
tabSize: { default: 2 },
|
|
55
64
|
toolbar: {
|
|
56
65
|
type: Boolean,
|
|
@@ -131,6 +140,17 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
131
140
|
/** The runs of unchanged lines opened with their bar, by key; `true` for all of them. */
|
|
132
141
|
const opened = reactive(/* @__PURE__ */ new Set());
|
|
133
142
|
const allOpen = ref(false);
|
|
143
|
+
/** The lines with a marker or widget on each side, which collapsing leaves in view. */
|
|
144
|
+
const annotatedLines = computed(() => {
|
|
145
|
+
const out = {
|
|
146
|
+
a: /* @__PURE__ */ new Set(),
|
|
147
|
+
b: /* @__PURE__ */ new Set()
|
|
148
|
+
};
|
|
149
|
+
const add = (side, line) => out[side === "original" ? "a" : "b"].add(line);
|
|
150
|
+
for (const gutter of props.gutters ?? []) for (const marker of gutter.markers) add(marker.side, marker.line);
|
|
151
|
+
for (const widget of props.widgets ?? []) add(widget.side, widget.line);
|
|
152
|
+
return out;
|
|
153
|
+
});
|
|
134
154
|
const unchanged = computed(() => {
|
|
135
155
|
const list = hunks.value;
|
|
136
156
|
const total = originalLines.value.length;
|
|
@@ -157,15 +177,25 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
157
177
|
key: a
|
|
158
178
|
});
|
|
159
179
|
};
|
|
180
|
+
const kept = annotatedLines.value;
|
|
181
|
+
const cut = (a, b, count, before, after) => {
|
|
182
|
+
let from = 0;
|
|
183
|
+
for (let i = 0; i < count; i++) {
|
|
184
|
+
if (!kept.a.has(a + i) && !kept.b.has(b + i)) continue;
|
|
185
|
+
if (i > from) push(a + from, b + from, i - from, from > 0 || before, true);
|
|
186
|
+
from = Math.max(from, i);
|
|
187
|
+
}
|
|
188
|
+
push(a + from, b + from, count - from, from > 0 || before, after);
|
|
189
|
+
};
|
|
160
190
|
let a = 1;
|
|
161
191
|
let b = 1;
|
|
162
192
|
for (let i = 0; i < list.length; i++) {
|
|
163
193
|
const hunk = list[i];
|
|
164
|
-
|
|
194
|
+
cut(a, b, hunk.originalStart - a, i > 0, true);
|
|
165
195
|
a = hunk.originalStart + hunk.originalCount;
|
|
166
196
|
b = hunk.modifiedStart + hunk.modifiedCount;
|
|
167
197
|
}
|
|
168
|
-
|
|
198
|
+
cut(a, b, total - a + 1, list.length > 0, false);
|
|
169
199
|
return out;
|
|
170
200
|
});
|
|
171
201
|
const hiddenCount = computed(() => unchanged.value.reduce((sum, run) => sum + run.hideCount, 0));
|
|
@@ -255,21 +285,20 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
255
285
|
}],
|
|
256
286
|
widgets() {
|
|
257
287
|
const out = [];
|
|
288
|
+
const fills = splitPlan.value[side].fills;
|
|
258
289
|
hunks.value.forEach((hunk, i) => {
|
|
259
290
|
const missing = otherCountOf(hunk) - countOf(hunk);
|
|
260
291
|
if (missing <= 0) return;
|
|
261
292
|
const last = startOf(hunk) - 1 + countOf(hunk);
|
|
293
|
+
const blanks = fills.get(i) ?? [];
|
|
262
294
|
out.push({
|
|
263
|
-
id: `fill-${i}`,
|
|
295
|
+
id: `fill-${i}-${missing}-${blanks.map((blank) => blank.height).join(".")}`,
|
|
264
296
|
...last >= 1 ? { line: last } : {
|
|
265
297
|
line: 1,
|
|
266
298
|
position: "above"
|
|
267
299
|
},
|
|
268
|
-
height: missing *
|
|
269
|
-
render: () =>
|
|
270
|
-
class: ns.e("filler"),
|
|
271
|
-
style: { height: `calc(${missing} * var(--dou-code-editor-line-height))` }
|
|
272
|
-
})
|
|
300
|
+
height: missing * LINE + blanks.reduce((sum, blank) => sum + blank.height, 0),
|
|
301
|
+
render: () => filler(missing, blanks)
|
|
273
302
|
});
|
|
274
303
|
});
|
|
275
304
|
const total = side === "a" ? originalLines.value.length : modifiedLines.value.length;
|
|
@@ -303,16 +332,45 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
303
332
|
}
|
|
304
333
|
});
|
|
305
334
|
}
|
|
335
|
+
/** Rows of filler, or rows split by the blanks of widgets on the other side. */
|
|
336
|
+
const block = (count) => h("div", {
|
|
337
|
+
class: ns.e("filler"),
|
|
338
|
+
style: { height: `calc(${count} * var(--dou-code-editor-line-height))` }
|
|
339
|
+
});
|
|
340
|
+
function filler(rows, blanks) {
|
|
341
|
+
if (!blanks.length) return block(rows);
|
|
342
|
+
const children = [];
|
|
343
|
+
let at = 0;
|
|
344
|
+
for (const blank of [...blanks].sort((x, y) => x.offset - y.offset || +x.above - +y.above)) {
|
|
345
|
+
if (blank.offset > at) children.push(block(blank.offset - at));
|
|
346
|
+
children.push(spacer(blank.height, blank.span)());
|
|
347
|
+
at = Math.max(at, blank.offset);
|
|
348
|
+
}
|
|
349
|
+
if (rows > at) children.push(block(rows - at));
|
|
350
|
+
return h("div", children);
|
|
351
|
+
}
|
|
306
352
|
const modifiedOwn = sideExtension("b");
|
|
307
|
-
const modifiedExtensions = computed(() => [
|
|
353
|
+
const modifiedExtensions = computed(() => [
|
|
354
|
+
annotationsB,
|
|
355
|
+
modifiedOwn,
|
|
356
|
+
annotationsEndB,
|
|
357
|
+
...props.extensions ?? []
|
|
358
|
+
]);
|
|
308
359
|
const editorA = ref();
|
|
309
360
|
const editorB = ref();
|
|
310
361
|
const sideA = ref();
|
|
311
362
|
const sideB = ref();
|
|
312
|
-
const extensionsA = [
|
|
363
|
+
const extensionsA = [
|
|
364
|
+
annotations("a"),
|
|
365
|
+
sideExtension("a"),
|
|
366
|
+
annotations("a", true)
|
|
367
|
+
];
|
|
368
|
+
const annotationsB = annotations("b");
|
|
369
|
+
const annotationsEndB = annotations("b", true);
|
|
313
370
|
const link = createScrollLink();
|
|
314
371
|
function onScroll(event) {
|
|
315
372
|
if (view.value !== "split") return;
|
|
373
|
+
if (spans.value.length) schedulePlace();
|
|
316
374
|
const from = event.target.closest(`.${ns.e("side")}`);
|
|
317
375
|
const source = from?.querySelector(".dou-code-editor__scroller");
|
|
318
376
|
if (!source || source !== event.target) return;
|
|
@@ -330,12 +388,16 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
330
388
|
const hunkLines = [];
|
|
331
389
|
const runLines = /* @__PURE__ */ new Map();
|
|
332
390
|
const marks = [];
|
|
391
|
+
const aRows = new Int32Array(a.length + 1);
|
|
392
|
+
const bRows = new Int32Array(b.length + 1);
|
|
333
393
|
let pos = 0;
|
|
334
394
|
const push = (text, kind, an, bn) => {
|
|
335
395
|
lines.push(text);
|
|
336
396
|
kinds.push(kind);
|
|
337
397
|
aNums.push(an);
|
|
338
398
|
bNums.push(bn);
|
|
399
|
+
if (an) aRows[an] = lines.length;
|
|
400
|
+
if (bn) bRows[bn] = lines.length;
|
|
339
401
|
pos += text.length + 1;
|
|
340
402
|
};
|
|
341
403
|
const runs = unchanged.value;
|
|
@@ -377,6 +439,8 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
377
439
|
kinds: Uint8Array.from(kinds),
|
|
378
440
|
aNums: Int32Array.from(aNums),
|
|
379
441
|
bNums: Int32Array.from(bNums),
|
|
442
|
+
aRows,
|
|
443
|
+
bRows,
|
|
380
444
|
hunkLines,
|
|
381
445
|
runLines,
|
|
382
446
|
marks
|
|
@@ -402,7 +466,7 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
402
466
|
}
|
|
403
467
|
return out;
|
|
404
468
|
};
|
|
405
|
-
const
|
|
469
|
+
const inlineExtension = defineCodeExtension({
|
|
406
470
|
name: "diff",
|
|
407
471
|
decorations({ visible }) {
|
|
408
472
|
const u = unified.value;
|
|
@@ -503,9 +567,328 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
503
567
|
}
|
|
504
568
|
return out;
|
|
505
569
|
}
|
|
506
|
-
})
|
|
570
|
+
});
|
|
571
|
+
const inlineExtensions = [
|
|
572
|
+
inlineAnnotations(),
|
|
573
|
+
inlineExtension,
|
|
574
|
+
inlineAnnotations(true)
|
|
575
|
+
];
|
|
507
576
|
const editorInline = ref();
|
|
508
577
|
const inlineBox = ref();
|
|
578
|
+
/** One line's height, before the editors have measured theirs. */
|
|
579
|
+
const sideName = (side) => side === "a" ? "original" : "modified";
|
|
580
|
+
/** The widgets' measured heights, by id, for the blank left beside each. */
|
|
581
|
+
const heights = reactive(/* @__PURE__ */ new Map());
|
|
582
|
+
let heightObserver;
|
|
583
|
+
/** The element each widget's content is drawn in now, by id, and back. */
|
|
584
|
+
const measuredEls = /* @__PURE__ */ new Map();
|
|
585
|
+
const measuredIds = /* @__PURE__ */ new Map();
|
|
586
|
+
function readHeight(box, entry) {
|
|
587
|
+
const id = measuredIds.get(box);
|
|
588
|
+
const size = entry?.borderBoxSize?.[0]?.blockSize ?? entry?.contentRect.height;
|
|
589
|
+
const height = size === void 0 ? box.offsetHeight : Math.round(size);
|
|
590
|
+
if (id && height && heights.get(id) !== height) heights.set(id, height);
|
|
591
|
+
}
|
|
592
|
+
function forget(box) {
|
|
593
|
+
heightObserver?.unobserve(box);
|
|
594
|
+
measuredIds.delete(box);
|
|
595
|
+
}
|
|
596
|
+
/** Measures a widget across both sides as drawn, now and whenever it resizes; `null` once it is gone. */
|
|
597
|
+
function measure(el, id) {
|
|
598
|
+
const had = measuredEls.get(id);
|
|
599
|
+
if (!el) {
|
|
600
|
+
if (had && !had.isConnected) {
|
|
601
|
+
forget(had);
|
|
602
|
+
measuredEls.delete(id);
|
|
603
|
+
}
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
if (had === el) return;
|
|
607
|
+
if (had) forget(had);
|
|
608
|
+
measuredEls.set(id, el);
|
|
609
|
+
measuredIds.set(el, id);
|
|
610
|
+
readHeight(el);
|
|
611
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
612
|
+
heightObserver ??= new ResizeObserver((entries) => {
|
|
613
|
+
for (const entry of entries) readHeight(entry.target, entry);
|
|
614
|
+
});
|
|
615
|
+
heightObserver.observe(el);
|
|
616
|
+
}
|
|
617
|
+
onBeforeUnmount(() => heightObserver?.disconnect());
|
|
618
|
+
const heightOf = (widget) => heights.get(widget.id) ?? widget.height ?? LINE;
|
|
619
|
+
const widgetIds = computed(() => new Set((props.widgets ?? []).map((widget) => widget.id)));
|
|
620
|
+
provide(WIDGET_HEIGHT_KEY, (key, height) => {
|
|
621
|
+
if (!key.startsWith("diff-widgets")) return;
|
|
622
|
+
const id = key.slice(key.indexOf("/") + 1);
|
|
623
|
+
if (widgetIds.value.has(id) && heights.get(id) !== height) heights.set(id, height);
|
|
624
|
+
});
|
|
625
|
+
/** A widget's own content. */
|
|
626
|
+
const content = (widget) => () => h("div", { class: ns.e("widget") }, [widget.render()]);
|
|
627
|
+
/** The blank across from a widget; `span` marks where a widget across both sides is drawn. */
|
|
628
|
+
const spacer = (height, span) => () => h("div", {
|
|
629
|
+
class: ns.e("spacer"),
|
|
630
|
+
style: { height: `${height}px` },
|
|
631
|
+
"data-diff-span": span
|
|
632
|
+
});
|
|
633
|
+
/** A blank inside a filler: `offset` rows into it. */
|
|
634
|
+
function across(side, line, above) {
|
|
635
|
+
const list = hunks.value;
|
|
636
|
+
for (let i = 0; i < list.length; i++) {
|
|
637
|
+
const hunk = list[i];
|
|
638
|
+
const start = side === "a" ? hunk.originalStart : hunk.modifiedStart;
|
|
639
|
+
const count = side === "a" ? hunk.originalCount : hunk.modifiedCount;
|
|
640
|
+
if (line < start) break;
|
|
641
|
+
if (line >= start + count) continue;
|
|
642
|
+
const otherStart = side === "a" ? hunk.modifiedStart : hunk.originalStart;
|
|
643
|
+
const otherCount = side === "a" ? hunk.modifiedCount : hunk.originalCount;
|
|
644
|
+
const index = line - start;
|
|
645
|
+
if (index < otherCount) return { line: otherStart + index };
|
|
646
|
+
return {
|
|
647
|
+
fill: i,
|
|
648
|
+
offset: index - otherCount + (above ? 0 : 1)
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
for (const run of unchanged.value) {
|
|
652
|
+
const start = side === "a" ? run.a : run.b;
|
|
653
|
+
if (line >= start && line < start + run.count) return { line: line - start + (side === "a" ? run.b : run.a) };
|
|
654
|
+
}
|
|
655
|
+
return null;
|
|
656
|
+
}
|
|
657
|
+
/** Side by side: each widget on its side, a blank of its height on the other. */
|
|
658
|
+
if (process.env.NODE_ENV !== "production") {
|
|
659
|
+
const warned = /* @__PURE__ */ new Set();
|
|
660
|
+
watch(() => props.widgets, (widgets) => {
|
|
661
|
+
const seen = /* @__PURE__ */ new Set();
|
|
662
|
+
for (const widget of widgets ?? []) {
|
|
663
|
+
if (seen.has(widget.id) && !warned.has(widget.id)) {
|
|
664
|
+
warned.add(widget.id);
|
|
665
|
+
console.warn(`[@dou-ui/code] DouCodeDiff: widget id "${widget.id}" is used more than once; ids must be unique across both sides.`);
|
|
666
|
+
}
|
|
667
|
+
seen.add(widget.id);
|
|
668
|
+
}
|
|
669
|
+
}, { immediate: true });
|
|
670
|
+
}
|
|
671
|
+
const splitPlan = computed(() => {
|
|
672
|
+
const plan = {
|
|
673
|
+
a: {
|
|
674
|
+
below: [],
|
|
675
|
+
above: [],
|
|
676
|
+
fills: /* @__PURE__ */ new Map()
|
|
677
|
+
},
|
|
678
|
+
b: {
|
|
679
|
+
below: [],
|
|
680
|
+
above: [],
|
|
681
|
+
fills: /* @__PURE__ */ new Map()
|
|
682
|
+
}
|
|
683
|
+
};
|
|
684
|
+
if (view.value !== "split") return plan;
|
|
685
|
+
for (const widget of props.widgets ?? []) {
|
|
686
|
+
const own = widget.side === "original" ? "a" : "b";
|
|
687
|
+
const other = own === "a" ? "b" : "a";
|
|
688
|
+
const lines = own === "a" ? originalLines.value.length : modifiedLines.value.length;
|
|
689
|
+
if (!(widget.line >= 1 && widget.line <= lines)) continue;
|
|
690
|
+
const above = widget.position === "above";
|
|
691
|
+
const height = heightOf(widget);
|
|
692
|
+
const span = widget.span === "both" ? widget.id : void 0;
|
|
693
|
+
const put = (side, line, render, label, blank = false) => plan[side][above ? "above" : "below"].push({
|
|
694
|
+
id: blank ? `${widget.id}~${height}` : widget.id,
|
|
695
|
+
line,
|
|
696
|
+
position: above ? "above" : "below",
|
|
697
|
+
height,
|
|
698
|
+
label,
|
|
699
|
+
render
|
|
700
|
+
});
|
|
701
|
+
if (span) put(own, widget.line, spacer(height, span), widget.label, true);
|
|
702
|
+
else put(own, widget.line, content(widget), widget.label);
|
|
703
|
+
const spot = across(own, widget.line, above);
|
|
704
|
+
if (!spot) continue;
|
|
705
|
+
if ("fill" in spot) {
|
|
706
|
+
const fills = plan[other].fills;
|
|
707
|
+
const blanks = fills.get(spot.fill) ?? [];
|
|
708
|
+
blanks.push({
|
|
709
|
+
offset: spot.offset,
|
|
710
|
+
above,
|
|
711
|
+
height,
|
|
712
|
+
span
|
|
713
|
+
});
|
|
714
|
+
fills.set(spot.fill, blanks);
|
|
715
|
+
} else put(other, spot.line, spacer(height, span), void 0, true);
|
|
716
|
+
}
|
|
717
|
+
return plan;
|
|
718
|
+
});
|
|
719
|
+
/** The widgets drawn across both sides, over them. */
|
|
720
|
+
const spans = computed(() => view.value === "split" ? (props.widgets ?? []).filter((widget) => widget.span === "both") : []);
|
|
721
|
+
/** A column of `gutters` as one side's editor draws it. */
|
|
722
|
+
function sideGutter(gutter, side) {
|
|
723
|
+
const name = sideName(side);
|
|
724
|
+
return {
|
|
725
|
+
name: gutter.name,
|
|
726
|
+
width: gutter.width,
|
|
727
|
+
markers: ({ visible }) => gutter.markers.filter((marker) => marker.side === name && marker.line >= visible.from && marker.line <= visible.to),
|
|
728
|
+
onPress: gutter.onPress && ((line, event) => gutter.onPress(line, event, name))
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* The `gutters` and `widgets` of one side. Two of them go around the
|
|
733
|
+
* diff's own: widgets below a line come before its filler or bar, those
|
|
734
|
+
* above a line after them.
|
|
735
|
+
*/
|
|
736
|
+
function annotations(side, end = false) {
|
|
737
|
+
return defineCodeExtension({
|
|
738
|
+
name: end ? "diff-widgets-end" : "diff-widgets",
|
|
739
|
+
get gutters() {
|
|
740
|
+
return end ? [] : (props.gutters ?? []).map((gutter) => sideGutter(gutter, side));
|
|
741
|
+
},
|
|
742
|
+
widgets: () => splitPlan.value[side][end ? "above" : "below"]
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
/** Where line `line` of `side` is inline, from 1; 0 when it isn't there. */
|
|
746
|
+
function inlineRow(u, side, line) {
|
|
747
|
+
const rows = side === "original" ? u.aRows : u.bRows;
|
|
748
|
+
return line >= 1 && line < rows.length ? rows[line] : 0;
|
|
749
|
+
}
|
|
750
|
+
/** The `gutters` and `widgets` inline, on the rows of their lines. */
|
|
751
|
+
function inlineAnnotations(end = false) {
|
|
752
|
+
return defineCodeExtension({
|
|
753
|
+
name: end ? "diff-widgets-end" : "diff-widgets",
|
|
754
|
+
get gutters() {
|
|
755
|
+
if (end) return [];
|
|
756
|
+
return (props.gutters ?? []).map((gutter) => ({
|
|
757
|
+
name: gutter.name,
|
|
758
|
+
width: gutter.width,
|
|
759
|
+
markers: ({ visible }) => {
|
|
760
|
+
const u = unified.value;
|
|
761
|
+
if (!u) return [];
|
|
762
|
+
const out = [];
|
|
763
|
+
for (const side of ["original", "modified"]) for (const marker of gutter.markers) {
|
|
764
|
+
if (marker.side !== side) continue;
|
|
765
|
+
const row = inlineRow(u, side, marker.line);
|
|
766
|
+
if (row && row >= visible.from && row <= visible.to) out.push({
|
|
767
|
+
...marker,
|
|
768
|
+
line: row
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
return out;
|
|
772
|
+
},
|
|
773
|
+
onPress: gutter.onPress && ((row, event) => {
|
|
774
|
+
const u = unified.value;
|
|
775
|
+
if (!u) return;
|
|
776
|
+
const a = u.aNums[row - 1] ?? 0;
|
|
777
|
+
const b = u.bNums[row - 1] ?? 0;
|
|
778
|
+
const has = (side, line) => line > 0 && gutter.markers.some((m) => m.side === side && m.line === line);
|
|
779
|
+
if (has("modified", b) || b && !has("original", a)) gutter.onPress(b, event, "modified");
|
|
780
|
+
else gutter.onPress(a, event, "original");
|
|
781
|
+
})
|
|
782
|
+
}));
|
|
783
|
+
},
|
|
784
|
+
widgets() {
|
|
785
|
+
const u = unified.value;
|
|
786
|
+
if (!u) return [];
|
|
787
|
+
const out = [];
|
|
788
|
+
for (const widget of props.widgets ?? []) {
|
|
789
|
+
if (widget.position === "above" !== end) continue;
|
|
790
|
+
const row = inlineRow(u, widget.side, widget.line);
|
|
791
|
+
if (!row) continue;
|
|
792
|
+
out.push({
|
|
793
|
+
id: widget.id,
|
|
794
|
+
line: row,
|
|
795
|
+
position: widget.position,
|
|
796
|
+
height: heightOf(widget),
|
|
797
|
+
label: widget.label,
|
|
798
|
+
render: content(widget)
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
return out;
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
const spanLayer = ref();
|
|
806
|
+
/** Where the layer of widgets across both sides is: over the text of the sides. */
|
|
807
|
+
const spanBox = ref({
|
|
808
|
+
top: 0,
|
|
809
|
+
height: 0
|
|
810
|
+
});
|
|
811
|
+
/** Each widget's y in the layer, by id; left out while its blank isn't drawn. */
|
|
812
|
+
const spanTops = ref({});
|
|
813
|
+
let placeFrame = 0;
|
|
814
|
+
function schedulePlace() {
|
|
815
|
+
if (!placeFrame && typeof requestAnimationFrame !== "undefined") placeFrame = requestAnimationFrame(placeSpans);
|
|
816
|
+
}
|
|
817
|
+
/** Puts each widget across both sides over its blank. */
|
|
818
|
+
function placeSpans() {
|
|
819
|
+
placeFrame = 0;
|
|
820
|
+
const body = spanLayer.value?.parentElement;
|
|
821
|
+
const scroller = sideA.value?.querySelector(".dou-code-editor__scroller");
|
|
822
|
+
if (!body || !scroller) return;
|
|
823
|
+
const frame = scroller.getBoundingClientRect();
|
|
824
|
+
const top = frame.top - body.getBoundingClientRect().top;
|
|
825
|
+
if (spanBox.value.top !== top || spanBox.value.height !== frame.height) spanBox.value = {
|
|
826
|
+
top,
|
|
827
|
+
height: frame.height
|
|
828
|
+
};
|
|
829
|
+
const tops = {};
|
|
830
|
+
for (const box of [sideA.value, sideB.value]) for (const el of box?.querySelectorAll("[data-diff-span]") ?? []) {
|
|
831
|
+
const id = el.dataset.diffSpan;
|
|
832
|
+
if (!(id in tops)) tops[id] = el.getBoundingClientRect().top - frame.top;
|
|
833
|
+
}
|
|
834
|
+
const had = spanTops.value;
|
|
835
|
+
const keys = Object.keys(tops);
|
|
836
|
+
if (keys.length !== Object.keys(had).length || keys.some((id) => had[id] !== tops[id])) spanTops.value = tops;
|
|
837
|
+
}
|
|
838
|
+
let stopPlacing;
|
|
839
|
+
function startPlacing() {
|
|
840
|
+
stopPlacing?.();
|
|
841
|
+
stopPlacing = void 0;
|
|
842
|
+
if (!spans.value.length || !sideA.value) return;
|
|
843
|
+
placeSpans();
|
|
844
|
+
const moved = typeof MutationObserver === "undefined" ? null : new MutationObserver(schedulePlace);
|
|
845
|
+
for (const side of [sideA.value, sideB.value]) if (side) moved?.observe(side, {
|
|
846
|
+
subtree: true,
|
|
847
|
+
childList: true,
|
|
848
|
+
attributeFilter: ["style"]
|
|
849
|
+
});
|
|
850
|
+
const resized = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(schedulePlace);
|
|
851
|
+
if (root.value) resized?.observe(root.value);
|
|
852
|
+
stopPlacing = () => {
|
|
853
|
+
moved?.disconnect();
|
|
854
|
+
resized?.disconnect();
|
|
855
|
+
if (placeFrame) cancelAnimationFrame(placeFrame);
|
|
856
|
+
placeFrame = 0;
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
watch(() => spans.value.length > 0 && view.value, startPlacing, { flush: "post" });
|
|
860
|
+
onMounted(startPlacing);
|
|
861
|
+
onBeforeUnmount(() => stopPlacing?.());
|
|
862
|
+
/**
|
|
863
|
+
* While the text scrolls, the widgets across both sides are hidden: placed a
|
|
864
|
+
* frame late, they would trail behind it. They come back once it rests.
|
|
865
|
+
*/
|
|
866
|
+
const spansScrolling = ref(false);
|
|
867
|
+
let restTimer;
|
|
868
|
+
function onSideScroll() {
|
|
869
|
+
if (!spans.value.length) return;
|
|
870
|
+
spansScrolling.value = true;
|
|
871
|
+
clearTimeout(restTimer);
|
|
872
|
+
restTimer = setTimeout(() => {
|
|
873
|
+
spansScrolling.value = false;
|
|
874
|
+
placeSpans();
|
|
875
|
+
}, 120);
|
|
876
|
+
}
|
|
877
|
+
onBeforeUnmount(() => clearTimeout(restTimer));
|
|
878
|
+
/** A wheel over a widget across both sides scrolls the text under it. */
|
|
879
|
+
function onSpanWheel(event) {
|
|
880
|
+
const scroller = sideB.value?.querySelector(".dou-code-editor__scroller");
|
|
881
|
+
if (!scroller) return;
|
|
882
|
+
const unit = event.deltaMode === 1 ? LINE : event.deltaMode === 2 ? scroller.clientHeight : 1;
|
|
883
|
+
const dy = event.deltaY * unit;
|
|
884
|
+
const dx = event.deltaX * unit;
|
|
885
|
+
const canY = dy < 0 ? scroller.scrollTop > 0 : dy > 0 && scroller.scrollTop + scroller.clientHeight < scroller.scrollHeight - 1;
|
|
886
|
+
const canX = dx < 0 ? scroller.scrollLeft > 0 : dx > 0 && scroller.scrollLeft + scroller.clientWidth < scroller.scrollWidth - 1;
|
|
887
|
+
if (!canY && !canX) return;
|
|
888
|
+
event.preventDefault();
|
|
889
|
+
if (canY) scroller.scrollTop += dy;
|
|
890
|
+
if (canX) scroller.scrollLeft += dx;
|
|
891
|
+
}
|
|
509
892
|
/** Shows the run of hidden lines a cursor moved into, e.g. with search. */
|
|
510
893
|
function revealAt(side, result) {
|
|
511
894
|
if (!hiddenCount.value) return;
|
|
@@ -708,6 +1091,7 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
708
1091
|
createElementVNode("div", {
|
|
709
1092
|
ref_key: "sideA",
|
|
710
1093
|
ref: sideA,
|
|
1094
|
+
onScrollCapturePassive: onSideScroll,
|
|
711
1095
|
class: normalizeClass([unref(ns).e("side"), "is-original"])
|
|
712
1096
|
}, [createVNode(code_editor_default, {
|
|
713
1097
|
ref_key: "editorA",
|
|
@@ -730,10 +1114,11 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
730
1114
|
"tab-size",
|
|
731
1115
|
"messages",
|
|
732
1116
|
"aria-label"
|
|
733
|
-
])],
|
|
1117
|
+
])], 34),
|
|
734
1118
|
createElementVNode("div", {
|
|
735
1119
|
ref_key: "sideB",
|
|
736
1120
|
ref: sideB,
|
|
1121
|
+
onScrollCapturePassive: onSideScroll,
|
|
737
1122
|
class: normalizeClass([unref(ns).e("side"), "is-modified"])
|
|
738
1123
|
}, [createVNode(code_editor_default, {
|
|
739
1124
|
ref_key: "editorB",
|
|
@@ -759,7 +1144,28 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
759
1144
|
"messages",
|
|
760
1145
|
"aria-label",
|
|
761
1146
|
"readonly"
|
|
762
|
-
])],
|
|
1147
|
+
])], 34),
|
|
1148
|
+
spans.value.length ? (openBlock(), createElementBlock("div", {
|
|
1149
|
+
key: 0,
|
|
1150
|
+
ref_key: "spanLayer",
|
|
1151
|
+
ref: spanLayer,
|
|
1152
|
+
class: normalizeClass(unref(ns).e("spans")),
|
|
1153
|
+
style: normalizeStyle({
|
|
1154
|
+
top: `${spanBox.value.top}px`,
|
|
1155
|
+
height: `${spanBox.value.height}px`
|
|
1156
|
+
})
|
|
1157
|
+
}, [(openBlock(true), createElementBlock(Fragment, null, renderList(spans.value, (widget) => {
|
|
1158
|
+
return openBlock(), createElementBlock("div", {
|
|
1159
|
+
key: widget.id,
|
|
1160
|
+
ref_for: true,
|
|
1161
|
+
ref: (el) => measure(el, widget.id),
|
|
1162
|
+
class: normalizeClass([unref(ns).e("span"), unref(ns).is("placed", widget.id in spanTops.value && !spansScrolling.value)]),
|
|
1163
|
+
style: normalizeStyle({ transform: `translateY(${spanTops.value[widget.id] ?? 0}px)` }),
|
|
1164
|
+
role: widget.label ? "group" : void 0,
|
|
1165
|
+
"aria-label": widget.label,
|
|
1166
|
+
onWheel: onSpanWheel
|
|
1167
|
+
}, [createVNode(unref(render_part_default), { render: widget.render }, null, 8, ["render"])], 46, _hoisted_2);
|
|
1168
|
+
}), 128))], 6)) : createCommentVNode("", true)
|
|
763
1169
|
], 34)) : (openBlock(), createElementBlock("div", {
|
|
764
1170
|
key: 2,
|
|
765
1171
|
ref_key: "inlineBox",
|