@dou-ui/code 0.0.0-stage → 0.1.0-alpha.0

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.
Files changed (158) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -2
  3. package/dist/_virtual/_rolldown/runtime.js +12 -0
  4. package/dist/assets/default-BZBMZKf_.js.map +1 -0
  5. package/dist/code-diff.js +6 -0
  6. package/dist/code-diff.js.map +1 -0
  7. package/dist/code-diff.vue_vue_type_script_setup_true_lang.js +792 -0
  8. package/dist/code-diff.vue_vue_type_script_setup_true_lang.js.map +1 -0
  9. package/dist/code-editor.js +6 -0
  10. package/dist/code-editor.js.map +1 -0
  11. package/dist/code-editor.vue_vue_type_script_setup_true_lang.js +4917 -0
  12. package/dist/code-editor.vue_vue_type_script_setup_true_lang.js.map +1 -0
  13. package/dist/code-input.js +6 -0
  14. package/dist/code-input.js.map +1 -0
  15. package/dist/code-input.vue_vue_type_script_setup_true_lang.js +237 -0
  16. package/dist/code-input.vue_vue_type_script_setup_true_lang.js.map +1 -0
  17. package/dist/code-keys.js +42 -0
  18. package/dist/code-keys.js.map +1 -0
  19. package/dist/code-keys.vue_vue_type_script_setup_true_lang.js +168 -0
  20. package/dist/code-keys.vue_vue_type_script_setup_true_lang.js.map +1 -0
  21. package/dist/code-keys2.js +6 -0
  22. package/dist/code-keys2.js.map +1 -0
  23. package/dist/collaboration/index.js +255 -0
  24. package/dist/collaboration/index.js.map +1 -0
  25. package/dist/completion/match.js +55 -0
  26. package/dist/completion/match.js.map +1 -0
  27. package/dist/completion/snippet.js +71 -0
  28. package/dist/completion/snippet.js.map +1 -0
  29. package/dist/core/brackets.js +55 -0
  30. package/dist/core/brackets.js.map +1 -0
  31. package/dist/core/changes.js +185 -0
  32. package/dist/core/changes.js.map +1 -0
  33. package/dist/core/chars.js +130 -0
  34. package/dist/core/chars.js.map +1 -0
  35. package/dist/core/commands.js +910 -0
  36. package/dist/core/commands.js.map +1 -0
  37. package/dist/core/diff.js +215 -0
  38. package/dist/core/diff.js.map +1 -0
  39. package/dist/core/folding.js +87 -0
  40. package/dist/core/folding.js.map +1 -0
  41. package/dist/core/history.js +74 -0
  42. package/dist/core/history.js.map +1 -0
  43. package/dist/core/keymap.js +326 -0
  44. package/dist/core/keymap.js.map +1 -0
  45. package/dist/core/search.js +91 -0
  46. package/dist/core/search.js.map +1 -0
  47. package/dist/core/selection.js +88 -0
  48. package/dist/core/selection.js.map +1 -0
  49. package/dist/core/text.js +320 -0
  50. package/dist/core/text.js.map +1 -0
  51. package/dist/core/transaction.js +98 -0
  52. package/dist/core/transaction.js.map +1 -0
  53. package/dist/extension.js +16 -0
  54. package/dist/extension.js.map +1 -0
  55. package/dist/index.js +38 -0
  56. package/dist/index.js.map +1 -0
  57. package/dist/language/highlighter.js +336 -0
  58. package/dist/language/highlighter.js.map +1 -0
  59. package/dist/language/language.js +138 -0
  60. package/dist/language/language.js.map +1 -0
  61. package/dist/language/stream.js +112 -0
  62. package/dist/language/stream.js.map +1 -0
  63. package/dist/language/to-html.js +36 -0
  64. package/dist/language/to-html.js.map +1 -0
  65. package/dist/language/worker-client.js +198 -0
  66. package/dist/language/worker-client.js.map +1 -0
  67. package/dist/language/worker-protocol.js +48 -0
  68. package/dist/language/worker-protocol.js.map +1 -0
  69. package/dist/languages/css.js +128 -0
  70. package/dist/languages/css.js.map +1 -0
  71. package/dist/languages/html.js +175 -0
  72. package/dist/languages/html.js.map +1 -0
  73. package/dist/languages/javascript.js +300 -0
  74. package/dist/languages/javascript.js.map +1 -0
  75. package/dist/languages/json.js +45 -0
  76. package/dist/languages/json.js.map +1 -0
  77. package/dist/languages/markdown.js +82 -0
  78. package/dist/languages/markdown.js.map +1 -0
  79. package/dist/languages/python.js +88 -0
  80. package/dist/languages/python.js.map +1 -0
  81. package/dist/languages/shell.js +153 -0
  82. package/dist/languages/shell.js.map +1 -0
  83. package/dist/languages/sql.js +91 -0
  84. package/dist/languages/sql.js.map +1 -0
  85. package/dist/languages/yaml.js +85 -0
  86. package/dist/languages/yaml.js.map +1 -0
  87. package/dist/locale.js +164 -0
  88. package/dist/locale.js.map +1 -0
  89. package/dist/lsp/index.js +715 -0
  90. package/dist/lsp/index.js.map +1 -0
  91. package/dist/lsp/markup.js +139 -0
  92. package/dist/lsp/markup.js.map +1 -0
  93. package/dist/render-part.js +14 -0
  94. package/dist/render-part.js.map +1 -0
  95. package/dist/shiki/index.js +140 -0
  96. package/dist/shiki/index.js.map +1 -0
  97. package/dist/style.css +1 -0
  98. package/dist/style.d.ts +1 -0
  99. package/dist/style.js +4 -0
  100. package/dist/types/code-diff.vue.d.ts +45 -0
  101. package/dist/types/code-editor.vue.d.ts +43 -0
  102. package/dist/types/code-input.vue.d.ts +32 -0
  103. package/dist/types/code-keys.d.ts +9 -0
  104. package/dist/types/code-keys.vue.d.ts +8 -0
  105. package/dist/types/collaboration/index.d.ts +36 -0
  106. package/dist/types/completion/match.d.ts +13 -0
  107. package/dist/types/completion/snippet.d.ts +17 -0
  108. package/dist/types/core/brackets.d.ts +15 -0
  109. package/dist/types/core/changes.d.ts +52 -0
  110. package/dist/types/core/chars.d.ts +24 -0
  111. package/dist/types/core/commands.d.ts +199 -0
  112. package/dist/types/core/diff.d.ts +52 -0
  113. package/dist/types/core/folding.d.ts +23 -0
  114. package/dist/types/core/history.d.ts +24 -0
  115. package/dist/types/core/keymap.d.ts +29 -0
  116. package/dist/types/core/search.d.ts +37 -0
  117. package/dist/types/core/selection.d.ts +28 -0
  118. package/dist/types/core/text.d.ts +79 -0
  119. package/dist/types/core/transaction.d.ts +65 -0
  120. package/dist/types/extension.d.ts +321 -0
  121. package/dist/types/index.d.ts +159 -0
  122. package/dist/types/language/highlighter.d.ts +99 -0
  123. package/dist/types/language/language.d.ts +100 -0
  124. package/dist/types/language/stream.d.ts +56 -0
  125. package/dist/types/language/to-html.d.ts +17 -0
  126. package/dist/types/language/worker-client.d.ts +52 -0
  127. package/dist/types/language/worker-protocol.d.ts +50 -0
  128. package/dist/types/languages/css.d.ts +17 -0
  129. package/dist/types/languages/html.d.ts +34 -0
  130. package/dist/types/languages/javascript.d.ts +29 -0
  131. package/dist/types/languages/json.d.ts +7 -0
  132. package/dist/types/languages/markdown.d.ts +17 -0
  133. package/dist/types/languages/python.d.ts +9 -0
  134. package/dist/types/languages/shell.d.ts +11 -0
  135. package/dist/types/languages/sql.d.ts +9 -0
  136. package/dist/types/languages/yaml.d.ts +14 -0
  137. package/dist/types/locale.d.ts +79 -0
  138. package/dist/types/lsp/index.d.ts +119 -0
  139. package/dist/types/lsp/markup.d.ts +14 -0
  140. package/dist/types/render-part.d.ts +14 -0
  141. package/dist/types/shiki/index.d.ts +63 -0
  142. package/dist/types/types.d.ts +267 -0
  143. package/dist/types/view/input.d.ts +12 -0
  144. package/dist/types/view/layout.d.ts +55 -0
  145. package/dist/types/view/measure.d.ts +22 -0
  146. package/dist/types/worker/default.d.ts +1 -0
  147. package/dist/types/worker/index.d.ts +34 -0
  148. package/dist/view/input.js +27 -0
  149. package/dist/view/input.js.map +1 -0
  150. package/dist/view/layout.js +194 -0
  151. package/dist/view/layout.js.map +1 -0
  152. package/dist/view/measure.js +104 -0
  153. package/dist/view/measure.js.map +1 -0
  154. package/dist/worker/default.js +20 -0
  155. package/dist/worker/default.js.map +1 -0
  156. package/dist/worker/index.js +139 -0
  157. package/dist/worker/index.js.map +1 -0
  158. package/package.json +88 -4
@@ -0,0 +1,792 @@
1
+ import { splitLines } from "./core/text.js";
2
+ import { format, useCodeMessages } from "./locale.js";
3
+ import { defineCodeExtension } from "./extension.js";
4
+ import code_editor_default from "./code-editor.js";
5
+ import { diffText, lineStarts } from "./core/diff.js";
6
+ 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 { ArrowDownIcon, ArrowUpIcon, DiffSplitIcon, DiffUnifiedIcon, FoldVerticalIcon, UnfoldVerticalIcon } from "@dou-ui/icons";
9
+ // src/code-diff.vue?vue&type=script&setup=true&lang.ts
10
+ var _hoisted_1 = ["aria-label"];
11
+ /**
12
+ * Two texts compared: side by side, or the changes in one column. Changed
13
+ * lines are tinted, the words that changed inside them marked, and long
14
+ * runs of unchanged lines hidden behind a bar that shows them.
15
+ */
16
+ var SPLIT_WIDTH = 560;
17
+ var BAR_HEIGHT = 28;
18
+ /** What one side of the split view draws: tints, marks, fillers, hidden lines and bars. */
19
+ var Kind = /* @__PURE__ */ function(Kind) {
20
+ Kind[Kind["Same"] = 0] = "Same";
21
+ Kind[Kind["Removed"] = 1] = "Removed";
22
+ Kind[Kind["Added"] = 2] = "Added";
23
+ return Kind;
24
+ }(Kind || {});
25
+ var code_diff_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defineComponent({
26
+ name: "DouCodeDiff",
27
+ __name: "code-diff",
28
+ props: {
29
+ original: { default: "" },
30
+ modelValue: { default: "" },
31
+ language: { default: void 0 },
32
+ mode: { default: "auto" },
33
+ editable: {
34
+ type: Boolean,
35
+ default: false
36
+ },
37
+ collapseUnchanged: {
38
+ type: [Boolean, Object],
39
+ default: true
40
+ },
41
+ ignoreWhitespace: {
42
+ type: Boolean,
43
+ default: false
44
+ },
45
+ words: {
46
+ type: Boolean,
47
+ default: true
48
+ },
49
+ wrap: {
50
+ type: Boolean,
51
+ default: false
52
+ },
53
+ extensions: { default: void 0 },
54
+ tabSize: { default: 2 },
55
+ toolbar: {
56
+ type: Boolean,
57
+ default: true
58
+ },
59
+ messages: { default: void 0 }
60
+ },
61
+ emits: ["update:modelValue", "update:mode"],
62
+ setup(__props, { expose: __expose, emit: __emit }) {
63
+ const props = __props;
64
+ const emit = __emit;
65
+ const ns = useNamespace("code-diff");
66
+ const messages = useCodeMessages(() => props.messages);
67
+ const root = ref();
68
+ /** Whether the box is wide enough for two columns. */
69
+ const roomy = ref(true);
70
+ /** The view picked with the toggle, when `mode` isn't bound. */
71
+ const picked = ref(null);
72
+ watch(() => props.mode, () => picked.value = null);
73
+ const view = computed(() => {
74
+ if (picked.value) return picked.value;
75
+ if (props.mode !== "auto") return props.mode;
76
+ return roomy.value ? "split" : "inline";
77
+ });
78
+ function toggleView() {
79
+ const other = view.value === "split" ? "inline" : "split";
80
+ picked.value = other;
81
+ emit("update:mode", other);
82
+ }
83
+ /** Below this, each side would show fewer than about 30 columns: one column reads better. */
84
+ let resize;
85
+ onMounted(() => {
86
+ if (typeof ResizeObserver === "undefined" || !root.value) return;
87
+ resize = new ResizeObserver(([entry]) => {
88
+ if (entry) roomy.value = entry.contentRect.width >= SPLIT_WIDTH;
89
+ });
90
+ resize.observe(root.value);
91
+ });
92
+ onBeforeUnmount(() => resize?.disconnect());
93
+ /** The modified text as edited here, ahead of the parent's copy. */
94
+ const modified = ref(props.modelValue);
95
+ watch(() => props.modelValue, (value) => modified.value = value);
96
+ function onModifiedInput(value) {
97
+ modified.value = value;
98
+ emit("update:modelValue", value);
99
+ }
100
+ const hunks = computed(() => diffText(props.original, modified.value, {
101
+ ignoreWhitespace: props.ignoreWhitespace,
102
+ words: props.words
103
+ }));
104
+ const originalLines = computed(() => splitLines(props.original));
105
+ const modifiedLines = computed(() => splitLines(modified.value));
106
+ const stats = computed(() => {
107
+ let added = 0;
108
+ let removed = 0;
109
+ for (const hunk of hunks.value) {
110
+ added += hunk.modifiedCount;
111
+ removed += hunk.originalCount;
112
+ }
113
+ return {
114
+ added,
115
+ removed
116
+ };
117
+ });
118
+ const collapse = computed(() => {
119
+ const option = props.collapseUnchanged;
120
+ if (!option) return null;
121
+ const own = option === true ? {} : option;
122
+ return {
123
+ context: Math.max(1, Math.floor(own.context ?? 3)),
124
+ minLines: Math.max(1, Math.floor(own.minLines ?? 4))
125
+ };
126
+ });
127
+ /** The runs of unchanged lines opened with their bar, by key; `true` for all of them. */
128
+ const opened = reactive(/* @__PURE__ */ new Set());
129
+ const allOpen = ref(false);
130
+ const unchanged = computed(() => {
131
+ const list = hunks.value;
132
+ const total = originalLines.value.length;
133
+ const out = [];
134
+ const rule = collapse.value;
135
+ const push = (a, b, count, before, after) => {
136
+ if (count <= 0) return;
137
+ let hideFrom = 0;
138
+ let hideCount = 0;
139
+ if (rule && list.length && !allOpen.value && !opened.has(a)) {
140
+ const from = before ? rule.context : 0;
141
+ const to = count - (after ? rule.context : 0);
142
+ if (to - from >= rule.minLines) {
143
+ hideFrom = from;
144
+ hideCount = to - from;
145
+ }
146
+ }
147
+ out.push({
148
+ a,
149
+ b,
150
+ count,
151
+ hideFrom,
152
+ hideCount,
153
+ key: a
154
+ });
155
+ };
156
+ let a = 1;
157
+ let b = 1;
158
+ for (let i = 0; i < list.length; i++) {
159
+ const hunk = list[i];
160
+ push(a, b, hunk.originalStart - a, i > 0, true);
161
+ a = hunk.originalStart + hunk.originalCount;
162
+ b = hunk.modifiedStart + hunk.modifiedCount;
163
+ }
164
+ push(a, b, total - a + 1, list.length > 0, false);
165
+ return out;
166
+ });
167
+ const hiddenCount = computed(() => unchanged.value.reduce((sum, run) => sum + run.hideCount, 0));
168
+ const anyHideable = computed(() => !!collapse.value && hunks.value.length > 0);
169
+ function open(key) {
170
+ opened.add(key);
171
+ }
172
+ function toggleAll() {
173
+ if (hiddenCount.value) allOpen.value = true;
174
+ else {
175
+ allOpen.value = false;
176
+ opened.clear();
177
+ }
178
+ }
179
+ watch(() => props.original, () => {
180
+ allOpen.value = false;
181
+ opened.clear();
182
+ });
183
+ /**
184
+ * The bar over a run of hidden lines. Side by side, the original's bar
185
+ * mirrors the modified one's: pressed with a pointer, but left out of the
186
+ * keyboard order and of what is read out, so the run is one stop, not two.
187
+ */
188
+ function bar(run, mirror = false) {
189
+ const label = format(messages.value.diffUnchanged, { count: run.hideCount });
190
+ return h("button", {
191
+ type: "button",
192
+ class: ns.e("bar"),
193
+ "aria-label": `${messages.value.unfold}:${label}`,
194
+ "aria-hidden": mirror ? "true" : void 0,
195
+ tabindex: mirror ? -1 : void 0,
196
+ onClick: () => open(run.key)
197
+ }, [h(UnfoldVerticalIcon, { class: ns.e("bar-icon") }), h("span", label)]);
198
+ }
199
+ function sideExtension(side) {
200
+ const startOf = (hunk) => side === "a" ? hunk.originalStart : hunk.modifiedStart;
201
+ const countOf = (hunk) => side === "a" ? hunk.originalCount : hunk.modifiedCount;
202
+ const otherCountOf = (hunk) => side === "a" ? hunk.modifiedCount : hunk.originalCount;
203
+ const tone = side === "a" ? "removed" : "added";
204
+ return defineCodeExtension({
205
+ name: "diff",
206
+ decorations({ visible }) {
207
+ const out = [];
208
+ for (const hunk of hunks.value) {
209
+ const start = startOf(hunk);
210
+ const count = countOf(hunk);
211
+ if (start > visible.to) break;
212
+ if (!count || start + count - 1 < visible.from) continue;
213
+ const from = Math.max(start, visible.from);
214
+ const to = Math.min(start + count - 1, visible.to);
215
+ for (let n = from; n <= to; n++) out.push({
216
+ type: "line",
217
+ line: n,
218
+ class: `${ns.e("line")} is-${tone}`
219
+ });
220
+ for (const part of hunk.parts) {
221
+ const range = side === "a" ? part.original : part.modified;
222
+ if (range.to > range.from) out.push({
223
+ type: "mark",
224
+ from: range.from,
225
+ to: range.to,
226
+ class: `${ns.e("word")} is-${tone}`
227
+ });
228
+ }
229
+ }
230
+ return out;
231
+ },
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
+ widgets() {
253
+ const out = [];
254
+ hunks.value.forEach((hunk, i) => {
255
+ const missing = otherCountOf(hunk) - countOf(hunk);
256
+ if (missing <= 0) return;
257
+ const last = startOf(hunk) - 1 + countOf(hunk);
258
+ out.push({
259
+ id: `fill-${i}`,
260
+ ...last >= 1 ? { line: last } : {
261
+ line: 1,
262
+ position: "above"
263
+ },
264
+ height: missing * 20,
265
+ render: () => h("div", {
266
+ class: ns.e("filler"),
267
+ style: { height: `calc(${missing} * var(--dou-code-editor-line-height))` }
268
+ })
269
+ });
270
+ });
271
+ const total = side === "a" ? originalLines.value.length : modifiedLines.value.length;
272
+ for (const run of unchanged.value) {
273
+ if (!run.hideCount) continue;
274
+ const first = (side === "a" ? run.a : run.b) + run.hideFrom;
275
+ const last = first + run.hideCount - 1;
276
+ out.push({
277
+ id: `bar-${run.key}`,
278
+ ...last < total ? {
279
+ line: last + 1,
280
+ position: "above"
281
+ } : { line: first - 1 },
282
+ height: BAR_HEIGHT,
283
+ render: () => bar(run, side === "a")
284
+ });
285
+ }
286
+ return out;
287
+ },
288
+ hide() {
289
+ const out = [];
290
+ for (const run of unchanged.value) {
291
+ if (!run.hideCount) continue;
292
+ const first = (side === "a" ? run.a : run.b) + run.hideFrom;
293
+ out.push({
294
+ from: first,
295
+ to: first + run.hideCount - 1
296
+ });
297
+ }
298
+ return out;
299
+ }
300
+ });
301
+ }
302
+ const modifiedOwn = sideExtension("b");
303
+ const modifiedExtensions = computed(() => [modifiedOwn, ...props.extensions ?? []]);
304
+ const editorA = ref();
305
+ const editorB = ref();
306
+ const sideA = ref();
307
+ const sideB = ref();
308
+ const extensionsA = [sideExtension("a")];
309
+ const link = createScrollLink();
310
+ function onScroll(event) {
311
+ if (view.value !== "split") return;
312
+ const from = event.target.closest(`.${ns.e("side")}`);
313
+ const source = from?.querySelector(".dou-code-editor__scroller");
314
+ if (!source || source !== event.target) return;
315
+ const other = (from === sideA.value ? sideB.value : sideA.value)?.querySelector(".dou-code-editor__scroller");
316
+ if (other) link.follow(source, other);
317
+ }
318
+ const unified = computed(() => {
319
+ if (view.value !== "inline") return null;
320
+ const a = originalLines.value;
321
+ const b = modifiedLines.value;
322
+ const lines = [];
323
+ const kinds = [];
324
+ const aNums = [];
325
+ const bNums = [];
326
+ const hunkLines = [];
327
+ const runLines = /* @__PURE__ */ new Map();
328
+ const marks = [];
329
+ let pos = 0;
330
+ const push = (text, kind, an, bn) => {
331
+ lines.push(text);
332
+ kinds.push(kind);
333
+ aNums.push(an);
334
+ bNums.push(bn);
335
+ pos += text.length + 1;
336
+ };
337
+ const runs = unchanged.value;
338
+ let r = 0;
339
+ const sameUpTo = (aEnd) => {
340
+ while (r < runs.length && runs[r].a < aEnd) {
341
+ const run = runs[r++];
342
+ runLines.set(run.key, lines.length + 1);
343
+ for (let i = 0; i < run.count; i++) push(b[run.b - 1 + i], Kind.Same, run.a + i, run.b + i);
344
+ }
345
+ };
346
+ const aStarts = lineStarts(a);
347
+ const bStarts = lineStarts(b);
348
+ for (const hunk of hunks.value) {
349
+ sameUpTo(hunk.originalStart);
350
+ hunkLines.push(lines.length + 1);
351
+ const removedAt = pos;
352
+ for (let i = 0; i < hunk.originalCount; i++) push(a[hunk.originalStart - 1 + i], Kind.Removed, hunk.originalStart + i, 0);
353
+ const addedAt = pos;
354
+ for (let i = 0; i < hunk.modifiedCount; i++) push(b[hunk.modifiedStart - 1 + i], Kind.Added, 0, hunk.modifiedStart + i);
355
+ const aBase = aStarts[hunk.originalStart - 1];
356
+ const bBase = bStarts[hunk.modifiedStart - 1];
357
+ for (const part of hunk.parts) {
358
+ if (part.original.to > part.original.from) marks.push({
359
+ from: removedAt + part.original.from - aBase,
360
+ to: removedAt + part.original.to - aBase,
361
+ kind: Kind.Removed
362
+ });
363
+ if (part.modified.to > part.modified.from) marks.push({
364
+ from: addedAt + part.modified.from - bBase,
365
+ to: addedAt + part.modified.to - bBase,
366
+ kind: Kind.Added
367
+ });
368
+ }
369
+ }
370
+ sameUpTo(Infinity);
371
+ return {
372
+ text: lines.join("\n"),
373
+ kinds: Uint8Array.from(kinds),
374
+ aNums: Int32Array.from(aNums),
375
+ bNums: Int32Array.from(bNums),
376
+ hunkLines,
377
+ runLines,
378
+ marks
379
+ };
380
+ });
381
+ const digits = (n) => String(Math.max(1, n)).length;
382
+ const numberWidth = (n) => digits(n) * 8 + 16;
383
+ /** The line numbers of one side, in the inline view's gutter. */
384
+ const numbersOf = (pick) => ({ visible }) => {
385
+ const u = unified.value;
386
+ if (!u) return [];
387
+ const out = [];
388
+ const list = u[pick];
389
+ for (let n = visible.from; n <= visible.to && n <= list.length; n++) {
390
+ const value = list[n - 1];
391
+ const kind = u.kinds[n - 1];
392
+ const tone = kind === Kind.Removed ? " is-removed" : kind === Kind.Added ? " is-added" : "";
393
+ out.push({
394
+ line: n,
395
+ text: value ? String(value) : "",
396
+ class: `${ns.e("number")}${tone}`
397
+ });
398
+ }
399
+ return out;
400
+ };
401
+ const inlineExtensions = [defineCodeExtension({
402
+ name: "diff",
403
+ decorations({ visible }) {
404
+ const u = unified.value;
405
+ if (!u) return [];
406
+ const out = [];
407
+ for (let n = visible.from; n <= visible.to && n <= u.kinds.length; n++) {
408
+ const kind = u.kinds[n - 1];
409
+ if (kind === Kind.Removed) out.push({
410
+ type: "line",
411
+ line: n,
412
+ class: `${ns.e("line")} is-removed`
413
+ });
414
+ else if (kind === Kind.Added) out.push({
415
+ type: "line",
416
+ line: n,
417
+ class: `${ns.e("line")} is-added`
418
+ });
419
+ }
420
+ for (const mark of u.marks) out.push({
421
+ type: "mark",
422
+ from: mark.from,
423
+ to: mark.to,
424
+ class: `${ns.e("word")} ${mark.kind === Kind.Removed ? "is-removed" : "is-added"}`
425
+ });
426
+ return out;
427
+ },
428
+ get gutters() {
429
+ return [
430
+ {
431
+ name: "diff-original",
432
+ width: numberWidth(originalLines.value.length),
433
+ markers: numbersOf("aNums")
434
+ },
435
+ {
436
+ name: "diff-modified",
437
+ width: numberWidth(modifiedLines.value.length),
438
+ markers: numbersOf("bNums")
439
+ },
440
+ {
441
+ name: "diff",
442
+ width: 16,
443
+ markers: ({ visible }) => {
444
+ const u = unified.value;
445
+ if (!u) return [];
446
+ const out = [];
447
+ for (let n = visible.from; n <= visible.to && n <= u.kinds.length; n++) {
448
+ const kind = u.kinds[n - 1];
449
+ if (kind === Kind.Removed) out.push({
450
+ line: n,
451
+ text: "−",
452
+ class: `${ns.e("sign")} is-removed`,
453
+ title: messages.value.diffRemoved
454
+ });
455
+ else if (kind === Kind.Added) out.push({
456
+ line: n,
457
+ text: "+",
458
+ class: `${ns.e("sign")} is-added`,
459
+ title: messages.value.diffAdded
460
+ });
461
+ }
462
+ return out;
463
+ }
464
+ }
465
+ ];
466
+ },
467
+ widgets() {
468
+ const u = unified.value;
469
+ if (!u) return [];
470
+ const total = u.kinds.length;
471
+ const out = [];
472
+ for (const run of unchanged.value) {
473
+ if (!run.hideCount) continue;
474
+ const first = u.runLines.get(run.key) + run.hideFrom;
475
+ const last = first + run.hideCount - 1;
476
+ out.push({
477
+ id: `bar-${run.key}`,
478
+ ...last < total ? {
479
+ line: last + 1,
480
+ position: "above"
481
+ } : { line: first - 1 },
482
+ height: BAR_HEIGHT,
483
+ render: () => bar(run)
484
+ });
485
+ }
486
+ return out;
487
+ },
488
+ hide() {
489
+ const u = unified.value;
490
+ if (!u) return [];
491
+ const out = [];
492
+ for (const run of unchanged.value) {
493
+ if (!run.hideCount) continue;
494
+ const first = u.runLines.get(run.key) + run.hideFrom;
495
+ out.push({
496
+ from: first,
497
+ to: first + run.hideCount - 1
498
+ });
499
+ }
500
+ return out;
501
+ }
502
+ })];
503
+ const editorInline = ref();
504
+ const inlineBox = ref();
505
+ /** Shows the run of hidden lines a cursor moved into, e.g. with search. */
506
+ function revealAt(side, result) {
507
+ if (!hiddenCount.value) return;
508
+ const { doc, selection } = result.state;
509
+ const u = unified.value;
510
+ for (const range of selection.ranges) {
511
+ const line = doc.lineAt(range.head).number;
512
+ for (const run of unchanged.value) {
513
+ if (!run.hideCount) continue;
514
+ const first = (side === "inline" ? u?.runLines.get(run.key) ?? 0 : side === "a" ? run.a : run.b) + run.hideFrom;
515
+ if (line >= first && line < first + run.hideCount) open(run.key);
516
+ }
517
+ }
518
+ }
519
+ const live = ref("");
520
+ const current = ref(-1);
521
+ function target() {
522
+ if (view.value === "inline") return editorInline.value && inlineBox.value ? {
523
+ editor: editorInline.value,
524
+ box: inlineBox.value,
525
+ side: "inline"
526
+ } : null;
527
+ const inA = !!sideA.value?.contains(document.activeElement);
528
+ const editor = inA ? editorA.value : editorB.value;
529
+ const box = inA ? sideA.value : sideB.value;
530
+ return editor && box ? {
531
+ editor,
532
+ box,
533
+ side: inA ? "a" : "b"
534
+ } : null;
535
+ }
536
+ function lineOf(index, side) {
537
+ const hunk = hunks.value[index];
538
+ if (side === "inline") return unified.value?.hunkLines[index] ?? 1;
539
+ return side === "a" ? hunk.originalStart : hunk.modifiedStart;
540
+ }
541
+ function go(step) {
542
+ const list = hunks.value;
543
+ const t = target();
544
+ if (!list.length || !t) return false;
545
+ const doc = t.editor.doc;
546
+ const at = doc.lineAt(t.editor.selection.ranges[t.editor.selection.main].head).number;
547
+ let index = -1;
548
+ if (step > 0) {
549
+ index = list.findIndex((_, i) => lineOf(i, t.side) > at);
550
+ if (index < 0) index = 0;
551
+ } else {
552
+ for (let i = list.length - 1; i >= 0; i--) if (lineOf(i, t.side) < at) {
553
+ index = i;
554
+ break;
555
+ }
556
+ if (index < 0) index = list.length - 1;
557
+ }
558
+ const line = Math.min(doc.lines, Math.max(1, lineOf(index, t.side)));
559
+ t.editor.select(doc.line(line).from);
560
+ current.value = index;
561
+ const hunk = list[index];
562
+ live.value = format(messages.value.diffChangeAt, {
563
+ index: index + 1,
564
+ count: list.length,
565
+ line: hunk.modifiedStart,
566
+ removed: hunk.originalCount,
567
+ added: hunk.modifiedCount
568
+ });
569
+ nextTick(() => t.box.querySelector("textarea")?.scrollIntoView({ block: "nearest" }));
570
+ return true;
571
+ }
572
+ const keymap = {
573
+ f7: () => go(1),
574
+ "shift+f7": () => go(-1)
575
+ };
576
+ function next() {
577
+ return go(1);
578
+ }
579
+ function previous() {
580
+ return go(-1);
581
+ }
582
+ const summary = computed(() => hunks.value.length ? format(messages.value.diffStats, stats.value) : messages.value.diffNoChanges);
583
+ __expose({
584
+ /** The changes found now. */
585
+ get hunks() {
586
+ return hunks.value;
587
+ },
588
+ /** Moves to the next change; `false` when there is none. */
589
+ next,
590
+ /** Moves to the previous change. */
591
+ previous,
592
+ /** Shows every unchanged line. */
593
+ expandAll: () => allOpen.value = true,
594
+ /** Hides the runs of unchanged lines again. */
595
+ collapseAll: () => {
596
+ allOpen.value = false;
597
+ opened.clear();
598
+ },
599
+ /** The editors drawn now: both sides, or the inline one as `modified`. */
600
+ get original() {
601
+ return view.value === "split" ? editorA.value : void 0;
602
+ },
603
+ get modified() {
604
+ return view.value === "split" ? editorB.value : editorInline.value;
605
+ }
606
+ });
607
+ return (_ctx, _cache) => {
608
+ return openBlock(), createElementBlock("div", {
609
+ ref_key: "root",
610
+ ref: root,
611
+ class: normalizeClass([unref(ns).b(), unref(ns).m(view.value)]),
612
+ role: "group",
613
+ "aria-label": unref(messages).diff
614
+ }, [
615
+ __props.toolbar ? (openBlock(), createElementBlock("div", {
616
+ key: 0,
617
+ class: normalizeClass(unref(ns).e("toolbar"))
618
+ }, [
619
+ createElementVNode("span", { class: normalizeClass(unref(ns).e("stats")) }, [hunks.value.length ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [
620
+ createElementVNode("span", {
621
+ class: normalizeClass([unref(ns).e("count"), "is-added"]),
622
+ "aria-hidden": "true"
623
+ }, "+" + toDisplayString(stats.value.added), 3),
624
+ createElementVNode("span", {
625
+ class: normalizeClass([unref(ns).e("count"), "is-removed"]),
626
+ "aria-hidden": "true"
627
+ }, "−" + toDisplayString(stats.value.removed), 3),
628
+ createElementVNode("span", { class: normalizeClass(unref(ns).e("hidden-text")) }, toDisplayString(summary.value), 3)
629
+ ], 64)) : (openBlock(), createElementBlock("span", {
630
+ key: 1,
631
+ class: normalizeClass(unref(ns).e("summary"))
632
+ }, toDisplayString(summary.value), 3))], 2),
633
+ createVNode(unref(DouTooltip), {
634
+ content: unref(messages).diffPrevious,
635
+ shortcut: "shift+f7"
636
+ }, {
637
+ default: withCtx(() => [createVNode(unref(DouButton), {
638
+ variant: "ghost",
639
+ size: "sm",
640
+ disabled: !hunks.value.length,
641
+ "aria-label": unref(messages).diffPrevious,
642
+ onClick: previous
643
+ }, {
644
+ default: withCtx(() => [createVNode(unref(ArrowUpIcon))]),
645
+ _: 1
646
+ }, 8, ["disabled", "aria-label"])]),
647
+ _: 1
648
+ }, 8, ["content"]),
649
+ createVNode(unref(DouTooltip), {
650
+ content: unref(messages).diffNext,
651
+ shortcut: "f7"
652
+ }, {
653
+ default: withCtx(() => [createVNode(unref(DouButton), {
654
+ variant: "ghost",
655
+ size: "sm",
656
+ disabled: !hunks.value.length,
657
+ "aria-label": unref(messages).diffNext,
658
+ onClick: next
659
+ }, {
660
+ default: withCtx(() => [createVNode(unref(ArrowDownIcon))]),
661
+ _: 1
662
+ }, 8, ["disabled", "aria-label"])]),
663
+ _: 1
664
+ }, 8, ["content"]),
665
+ anyHideable.value ? (openBlock(), createBlock(unref(DouTooltip), {
666
+ key: 0,
667
+ content: hiddenCount.value ? unref(messages).diffExpandAll : unref(messages).diffCollapse
668
+ }, {
669
+ default: withCtx(() => [createVNode(unref(DouButton), {
670
+ variant: "ghost",
671
+ size: "sm",
672
+ "aria-label": hiddenCount.value ? unref(messages).diffExpandAll : unref(messages).diffCollapse,
673
+ onClick: toggleAll
674
+ }, {
675
+ default: withCtx(() => [hiddenCount.value ? (openBlock(), createBlock(unref(UnfoldVerticalIcon), { key: 0 })) : (openBlock(), createBlock(unref(FoldVerticalIcon), { key: 1 }))]),
676
+ _: 1
677
+ }, 8, ["aria-label"])]),
678
+ _: 1
679
+ }, 8, ["content"])) : createCommentVNode("", true),
680
+ createVNode(unref(DouTooltip), { content: view.value === "split" ? unref(messages).diffInline : unref(messages).diffSplit }, {
681
+ default: withCtx(() => [createVNode(unref(DouButton), {
682
+ variant: "ghost",
683
+ size: "sm",
684
+ "aria-label": view.value === "split" ? unref(messages).diffInline : unref(messages).diffSplit,
685
+ onClick: toggleView
686
+ }, {
687
+ default: withCtx(() => [view.value === "split" ? (openBlock(), createBlock(unref(DiffUnifiedIcon), { key: 0 })) : (openBlock(), createBlock(unref(DiffSplitIcon), { key: 1 }))]),
688
+ _: 1
689
+ }, 8, ["aria-label"])]),
690
+ _: 1
691
+ }, 8, ["content"])
692
+ ], 2)) : createCommentVNode("", true),
693
+ view.value === "split" ? (openBlock(), createElementBlock("div", {
694
+ key: 1,
695
+ class: normalizeClass(unref(ns).e("body")),
696
+ onScrollCapture: onScroll
697
+ }, [
698
+ createElementVNode("div", { class: normalizeClass(unref(ns).e("label")) }, toDisplayString(unref(messages).diffOriginal), 3),
699
+ createElementVNode("div", { class: normalizeClass(unref(ns).e("label")) }, toDisplayString(unref(messages).diffModified), 3),
700
+ createElementVNode("div", {
701
+ ref_key: "sideA",
702
+ ref: sideA,
703
+ class: normalizeClass([unref(ns).e("side"), "is-original"])
704
+ }, [createVNode(code_editor_default, {
705
+ ref_key: "editorA",
706
+ ref: editorA,
707
+ "model-value": __props.original,
708
+ language: __props.language,
709
+ extensions: extensionsA,
710
+ "tab-size": __props.tabSize,
711
+ messages: unref(messages),
712
+ keymap,
713
+ "aria-label": unref(messages).diffOriginal,
714
+ foldable: false,
715
+ "active-line": false,
716
+ readonly: "",
717
+ onTransaction: _cache[0] || (_cache[0] = ($event) => revealAt("a", $event))
718
+ }, null, 8, [
719
+ "model-value",
720
+ "language",
721
+ "tab-size",
722
+ "messages",
723
+ "aria-label"
724
+ ])], 2),
725
+ createElementVNode("div", {
726
+ ref_key: "sideB",
727
+ ref: sideB,
728
+ class: normalizeClass([unref(ns).e("side"), "is-modified"])
729
+ }, [createVNode(code_editor_default, {
730
+ ref_key: "editorB",
731
+ ref: editorB,
732
+ "model-value": modified.value,
733
+ language: __props.language,
734
+ extensions: modifiedExtensions.value,
735
+ "tab-size": __props.tabSize,
736
+ messages: unref(messages),
737
+ keymap,
738
+ "aria-label": unref(messages).diffModified,
739
+ foldable: false,
740
+ "active-line": false,
741
+ readonly: !__props.editable,
742
+ "onUpdate:modelValue": onModifiedInput,
743
+ onTransaction: _cache[1] || (_cache[1] = ($event) => revealAt("b", $event))
744
+ }, null, 8, [
745
+ "model-value",
746
+ "language",
747
+ "extensions",
748
+ "tab-size",
749
+ "messages",
750
+ "aria-label",
751
+ "readonly"
752
+ ])], 2)
753
+ ], 34)) : (openBlock(), createElementBlock("div", {
754
+ key: 2,
755
+ ref_key: "inlineBox",
756
+ ref: inlineBox,
757
+ class: normalizeClass(unref(ns).e("body"))
758
+ }, [createVNode(code_editor_default, {
759
+ ref_key: "editorInline",
760
+ ref: editorInline,
761
+ "model-value": unified.value?.text ?? "",
762
+ language: __props.language,
763
+ extensions: inlineExtensions,
764
+ "tab-size": __props.tabSize,
765
+ messages: unref(messages),
766
+ keymap,
767
+ "aria-label": unref(messages).diff,
768
+ "line-numbers": false,
769
+ foldable: false,
770
+ "active-line": false,
771
+ wrap: __props.wrap,
772
+ readonly: "",
773
+ onTransaction: _cache[2] || (_cache[2] = ($event) => revealAt("inline", $event))
774
+ }, null, 8, [
775
+ "model-value",
776
+ "language",
777
+ "tab-size",
778
+ "messages",
779
+ "aria-label",
780
+ "wrap"
781
+ ])], 2)),
782
+ createElementVNode("div", {
783
+ class: normalizeClass(unref(ns).e("live")),
784
+ "aria-live": "polite"
785
+ }, toDisplayString(live.value), 3)
786
+ ], 10, _hoisted_1);
787
+ };
788
+ }
789
+ });
790
+ export { code_diff_vue_vue_type_script_setup_true_lang_default as default };
791
+
792
+ //# sourceMappingURL=code-diff.vue_vue_type_script_setup_true_lang.js.map