@mirror-physics/fractal-ui 0.2.0-next.53 → 0.2.0-next.55

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/index.js CHANGED
@@ -170,7 +170,12 @@ import { useEffect, useMemo, useRef, useState as useState2 } from "react";
170
170
  import CodeMirror from "@uiw/react-codemirror";
171
171
  import { HighlightStyle, foldGutter, foldService, syntaxHighlighting } from "@codemirror/language";
172
172
  import { json } from "@codemirror/lang-json";
173
- import { EditorView, lineNumbers as lineNumberGutter } from "@codemirror/view";
173
+ import {
174
+ Decoration,
175
+ EditorView,
176
+ ViewPlugin,
177
+ lineNumbers as lineNumberGutter
178
+ } from "@codemirror/view";
174
179
  import { tags } from "@lezer/highlight";
175
180
  import { Check, Copy } from "@mirror-physics/fractal-icons";
176
181
  import { jsx, jsxs as jsxs2 } from "react/jsx-runtime";
@@ -187,7 +192,10 @@ var codeTheme = EditorView.theme({
187
192
  overscrollBehavior: "none"
188
193
  },
189
194
  ".cm-content": { padding: "10px 0" },
190
- ".cm-line": { padding: "0 12px" },
195
+ ".cm-line": {
196
+ padding: "0 12px 0 calc(12px + var(--fractal-code-wrap-indent, 0ch))",
197
+ textIndent: "calc(0px - var(--fractal-code-wrap-indent, 0ch))"
198
+ },
191
199
  ".cm-cursor, .cm-dropCursor": { borderLeftColor: "var(--fg-primary)" },
192
200
  ".cm-gutters": {
193
201
  backgroundColor: "var(--bg-canvas)",
@@ -214,6 +222,7 @@ var codeTheme = EditorView.theme({
214
222
  padding: "0",
215
223
  color: "var(--fg-tertiary)",
216
224
  lineHeight: "14px",
225
+ transform: "translateY(-3px)",
217
226
  transition: "color 120ms ease"
218
227
  },
219
228
  ".cm-foldGutter .cm-gutterElement > span:hover": { color: "var(--fg-secondary)" },
@@ -252,6 +261,48 @@ var jsonSupport = json();
252
261
  var JSON_LANGUAGE_NAMES = /* @__PURE__ */ new Set(["json", "jsonl", "ndjson", "geojson"]);
253
262
  var JSON_LINES_LANGUAGE_NAMES = /* @__PURE__ */ new Set(["jsonl", "ndjson"]);
254
263
  var JSON_LINES_PRETTY_PRINT_THRESHOLD = 160;
264
+ var JSON_PROPERTY_PREFIX = /^(\s*"(?:\\.|[^"\\])*"\s*:\s*)/;
265
+ function countVisualColumns(value) {
266
+ let columns = 0;
267
+ for (const character of value) columns += character === " " ? 2 : 1;
268
+ return columns;
269
+ }
270
+ function getWrappedLineIndent(line) {
271
+ const propertyPrefix = line.match(JSON_PROPERTY_PREFIX)?.[1];
272
+ if (propertyPrefix) return Math.min(countVisualColumns(propertyPrefix), 24);
273
+ const leadingWhitespace = line.match(/^\s*/)?.[0] ?? "";
274
+ return Math.min(countVisualColumns(leadingWhitespace) + 2, 12);
275
+ }
276
+ function buildWrappedLineIndentation(view) {
277
+ const decorations = [];
278
+ const decoratedLines = /* @__PURE__ */ new Set();
279
+ for (const visibleRange of view.visibleRanges) {
280
+ let position = visibleRange.from;
281
+ while (position <= visibleRange.to) {
282
+ const line = view.state.doc.lineAt(position);
283
+ if (!decoratedLines.has(line.from)) {
284
+ const indent = getWrappedLineIndent(line.text);
285
+ decorations.push(Decoration.line({
286
+ attributes: { style: `--fractal-code-wrap-indent: ${indent}ch` }
287
+ }).range(line.from));
288
+ decoratedLines.add(line.from);
289
+ }
290
+ if (line.to >= visibleRange.to) break;
291
+ position = line.to + 1;
292
+ }
293
+ }
294
+ return Decoration.set(decorations, true);
295
+ }
296
+ var wrappedLineIndentation = ViewPlugin.fromClass(class {
297
+ constructor(view) {
298
+ this.decorations = buildWrappedLineIndentation(view);
299
+ }
300
+ update(update) {
301
+ this.decorations = buildWrappedLineIndentation(update.view);
302
+ }
303
+ }, {
304
+ decorations: (plugin) => plugin.decorations
305
+ });
255
306
  function formatJsonLinesForDisplay(value) {
256
307
  return value.split(/\r?\n/).map((line) => {
257
308
  const firstContentIndex = line.search(/\S/);
@@ -367,7 +418,7 @@ function CodeBlock({
367
418
  ...resolvedLanguage.support ? [resolvedLanguage.support] : [],
368
419
  ...resolvedLanguage.isJsonLines ? [jsonLinesFoldSupport] : [],
369
420
  ...lineNumbers ? [foldGutter(), lineNumberGutter()] : [],
370
- ...wrap ? [EditorView.lineWrapping] : []
421
+ ...wrap ? [EditorView.lineWrapping, wrappedLineIndentation] : []
371
422
  ], [lineNumbers, resolvedLanguage.isJsonLines, resolvedLanguage.support, wrap]);
372
423
  const languageLabel = resolvedLanguage.label;
373
424
  const toolbarLabel = label ?? fileName ?? languageLabel;