@dou-ui/code 0.1.0-alpha.2 → 0.1.0-alpha.21
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 +527 -37
- 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 +176 -54
- 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 +5 -2
- package/dist/types/code-editor.vue.d.ts +1 -0
- package/dist/types/extension.d.ts +44 -4
- package/dist/types/index.d.ts +7 -3
- package/dist/types/types.d.ts +89 -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,11 +58,17 @@ 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,
|
|
57
66
|
default: true
|
|
58
67
|
},
|
|
68
|
+
bordered: {
|
|
69
|
+
type: Boolean,
|
|
70
|
+
default: true
|
|
71
|
+
},
|
|
59
72
|
messages: { default: void 0 }
|
|
60
73
|
},
|
|
61
74
|
emits: ["update:modelValue", "update:mode"],
|
|
@@ -127,6 +140,20 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
127
140
|
/** The runs of unchanged lines opened with their bar, by key; `true` for all of them. */
|
|
128
141
|
const opened = reactive(/* @__PURE__ */ new Set());
|
|
129
142
|
const allOpen = ref(false);
|
|
143
|
+
/**
|
|
144
|
+
* The lines with a marker or widget on each side, which collapsing leaves in
|
|
145
|
+
* view. A marker shown only on hover is out of sight, so it doesn't count.
|
|
146
|
+
*/
|
|
147
|
+
const annotatedLines = computed(() => {
|
|
148
|
+
const out = {
|
|
149
|
+
a: /* @__PURE__ */ new Set(),
|
|
150
|
+
b: /* @__PURE__ */ new Set()
|
|
151
|
+
};
|
|
152
|
+
const add = (side, line) => out[side === "original" ? "a" : "b"].add(line);
|
|
153
|
+
for (const gutter of props.gutters ?? []) for (const marker of gutter.markers ?? []) if (marker.visible !== "hover") add(marker.side, marker.line);
|
|
154
|
+
for (const widget of props.widgets ?? []) add(widget.side, widget.line);
|
|
155
|
+
return out;
|
|
156
|
+
});
|
|
130
157
|
const unchanged = computed(() => {
|
|
131
158
|
const list = hunks.value;
|
|
132
159
|
const total = originalLines.value.length;
|
|
@@ -153,15 +180,25 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
153
180
|
key: a
|
|
154
181
|
});
|
|
155
182
|
};
|
|
183
|
+
const kept = annotatedLines.value;
|
|
184
|
+
const cut = (a, b, count, before, after) => {
|
|
185
|
+
let from = 0;
|
|
186
|
+
for (let i = 0; i < count; i++) {
|
|
187
|
+
if (!kept.a.has(a + i) && !kept.b.has(b + i)) continue;
|
|
188
|
+
if (i > from) push(a + from, b + from, i - from, from > 0 || before, true);
|
|
189
|
+
from = Math.max(from, i);
|
|
190
|
+
}
|
|
191
|
+
push(a + from, b + from, count - from, from > 0 || before, after);
|
|
192
|
+
};
|
|
156
193
|
let a = 1;
|
|
157
194
|
let b = 1;
|
|
158
195
|
for (let i = 0; i < list.length; i++) {
|
|
159
196
|
const hunk = list[i];
|
|
160
|
-
|
|
197
|
+
cut(a, b, hunk.originalStart - a, i > 0, true);
|
|
161
198
|
a = hunk.originalStart + hunk.originalCount;
|
|
162
199
|
b = hunk.modifiedStart + hunk.modifiedCount;
|
|
163
200
|
}
|
|
164
|
-
|
|
201
|
+
cut(a, b, total - a + 1, list.length > 0, false);
|
|
165
202
|
return out;
|
|
166
203
|
});
|
|
167
204
|
const hiddenCount = computed(() => unchanged.value.reduce((sum, run) => sum + run.hideCount, 0));
|
|
@@ -229,43 +266,22 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
229
266
|
}
|
|
230
267
|
return out;
|
|
231
268
|
},
|
|
232
|
-
gutters: [{
|
|
233
|
-
name: "diff",
|
|
234
|
-
width: 16,
|
|
235
|
-
markers({ visible }) {
|
|
236
|
-
const out = [];
|
|
237
|
-
const title = side === "a" ? messages.value.diffRemoved : messages.value.diffAdded;
|
|
238
|
-
for (const hunk of hunks.value) {
|
|
239
|
-
const start = startOf(hunk);
|
|
240
|
-
const count = countOf(hunk);
|
|
241
|
-
if (start > visible.to) break;
|
|
242
|
-
for (let n = Math.max(start, visible.from); n < start + count && n <= visible.to; n++) out.push({
|
|
243
|
-
line: n,
|
|
244
|
-
text: side === "a" ? "−" : "+",
|
|
245
|
-
class: `${ns.e("sign")} is-${tone}`,
|
|
246
|
-
title
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
return out;
|
|
250
|
-
}
|
|
251
|
-
}],
|
|
252
269
|
widgets() {
|
|
253
270
|
const out = [];
|
|
271
|
+
const fills = splitPlan.value[side].fills;
|
|
254
272
|
hunks.value.forEach((hunk, i) => {
|
|
255
273
|
const missing = otherCountOf(hunk) - countOf(hunk);
|
|
256
274
|
if (missing <= 0) return;
|
|
257
275
|
const last = startOf(hunk) - 1 + countOf(hunk);
|
|
276
|
+
const blanks = fills.get(i) ?? [];
|
|
258
277
|
out.push({
|
|
259
|
-
id: `fill-${i}`,
|
|
278
|
+
id: `fill-${i}-${missing}-${blanks.map((blank) => blank.height).join(".")}`,
|
|
260
279
|
...last >= 1 ? { line: last } : {
|
|
261
280
|
line: 1,
|
|
262
281
|
position: "above"
|
|
263
282
|
},
|
|
264
|
-
height: missing *
|
|
265
|
-
render: () =>
|
|
266
|
-
class: ns.e("filler"),
|
|
267
|
-
style: { height: `calc(${missing} * var(--dou-code-editor-line-height))` }
|
|
268
|
-
})
|
|
283
|
+
height: missing * LINE + blanks.reduce((sum, blank) => sum + blank.height, 0),
|
|
284
|
+
render: () => filler(missing, blanks)
|
|
269
285
|
});
|
|
270
286
|
});
|
|
271
287
|
const total = side === "a" ? originalLines.value.length : modifiedLines.value.length;
|
|
@@ -299,16 +315,81 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
299
315
|
}
|
|
300
316
|
});
|
|
301
317
|
}
|
|
318
|
+
/** Rows of filler, or rows split by the blanks of widgets on the other side. */
|
|
319
|
+
const block = (count) => h("div", {
|
|
320
|
+
class: ns.e("filler"),
|
|
321
|
+
style: { height: `calc(${count} * var(--dou-code-editor-line-height))` }
|
|
322
|
+
});
|
|
323
|
+
function filler(rows, blanks) {
|
|
324
|
+
if (!blanks.length) return block(rows);
|
|
325
|
+
const children = [];
|
|
326
|
+
let at = 0;
|
|
327
|
+
for (const blank of [...blanks].sort((x, y) => x.offset - y.offset || +x.above - +y.above)) {
|
|
328
|
+
if (blank.offset > at) children.push(block(blank.offset - at));
|
|
329
|
+
children.push(spacer(blank.height, blank.span)());
|
|
330
|
+
at = Math.max(at, blank.offset);
|
|
331
|
+
}
|
|
332
|
+
if (rows > at) children.push(block(rows - at));
|
|
333
|
+
return h("div", children);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* The `+`/`−` column of one side. It goes after every other extension, so it
|
|
337
|
+
* is always the gutter's last column, next to the text: its tint spreads
|
|
338
|
+
* over the whole gutter row from there (see style.css).
|
|
339
|
+
*/
|
|
340
|
+
function signExtension(side) {
|
|
341
|
+
const startOf = (hunk) => side === "a" ? hunk.originalStart : hunk.modifiedStart;
|
|
342
|
+
const countOf = (hunk) => side === "a" ? hunk.originalCount : hunk.modifiedCount;
|
|
343
|
+
const tone = side === "a" ? "removed" : "added";
|
|
344
|
+
return defineCodeExtension({
|
|
345
|
+
name: "diff-sign",
|
|
346
|
+
gutters: [{
|
|
347
|
+
name: "diff",
|
|
348
|
+
width: 16,
|
|
349
|
+
markers({ visible }) {
|
|
350
|
+
const out = [];
|
|
351
|
+
const title = side === "a" ? messages.value.diffRemoved : messages.value.diffAdded;
|
|
352
|
+
for (const hunk of hunks.value) {
|
|
353
|
+
const start = startOf(hunk);
|
|
354
|
+
const count = countOf(hunk);
|
|
355
|
+
if (start > visible.to) break;
|
|
356
|
+
for (let n = Math.max(start, visible.from); n < start + count && n <= visible.to; n++) out.push({
|
|
357
|
+
line: n,
|
|
358
|
+
text: side === "a" ? "−" : "+",
|
|
359
|
+
class: `${ns.e("sign")} is-${tone}`,
|
|
360
|
+
title
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
return out;
|
|
364
|
+
}
|
|
365
|
+
}]
|
|
366
|
+
});
|
|
367
|
+
}
|
|
302
368
|
const modifiedOwn = sideExtension("b");
|
|
303
|
-
const
|
|
369
|
+
const signB = signExtension("b");
|
|
370
|
+
const modifiedExtensions = computed(() => [
|
|
371
|
+
annotationsB,
|
|
372
|
+
modifiedOwn,
|
|
373
|
+
annotationsEndB,
|
|
374
|
+
...props.extensions ?? [],
|
|
375
|
+
signB
|
|
376
|
+
]);
|
|
304
377
|
const editorA = ref();
|
|
305
378
|
const editorB = ref();
|
|
306
379
|
const sideA = ref();
|
|
307
380
|
const sideB = ref();
|
|
308
|
-
const extensionsA = [
|
|
381
|
+
const extensionsA = [
|
|
382
|
+
annotations("a"),
|
|
383
|
+
sideExtension("a"),
|
|
384
|
+
annotations("a", true),
|
|
385
|
+
signExtension("a")
|
|
386
|
+
];
|
|
387
|
+
const annotationsB = annotations("b");
|
|
388
|
+
const annotationsEndB = annotations("b", true);
|
|
309
389
|
const link = createScrollLink();
|
|
310
390
|
function onScroll(event) {
|
|
311
391
|
if (view.value !== "split") return;
|
|
392
|
+
if (spans.value.length) schedulePlace();
|
|
312
393
|
const from = event.target.closest(`.${ns.e("side")}`);
|
|
313
394
|
const source = from?.querySelector(".dou-code-editor__scroller");
|
|
314
395
|
if (!source || source !== event.target) return;
|
|
@@ -326,12 +407,16 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
326
407
|
const hunkLines = [];
|
|
327
408
|
const runLines = /* @__PURE__ */ new Map();
|
|
328
409
|
const marks = [];
|
|
410
|
+
const aRows = new Int32Array(a.length + 1);
|
|
411
|
+
const bRows = new Int32Array(b.length + 1);
|
|
329
412
|
let pos = 0;
|
|
330
413
|
const push = (text, kind, an, bn) => {
|
|
331
414
|
lines.push(text);
|
|
332
415
|
kinds.push(kind);
|
|
333
416
|
aNums.push(an);
|
|
334
417
|
bNums.push(bn);
|
|
418
|
+
if (an) aRows[an] = lines.length;
|
|
419
|
+
if (bn) bRows[bn] = lines.length;
|
|
335
420
|
pos += text.length + 1;
|
|
336
421
|
};
|
|
337
422
|
const runs = unchanged.value;
|
|
@@ -373,6 +458,8 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
373
458
|
kinds: Uint8Array.from(kinds),
|
|
374
459
|
aNums: Int32Array.from(aNums),
|
|
375
460
|
bNums: Int32Array.from(bNums),
|
|
461
|
+
aRows,
|
|
462
|
+
bRows,
|
|
376
463
|
hunkLines,
|
|
377
464
|
runLines,
|
|
378
465
|
marks
|
|
@@ -398,7 +485,7 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
398
485
|
}
|
|
399
486
|
return out;
|
|
400
487
|
};
|
|
401
|
-
const
|
|
488
|
+
const inlineExtension = defineCodeExtension({
|
|
402
489
|
name: "diff",
|
|
403
490
|
decorations({ visible }) {
|
|
404
491
|
const u = unified.value;
|
|
@@ -499,9 +586,382 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
499
586
|
}
|
|
500
587
|
return out;
|
|
501
588
|
}
|
|
502
|
-
})
|
|
589
|
+
});
|
|
590
|
+
const inlineExtensions = [
|
|
591
|
+
inlineAnnotations(),
|
|
592
|
+
inlineExtension,
|
|
593
|
+
inlineAnnotations(true)
|
|
594
|
+
];
|
|
503
595
|
const editorInline = ref();
|
|
504
596
|
const inlineBox = ref();
|
|
597
|
+
/** One line's height, before the editors have measured theirs. */
|
|
598
|
+
const sideName = (side) => side === "a" ? "original" : "modified";
|
|
599
|
+
/** The widgets' measured heights, by id, for the blank left beside each. */
|
|
600
|
+
const heights = reactive(/* @__PURE__ */ new Map());
|
|
601
|
+
let heightObserver;
|
|
602
|
+
/** The element each widget's content is drawn in now, by id, and back. */
|
|
603
|
+
const measuredEls = /* @__PURE__ */ new Map();
|
|
604
|
+
const measuredIds = /* @__PURE__ */ new Map();
|
|
605
|
+
function readHeight(box, entry) {
|
|
606
|
+
const id = measuredIds.get(box);
|
|
607
|
+
const size = entry?.borderBoxSize?.[0]?.blockSize ?? entry?.contentRect.height;
|
|
608
|
+
const height = size === void 0 ? box.offsetHeight : Math.round(size);
|
|
609
|
+
if (id && height && heights.get(id) !== height) heights.set(id, height);
|
|
610
|
+
}
|
|
611
|
+
function forget(box) {
|
|
612
|
+
heightObserver?.unobserve(box);
|
|
613
|
+
measuredIds.delete(box);
|
|
614
|
+
}
|
|
615
|
+
/** Measures a widget across both sides as drawn, now and whenever it resizes; `null` once it is gone. */
|
|
616
|
+
function measure(el, id) {
|
|
617
|
+
const had = measuredEls.get(id);
|
|
618
|
+
if (!el) {
|
|
619
|
+
if (had && !had.isConnected) {
|
|
620
|
+
forget(had);
|
|
621
|
+
measuredEls.delete(id);
|
|
622
|
+
}
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
if (had === el) return;
|
|
626
|
+
if (had) forget(had);
|
|
627
|
+
measuredEls.set(id, el);
|
|
628
|
+
measuredIds.set(el, id);
|
|
629
|
+
readHeight(el);
|
|
630
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
631
|
+
heightObserver ??= new ResizeObserver((entries) => {
|
|
632
|
+
for (const entry of entries) readHeight(entry.target, entry);
|
|
633
|
+
});
|
|
634
|
+
heightObserver.observe(el);
|
|
635
|
+
}
|
|
636
|
+
onBeforeUnmount(() => heightObserver?.disconnect());
|
|
637
|
+
const heightOf = (widget) => heights.get(widget.id) ?? widget.height ?? LINE;
|
|
638
|
+
const widgetIds = computed(() => new Set((props.widgets ?? []).map((widget) => widget.id)));
|
|
639
|
+
provide(WIDGET_HEIGHT_KEY, (key, height) => {
|
|
640
|
+
if (!key.startsWith("diff-widgets")) return;
|
|
641
|
+
const id = key.slice(key.indexOf("/") + 1);
|
|
642
|
+
if (widgetIds.value.has(id) && heights.get(id) !== height) heights.set(id, height);
|
|
643
|
+
});
|
|
644
|
+
/** A widget's own content. */
|
|
645
|
+
const content = (widget) => () => h("div", { class: ns.e("widget") }, [widget.render()]);
|
|
646
|
+
/** The blank across from a widget; `span` marks where a widget across both sides is drawn. */
|
|
647
|
+
const spacer = (height, span) => () => h("div", {
|
|
648
|
+
class: ns.e("spacer"),
|
|
649
|
+
style: { height: `${height}px` },
|
|
650
|
+
"data-diff-span": span
|
|
651
|
+
});
|
|
652
|
+
/** A blank inside a filler: `offset` rows into it. */
|
|
653
|
+
function across(side, line, above) {
|
|
654
|
+
const list = hunks.value;
|
|
655
|
+
for (let i = 0; i < list.length; i++) {
|
|
656
|
+
const hunk = list[i];
|
|
657
|
+
const start = side === "a" ? hunk.originalStart : hunk.modifiedStart;
|
|
658
|
+
const count = side === "a" ? hunk.originalCount : hunk.modifiedCount;
|
|
659
|
+
if (line < start) break;
|
|
660
|
+
if (line >= start + count) continue;
|
|
661
|
+
const otherStart = side === "a" ? hunk.modifiedStart : hunk.originalStart;
|
|
662
|
+
const otherCount = side === "a" ? hunk.modifiedCount : hunk.originalCount;
|
|
663
|
+
const index = line - start;
|
|
664
|
+
if (index < otherCount) return { line: otherStart + index };
|
|
665
|
+
return {
|
|
666
|
+
fill: i,
|
|
667
|
+
offset: index - otherCount + (above ? 0 : 1)
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
for (const run of unchanged.value) {
|
|
671
|
+
const start = side === "a" ? run.a : run.b;
|
|
672
|
+
if (line >= start && line < start + run.count) return { line: line - start + (side === "a" ? run.b : run.a) };
|
|
673
|
+
}
|
|
674
|
+
return null;
|
|
675
|
+
}
|
|
676
|
+
/** Side by side: each widget on its side, a blank of its height on the other. */
|
|
677
|
+
if (process.env.NODE_ENV !== "production") {
|
|
678
|
+
const warned = /* @__PURE__ */ new Set();
|
|
679
|
+
watch(() => props.widgets, (widgets) => {
|
|
680
|
+
const seen = /* @__PURE__ */ new Set();
|
|
681
|
+
for (const widget of widgets ?? []) {
|
|
682
|
+
if (seen.has(widget.id) && !warned.has(widget.id)) {
|
|
683
|
+
warned.add(widget.id);
|
|
684
|
+
console.warn(`[@dou-ui/code] DouCodeDiff: widget id "${widget.id}" is used more than once; ids must be unique across both sides.`);
|
|
685
|
+
}
|
|
686
|
+
seen.add(widget.id);
|
|
687
|
+
}
|
|
688
|
+
}, { immediate: true });
|
|
689
|
+
}
|
|
690
|
+
const splitPlan = computed(() => {
|
|
691
|
+
const plan = {
|
|
692
|
+
a: {
|
|
693
|
+
below: [],
|
|
694
|
+
above: [],
|
|
695
|
+
fills: /* @__PURE__ */ new Map()
|
|
696
|
+
},
|
|
697
|
+
b: {
|
|
698
|
+
below: [],
|
|
699
|
+
above: [],
|
|
700
|
+
fills: /* @__PURE__ */ new Map()
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
if (view.value !== "split") return plan;
|
|
704
|
+
for (const widget of props.widgets ?? []) {
|
|
705
|
+
const own = widget.side === "original" ? "a" : "b";
|
|
706
|
+
const other = own === "a" ? "b" : "a";
|
|
707
|
+
const lines = own === "a" ? originalLines.value.length : modifiedLines.value.length;
|
|
708
|
+
if (!(widget.line >= 1 && widget.line <= lines)) continue;
|
|
709
|
+
const above = widget.position === "above";
|
|
710
|
+
const height = heightOf(widget);
|
|
711
|
+
const span = widget.span === "both" ? widget.id : void 0;
|
|
712
|
+
const put = (side, line, render, label, blank = false) => plan[side][above ? "above" : "below"].push({
|
|
713
|
+
id: blank ? `${widget.id}~${height}` : widget.id,
|
|
714
|
+
line,
|
|
715
|
+
position: above ? "above" : "below",
|
|
716
|
+
height,
|
|
717
|
+
label,
|
|
718
|
+
render
|
|
719
|
+
});
|
|
720
|
+
if (span) put(own, widget.line, spacer(height, span), widget.label, true);
|
|
721
|
+
else put(own, widget.line, content(widget), widget.label);
|
|
722
|
+
const spot = across(own, widget.line, above);
|
|
723
|
+
if (!spot) continue;
|
|
724
|
+
if ("fill" in spot) {
|
|
725
|
+
const fills = plan[other].fills;
|
|
726
|
+
const blanks = fills.get(spot.fill) ?? [];
|
|
727
|
+
blanks.push({
|
|
728
|
+
offset: spot.offset,
|
|
729
|
+
above,
|
|
730
|
+
height,
|
|
731
|
+
span
|
|
732
|
+
});
|
|
733
|
+
fills.set(spot.fill, blanks);
|
|
734
|
+
} else put(other, spot.line, spacer(height, span), void 0, true);
|
|
735
|
+
}
|
|
736
|
+
return plan;
|
|
737
|
+
});
|
|
738
|
+
/** The widgets drawn across both sides, over them. */
|
|
739
|
+
const spans = computed(() => view.value === "split" ? (props.widgets ?? []).filter((widget) => widget.span === "both") : []);
|
|
740
|
+
/** Whether a column's `hoverMarker` goes on the lines of `side`. */
|
|
741
|
+
function hoversOn(gutter, side) {
|
|
742
|
+
return !!gutter.hoverMarker && (!gutter.hoverMarker.side || gutter.hoverMarker.side === side);
|
|
743
|
+
}
|
|
744
|
+
/** Whether a column has a marker of its own on line `line` of `side`. */
|
|
745
|
+
function ownMarkerOn(gutter, side, line) {
|
|
746
|
+
return line > 0 && (gutter.markers ?? []).some((m) => m.side === side && m.line === line);
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* Which line of which side a press on a column goes to, or `null` for none.
|
|
750
|
+
* A column whose `hoverMarker` is on one side only answers presses on the
|
|
751
|
+
* other side's own markers, not on its empty cells.
|
|
752
|
+
*/
|
|
753
|
+
function pressedOn(gutter, side, line) {
|
|
754
|
+
if (!gutter.hoverMarker || hoversOn(gutter, side) || ownMarkerOn(gutter, side, line)) return side;
|
|
755
|
+
return null;
|
|
756
|
+
}
|
|
757
|
+
/** The column's `hoverMarker` as the editor of one side takes it, named for that side. */
|
|
758
|
+
function hoverMarkerOf(hover, side) {
|
|
759
|
+
const { title } = hover;
|
|
760
|
+
return {
|
|
761
|
+
icon: hover.icon,
|
|
762
|
+
text: hover.text,
|
|
763
|
+
class: hover.class,
|
|
764
|
+
title: typeof title === "function" ? (line) => title(line, side) : title
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
/** A column of `gutters` as one side's editor draws it. */
|
|
768
|
+
function sideGutter(gutter, side) {
|
|
769
|
+
const name = sideName(side);
|
|
770
|
+
return {
|
|
771
|
+
name: gutter.name,
|
|
772
|
+
width: gutter.width,
|
|
773
|
+
markers: ({ visible }) => (gutter.markers ?? []).filter((marker) => marker.side === name && marker.line >= visible.from && marker.line <= visible.to),
|
|
774
|
+
hoverMarker: hoversOn(gutter, name) ? hoverMarkerOf(gutter.hoverMarker, name) : void 0,
|
|
775
|
+
pressOn: gutter.hoverMarker?.side ? "markers" : gutter.pressOn,
|
|
776
|
+
onPress: gutter.onPress && ((line, event) => {
|
|
777
|
+
if (pressedOn(gutter, name, line)) gutter.onPress(line, event, name);
|
|
778
|
+
})
|
|
779
|
+
};
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* The `gutters` and `widgets` of one side. Two of them go around the
|
|
783
|
+
* diff's own: widgets below a line come before its filler or bar, those
|
|
784
|
+
* above a line after them.
|
|
785
|
+
*/
|
|
786
|
+
function annotations(side, end = false) {
|
|
787
|
+
return defineCodeExtension({
|
|
788
|
+
name: end ? "diff-widgets-end" : "diff-widgets",
|
|
789
|
+
get gutters() {
|
|
790
|
+
return end ? [] : (props.gutters ?? []).map((gutter) => sideGutter(gutter, side));
|
|
791
|
+
},
|
|
792
|
+
widgets: () => splitPlan.value[side][end ? "above" : "below"]
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
/** Where line `line` of `side` is inline, from 1; 0 when it isn't there. */
|
|
796
|
+
function inlineRow(u, side, line) {
|
|
797
|
+
const rows = side === "original" ? u.aRows : u.bRows;
|
|
798
|
+
return line >= 1 && line < rows.length ? rows[line] : 0;
|
|
799
|
+
}
|
|
800
|
+
/** The `gutters` and `widgets` inline, on the rows of their lines. */
|
|
801
|
+
function inlineAnnotations(end = false) {
|
|
802
|
+
return defineCodeExtension({
|
|
803
|
+
name: end ? "diff-widgets-end" : "diff-widgets",
|
|
804
|
+
get gutters() {
|
|
805
|
+
if (end) return [];
|
|
806
|
+
return (props.gutters ?? []).map((gutter) => ({
|
|
807
|
+
name: gutter.name,
|
|
808
|
+
width: gutter.width,
|
|
809
|
+
pressOn: gutter.hoverMarker?.side ? "markers" : gutter.pressOn,
|
|
810
|
+
markers: ({ visible }) => {
|
|
811
|
+
const u = unified.value;
|
|
812
|
+
if (!u) return [];
|
|
813
|
+
const out = [];
|
|
814
|
+
const taken = /* @__PURE__ */ new Set();
|
|
815
|
+
for (const side of ["modified", "original"]) for (const marker of gutter.markers ?? []) {
|
|
816
|
+
if (marker.side !== side) continue;
|
|
817
|
+
const row = inlineRow(u, side, marker.line);
|
|
818
|
+
if (!row || row < visible.from || row > visible.to || taken.has(row)) continue;
|
|
819
|
+
out.push({
|
|
820
|
+
...marker,
|
|
821
|
+
line: row
|
|
822
|
+
});
|
|
823
|
+
taken.add(row);
|
|
824
|
+
}
|
|
825
|
+
if (gutter.hoverMarker) for (let row = visible.from; row <= visible.to && row <= u.kinds.length; row++) {
|
|
826
|
+
if (taken.has(row)) continue;
|
|
827
|
+
const b = u.bNums[row - 1] ?? 0;
|
|
828
|
+
const a = u.aNums[row - 1] ?? 0;
|
|
829
|
+
const at = b && hoversOn(gutter, "modified") ? [b, "modified"] : a && hoversOn(gutter, "original") ? [a, "original"] : null;
|
|
830
|
+
if (!at) continue;
|
|
831
|
+
const { icon, text, class: className, title } = gutter.hoverMarker;
|
|
832
|
+
out.push({
|
|
833
|
+
icon,
|
|
834
|
+
text,
|
|
835
|
+
class: className,
|
|
836
|
+
title: typeof title === "function" ? title(at[0], at[1]) : title,
|
|
837
|
+
line: row,
|
|
838
|
+
visible: "hover"
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
return out;
|
|
842
|
+
},
|
|
843
|
+
onPress: gutter.onPress && ((row, event) => {
|
|
844
|
+
const u = unified.value;
|
|
845
|
+
if (!u) return;
|
|
846
|
+
const a = u.aNums[row - 1] ?? 0;
|
|
847
|
+
const b = u.bNums[row - 1] ?? 0;
|
|
848
|
+
const press = (side, line) => gutter.onPress(line, event, side);
|
|
849
|
+
if (ownMarkerOn(gutter, "modified", b)) return press("modified", b);
|
|
850
|
+
if (ownMarkerOn(gutter, "original", a)) return press("original", a);
|
|
851
|
+
if (!gutter.hoverMarker) return b ? press("modified", b) : press("original", a);
|
|
852
|
+
if (b && hoversOn(gutter, "modified")) press("modified", b);
|
|
853
|
+
else if (a && hoversOn(gutter, "original")) press("original", a);
|
|
854
|
+
})
|
|
855
|
+
}));
|
|
856
|
+
},
|
|
857
|
+
widgets() {
|
|
858
|
+
const u = unified.value;
|
|
859
|
+
if (!u) return [];
|
|
860
|
+
const out = [];
|
|
861
|
+
for (const widget of props.widgets ?? []) {
|
|
862
|
+
if (widget.position === "above" !== end) continue;
|
|
863
|
+
const row = inlineRow(u, widget.side, widget.line);
|
|
864
|
+
if (!row) continue;
|
|
865
|
+
out.push({
|
|
866
|
+
id: widget.id,
|
|
867
|
+
line: row,
|
|
868
|
+
position: widget.position,
|
|
869
|
+
height: heightOf(widget),
|
|
870
|
+
label: widget.label,
|
|
871
|
+
render: content(widget)
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
return out;
|
|
875
|
+
}
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
const spanLayer = ref();
|
|
879
|
+
/** Where the layer of widgets across both sides is: over the text of the sides. */
|
|
880
|
+
const spanBox = ref({
|
|
881
|
+
top: 0,
|
|
882
|
+
height: 0
|
|
883
|
+
});
|
|
884
|
+
/** Each widget's y in the layer, by id; left out while its blank isn't drawn. */
|
|
885
|
+
const spanTops = ref({});
|
|
886
|
+
let placeFrame = 0;
|
|
887
|
+
function schedulePlace() {
|
|
888
|
+
if (!placeFrame && typeof requestAnimationFrame !== "undefined") placeFrame = requestAnimationFrame(placeSpans);
|
|
889
|
+
}
|
|
890
|
+
/** Puts each widget across both sides over its blank. */
|
|
891
|
+
function placeSpans() {
|
|
892
|
+
placeFrame = 0;
|
|
893
|
+
const body = spanLayer.value?.parentElement;
|
|
894
|
+
const scroller = sideA.value?.querySelector(".dou-code-editor__scroller");
|
|
895
|
+
if (!body || !scroller) return;
|
|
896
|
+
const frame = scroller.getBoundingClientRect();
|
|
897
|
+
const top = frame.top - body.getBoundingClientRect().top;
|
|
898
|
+
if (spanBox.value.top !== top || spanBox.value.height !== frame.height) spanBox.value = {
|
|
899
|
+
top,
|
|
900
|
+
height: frame.height
|
|
901
|
+
};
|
|
902
|
+
const tops = {};
|
|
903
|
+
for (const box of [sideA.value, sideB.value]) for (const el of box?.querySelectorAll("[data-diff-span]") ?? []) {
|
|
904
|
+
const id = el.dataset.diffSpan;
|
|
905
|
+
if (!(id in tops)) tops[id] = el.getBoundingClientRect().top - frame.top;
|
|
906
|
+
}
|
|
907
|
+
const had = spanTops.value;
|
|
908
|
+
const keys = Object.keys(tops);
|
|
909
|
+
if (keys.length !== Object.keys(had).length || keys.some((id) => had[id] !== tops[id])) spanTops.value = tops;
|
|
910
|
+
}
|
|
911
|
+
let stopPlacing;
|
|
912
|
+
function startPlacing() {
|
|
913
|
+
stopPlacing?.();
|
|
914
|
+
stopPlacing = void 0;
|
|
915
|
+
if (!spans.value.length || !sideA.value) return;
|
|
916
|
+
placeSpans();
|
|
917
|
+
const moved = typeof MutationObserver === "undefined" ? null : new MutationObserver(schedulePlace);
|
|
918
|
+
for (const side of [sideA.value, sideB.value]) if (side) moved?.observe(side, {
|
|
919
|
+
subtree: true,
|
|
920
|
+
childList: true,
|
|
921
|
+
attributeFilter: ["style"]
|
|
922
|
+
});
|
|
923
|
+
const resized = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(schedulePlace);
|
|
924
|
+
if (root.value) resized?.observe(root.value);
|
|
925
|
+
stopPlacing = () => {
|
|
926
|
+
moved?.disconnect();
|
|
927
|
+
resized?.disconnect();
|
|
928
|
+
if (placeFrame) cancelAnimationFrame(placeFrame);
|
|
929
|
+
placeFrame = 0;
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
watch(() => spans.value.length > 0 && view.value, startPlacing, { flush: "post" });
|
|
933
|
+
onMounted(startPlacing);
|
|
934
|
+
onBeforeUnmount(() => stopPlacing?.());
|
|
935
|
+
/**
|
|
936
|
+
* While the text scrolls, the widgets across both sides are hidden: placed a
|
|
937
|
+
* frame late, they would trail behind it. They come back once it rests.
|
|
938
|
+
*/
|
|
939
|
+
const spansScrolling = ref(false);
|
|
940
|
+
let restTimer;
|
|
941
|
+
function onSideScroll() {
|
|
942
|
+
if (!spans.value.length) return;
|
|
943
|
+
spansScrolling.value = true;
|
|
944
|
+
clearTimeout(restTimer);
|
|
945
|
+
restTimer = setTimeout(() => {
|
|
946
|
+
spansScrolling.value = false;
|
|
947
|
+
placeSpans();
|
|
948
|
+
}, 120);
|
|
949
|
+
}
|
|
950
|
+
onBeforeUnmount(() => clearTimeout(restTimer));
|
|
951
|
+
/** A wheel over a widget across both sides scrolls the text under it. */
|
|
952
|
+
function onSpanWheel(event) {
|
|
953
|
+
const scroller = sideB.value?.querySelector(".dou-code-editor__scroller");
|
|
954
|
+
if (!scroller) return;
|
|
955
|
+
const unit = event.deltaMode === 1 ? LINE : event.deltaMode === 2 ? scroller.clientHeight : 1;
|
|
956
|
+
const dy = event.deltaY * unit;
|
|
957
|
+
const dx = event.deltaX * unit;
|
|
958
|
+
const canY = dy < 0 ? scroller.scrollTop > 0 : dy > 0 && scroller.scrollTop + scroller.clientHeight < scroller.scrollHeight - 1;
|
|
959
|
+
const canX = dx < 0 ? scroller.scrollLeft > 0 : dx > 0 && scroller.scrollLeft + scroller.clientWidth < scroller.scrollWidth - 1;
|
|
960
|
+
if (!canY && !canX) return;
|
|
961
|
+
event.preventDefault();
|
|
962
|
+
if (canY) scroller.scrollTop += dy;
|
|
963
|
+
if (canX) scroller.scrollLeft += dx;
|
|
964
|
+
}
|
|
505
965
|
/** Shows the run of hidden lines a cursor moved into, e.g. with search. */
|
|
506
966
|
function revealAt(side, result) {
|
|
507
967
|
if (!hiddenCount.value) return;
|
|
@@ -608,7 +1068,11 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
608
1068
|
return openBlock(), createElementBlock("div", {
|
|
609
1069
|
ref_key: "root",
|
|
610
1070
|
ref: root,
|
|
611
|
-
class: normalizeClass([
|
|
1071
|
+
class: normalizeClass([
|
|
1072
|
+
unref(ns).b(),
|
|
1073
|
+
unref(ns).m(view.value),
|
|
1074
|
+
unref(ns).is("bordered", __props.bordered)
|
|
1075
|
+
]),
|
|
612
1076
|
role: "group",
|
|
613
1077
|
"aria-label": unref(messages).diff
|
|
614
1078
|
}, [
|
|
@@ -700,6 +1164,7 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
700
1164
|
createElementVNode("div", {
|
|
701
1165
|
ref_key: "sideA",
|
|
702
1166
|
ref: sideA,
|
|
1167
|
+
onScrollCapturePassive: onSideScroll,
|
|
703
1168
|
class: normalizeClass([unref(ns).e("side"), "is-original"])
|
|
704
1169
|
}, [createVNode(code_editor_default, {
|
|
705
1170
|
ref_key: "editorA",
|
|
@@ -713,6 +1178,7 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
713
1178
|
"aria-label": unref(messages).diffOriginal,
|
|
714
1179
|
foldable: false,
|
|
715
1180
|
"active-line": false,
|
|
1181
|
+
bordered: false,
|
|
716
1182
|
readonly: "",
|
|
717
1183
|
onTransaction: _cache[0] || (_cache[0] = ($event) => revealAt("a", $event))
|
|
718
1184
|
}, null, 8, [
|
|
@@ -721,10 +1187,11 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
721
1187
|
"tab-size",
|
|
722
1188
|
"messages",
|
|
723
1189
|
"aria-label"
|
|
724
|
-
])],
|
|
1190
|
+
])], 34),
|
|
725
1191
|
createElementVNode("div", {
|
|
726
1192
|
ref_key: "sideB",
|
|
727
1193
|
ref: sideB,
|
|
1194
|
+
onScrollCapturePassive: onSideScroll,
|
|
728
1195
|
class: normalizeClass([unref(ns).e("side"), "is-modified"])
|
|
729
1196
|
}, [createVNode(code_editor_default, {
|
|
730
1197
|
ref_key: "editorB",
|
|
@@ -738,6 +1205,7 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
738
1205
|
"aria-label": unref(messages).diffModified,
|
|
739
1206
|
foldable: false,
|
|
740
1207
|
"active-line": false,
|
|
1208
|
+
bordered: false,
|
|
741
1209
|
readonly: !__props.editable,
|
|
742
1210
|
"onUpdate:modelValue": onModifiedInput,
|
|
743
1211
|
onTransaction: _cache[1] || (_cache[1] = ($event) => revealAt("b", $event))
|
|
@@ -749,7 +1217,28 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
749
1217
|
"messages",
|
|
750
1218
|
"aria-label",
|
|
751
1219
|
"readonly"
|
|
752
|
-
])],
|
|
1220
|
+
])], 34),
|
|
1221
|
+
spans.value.length ? (openBlock(), createElementBlock("div", {
|
|
1222
|
+
key: 0,
|
|
1223
|
+
ref_key: "spanLayer",
|
|
1224
|
+
ref: spanLayer,
|
|
1225
|
+
class: normalizeClass(unref(ns).e("spans")),
|
|
1226
|
+
style: normalizeStyle({
|
|
1227
|
+
top: `${spanBox.value.top}px`,
|
|
1228
|
+
height: `${spanBox.value.height}px`
|
|
1229
|
+
})
|
|
1230
|
+
}, [(openBlock(true), createElementBlock(Fragment, null, renderList(spans.value, (widget) => {
|
|
1231
|
+
return openBlock(), createElementBlock("div", {
|
|
1232
|
+
key: widget.id,
|
|
1233
|
+
ref_for: true,
|
|
1234
|
+
ref: (el) => measure(el, widget.id),
|
|
1235
|
+
class: normalizeClass([unref(ns).e("span"), unref(ns).is("placed", widget.id in spanTops.value && !spansScrolling.value)]),
|
|
1236
|
+
style: normalizeStyle({ transform: `translateY(${spanTops.value[widget.id] ?? 0}px)` }),
|
|
1237
|
+
role: widget.label ? "group" : void 0,
|
|
1238
|
+
"aria-label": widget.label,
|
|
1239
|
+
onWheel: onSpanWheel
|
|
1240
|
+
}, [createVNode(unref(render_part_default), { render: widget.render }, null, 8, ["render"])], 46, _hoisted_2);
|
|
1241
|
+
}), 128))], 6)) : createCommentVNode("", true)
|
|
753
1242
|
], 34)) : (openBlock(), createElementBlock("div", {
|
|
754
1243
|
key: 2,
|
|
755
1244
|
ref_key: "inlineBox",
|
|
@@ -768,6 +1257,7 @@ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ define
|
|
|
768
1257
|
"line-numbers": false,
|
|
769
1258
|
foldable: false,
|
|
770
1259
|
"active-line": false,
|
|
1260
|
+
bordered: false,
|
|
771
1261
|
wrap: __props.wrap,
|
|
772
1262
|
readonly: "",
|
|
773
1263
|
onTransaction: _cache[2] || (_cache[2] = ($event) => revealAt("inline", $event))
|