@mirror-physics/fractal-ui 0.2.0-next.51 → 0.2.0-next.52

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
@@ -168,7 +168,7 @@ Button.displayName = "Button";
168
168
  // src/components/CodeBlock/CodeBlock.tsx
169
169
  import { useEffect, useMemo, useRef, useState as useState2 } from "react";
170
170
  import CodeMirror from "@uiw/react-codemirror";
171
- import { HighlightStyle, foldGutter, syntaxHighlighting } from "@codemirror/language";
171
+ import { HighlightStyle, foldGutter, foldService, syntaxHighlighting } from "@codemirror/language";
172
172
  import { json } from "@codemirror/lang-json";
173
173
  import { EditorView, lineNumbers as lineNumberGutter } from "@codemirror/view";
174
174
  import { tags } from "@lezer/highlight";
@@ -249,16 +249,40 @@ var fractalHighlightStyle = HighlightStyle.define([
249
249
  ]);
250
250
  var jsonSupport = json();
251
251
  var JSON_LANGUAGE_NAMES = /* @__PURE__ */ new Set(["json", "jsonl", "ndjson", "geojson"]);
252
+ var JSON_LINES_LANGUAGE_NAMES = /* @__PURE__ */ new Set(["jsonl", "ndjson"]);
253
+ var jsonLinesFoldSupport = foldService.of((state, lineStart, lineEnd) => {
254
+ const line = state.doc.sliceString(lineStart, lineEnd);
255
+ const firstContentIndex = line.search(/\S/);
256
+ if (firstContentIndex < 0) return null;
257
+ const lastContentIndex = line.search(/\s*$/) - 1;
258
+ const openingCharacter = line[firstContentIndex];
259
+ const closingCharacter = line[lastContentIndex];
260
+ const isContainer = openingCharacter === "{" && closingCharacter === "}" || openingCharacter === "[" && closingCharacter === "]";
261
+ if (!isContainer || lastContentIndex - firstContentIndex < 2) return null;
262
+ try {
263
+ const parsed = JSON.parse(line.slice(firstContentIndex, lastContentIndex + 1));
264
+ if (parsed === null || typeof parsed !== "object") return null;
265
+ } catch {
266
+ return null;
267
+ }
268
+ return {
269
+ from: lineStart + firstContentIndex + 1,
270
+ to: lineStart + lastContentIndex
271
+ };
272
+ });
252
273
  function resolveLanguage(language, fileName) {
253
274
  const normalized = language?.trim().toLowerCase();
254
275
  const extension = fileName?.split(/[?#]/, 1)[0].split(".").pop()?.toLowerCase();
255
276
  if (normalized && JSON_LANGUAGE_NAMES.has(normalized) || extension && JSON_LANGUAGE_NAMES.has(extension)) {
256
- return { label: normalized === "jsonl" || extension === "jsonl" ? "JSONL" : "JSON", support: jsonSupport };
277
+ const isJsonLines = Boolean(
278
+ normalized && JSON_LINES_LANGUAGE_NAMES.has(normalized) || extension && JSON_LINES_LANGUAGE_NAMES.has(extension)
279
+ );
280
+ return { label: isJsonLines ? "JSONL" : "JSON", support: jsonSupport, isJsonLines };
257
281
  }
258
282
  if (normalized && !["text", "txt", "plaintext", "plain"].includes(normalized)) {
259
- return { label: language?.trim(), support: null };
283
+ return { label: language?.trim(), support: null, isJsonLines: false };
260
284
  }
261
- return { support: null };
285
+ return { support: null, isJsonLines: false };
262
286
  }
263
287
  function CodeBlock({
264
288
  value,
@@ -289,9 +313,10 @@ function CodeBlock({
289
313
  codeTheme,
290
314
  syntaxHighlighting(fractalHighlightStyle),
291
315
  ...resolvedLanguage.support ? [resolvedLanguage.support] : [],
316
+ ...resolvedLanguage.isJsonLines ? [jsonLinesFoldSupport] : [],
292
317
  ...lineNumbers ? [foldGutter(), lineNumberGutter()] : [],
293
318
  ...wrap ? [EditorView.lineWrapping] : []
294
- ], [lineNumbers, resolvedLanguage.support, wrap]);
319
+ ], [lineNumbers, resolvedLanguage.isJsonLines, resolvedLanguage.support, wrap]);
295
320
  const languageLabel = resolvedLanguage.label;
296
321
  const toolbarLabel = label ?? fileName ?? languageLabel;
297
322
  const surfaceLabel = ariaLabel ?? (fileName ? `${fileName} ${editable ? "editor" : "viewer"}` : `Code ${editable ? "editor" : "viewer"}`);