@simple-mark/codemirror 0.3.0 → 0.3.1

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 (2) hide show
  1. package/index.js +42 -21
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -128,7 +128,7 @@ function lookCss(look, theme) {
128
128
  css.push(`text-decoration: ${lines.join(" ") || "none"}`);
129
129
  }
130
130
  if (look.lineBackground !== void 0) css.push(`background-color: ${look.lineBackground}`);
131
- if (look.borderLeft !== void 0) css.push(`box-shadow: inset 3px 0 0 ${look.borderLeft}`, "padding-left: 12px");
131
+ if (look.borderLeft !== void 0) css.push(`box-shadow: inset ${3 / 15}em 0 0 ${look.borderLeft}`, `padding-left: ${12 / 15}em`);
132
132
  return css.join("; ");
133
133
  }
134
134
  const fontChecks = /* @__PURE__ */ new Map();
@@ -289,8 +289,26 @@ function loadMath() {
289
289
  function loadedMath() {
290
290
  return loaded;
291
291
  }
292
- /** Gap between images on one line (CSS gap). */
293
- const IMAGE_GAP = 8;
292
+ //#endregion
293
+ //#region packages/codemirror/src/preview-heights.ts
294
+ /**
295
+ * Preview sizes in em of the editor's font: at 15px, 4px above and 8px below a preview, images
296
+ * at most 320px tall and 8px apart. The same numbers as @simple-mark/canvas, so a canvas and the
297
+ * editor agree at any font size (a canvas text may be 48 units tall).
298
+ */
299
+ const PREVIEW_EM = {
300
+ top: 4 / 15,
301
+ bottom: 8 / 15,
302
+ imageMaxHeight: 320 / 15,
303
+ imageGap: 8 / 15,
304
+ radius: 4 / 15
305
+ };
306
+ /** Vertical padding of a preview in pixels, at the editor's font size. */
307
+ function previewPadding() {
308
+ return (PREVIEW_EM.top + PREVIEW_EM.bottom) * layout.fontSize;
309
+ }
310
+ const imageMaxHeight = () => PREVIEW_EM.imageMaxHeight * layout.fontSize;
311
+ const imageGap = () => PREVIEW_EM.imageGap * layout.fontSize;
294
312
  /** Editor facts the estimates depend on, measured by `previewLayout`. */
295
313
  const layout = {
296
314
  contentWidth: 700,
@@ -375,7 +393,7 @@ function whenImageSized(src, then) {
375
393
  /** Gives an <img> its final layout size before its pixels arrive (CSS caps it at the width). */
376
394
  function reserveImageBox(img, size) {
377
395
  img.style.aspectRatio = `${size.width} / ${size.height}`;
378
- img.style.width = `${Math.min(size.width, 320 * size.width / size.height)}px`;
396
+ img.style.width = `${Math.min(size.width, imageMaxHeight() * size.width / size.height)}px`;
379
397
  }
380
398
  function probeImage(src) {
381
399
  if (probing.has(src) || imageSizes.has(src) || typeof Image === "undefined") return;
@@ -384,7 +402,7 @@ function probeImage(src) {
384
402
  watchImage(img, src);
385
403
  img.src = src;
386
404
  }
387
- /** Displayed size of one image: fits the width, at most IMAGE_MAX_HEIGHT tall, never enlarged. */
405
+ /** Displayed size of one image: fits the width, at most `imageMaxHeight` tall, never enlarged. */
388
406
  function imageBox(src) {
389
407
  const errorLine = {
390
408
  width: layout.contentWidth,
@@ -396,11 +414,11 @@ function imageBox(src) {
396
414
  probeImage(src);
397
415
  return {
398
416
  width: layout.contentWidth,
399
- height: 160
417
+ height: imageMaxHeight() / 2
400
418
  };
401
419
  }
402
420
  if (size === null) return errorLine;
403
- const scale = Math.min(1, layout.contentWidth / size.width, 320 / size.height);
421
+ const scale = Math.min(1, layout.contentWidth / size.width, imageMaxHeight() / size.height);
404
422
  return {
405
423
  width: size.width * scale,
406
424
  height: size.height * scale
@@ -415,15 +433,15 @@ function imagesEstimate(key, srcs) {
415
433
  let rowHeight = 0;
416
434
  for (const src of srcs) {
417
435
  const box = imageBox(src);
418
- if (rowWidth > 0 && rowWidth + IMAGE_GAP + box.width > layout.contentWidth) {
419
- total += rowHeight + IMAGE_GAP;
436
+ if (rowWidth > 0 && rowWidth + imageGap() + box.width > layout.contentWidth) {
437
+ total += rowHeight + imageGap();
420
438
  rowWidth = 0;
421
439
  rowHeight = 0;
422
440
  }
423
- rowWidth += (rowWidth > 0 ? IMAGE_GAP : 0) + box.width;
441
+ rowWidth += (rowWidth > 0 ? imageGap() : 0) + box.width;
424
442
  rowHeight = Math.max(rowHeight, box.height);
425
443
  }
426
- return Math.round(total + rowHeight + 12);
444
+ return Math.round(total + rowHeight + previewPadding());
427
445
  }
428
446
  /** Renders queued formulas one at a time, so their heights are known before they are shown. */
429
447
  function renderQueuedMath() {
@@ -449,13 +467,13 @@ function mathEstimate(key, tex) {
449
467
  const result = loadedMath()?.cachedMath(tex, true);
450
468
  if (result?.svg !== void 0) {
451
469
  const height = /height="([\d.]+)ex"/.exec(result.svg);
452
- if (height) return Math.round(parseFloat(height[1]) * layout.exPx + 12);
453
- } else if (result?.error !== void 0) return layout.lineHeight + 12;
470
+ if (height) return Math.round(parseFloat(height[1]) * layout.exPx + previewPadding());
471
+ } else if (result?.error !== void 0) return layout.lineHeight + previewPadding();
454
472
  else {
455
473
  mathQueue.add(tex);
456
474
  renderQueuedMath();
457
475
  }
458
- return 3 * layout.lineHeight + 12;
476
+ return 3 * layout.lineHeight + previewPadding();
459
477
  }
460
478
  /** Measures the editor (content width, x-height, line height) and refreshes previews on news. */
461
479
  const previewLayout = ViewPlugin.fromClass(class {
@@ -649,7 +667,7 @@ var MathBlockWidget = class extends WidgetType {
649
667
  const dom = document.createElement("div");
650
668
  dom.className = "sm-math-preview";
651
669
  dom.dataset["key"] = this.key;
652
- dom.style.minHeight = `${this.estimate - 12}px`;
670
+ dom.style.minHeight = `${this.estimate - previewPadding()}px`;
653
671
  renderInto(dom, this.tex, true, () => {
654
672
  dom.style.minHeight = "";
655
673
  view.requestMeasure();
@@ -752,7 +770,7 @@ var ImageWidget = class extends WidgetType {
752
770
  settled();
753
771
  };
754
772
  }
755
- if (unsized > 0) dom.style.minHeight = `${this.estimate - 12}px`;
773
+ if (unsized > 0) dom.style.minHeight = `${this.estimate - previewPadding()}px`;
756
774
  if (waiting === 0) rememberHeight(view, this.key, dom);
757
775
  return dom;
758
776
  }
@@ -823,17 +841,20 @@ const hoverMathTooltip = hoverTooltip((view, pos) => {
823
841
  const tooltip = mathTooltip(view.state, pos);
824
842
  return tooltip && cursor?.pos === tooltip.pos ? null : tooltip;
825
843
  });
844
+ const em = (value) => `${value}em`;
845
+ const previewSpacing = `${em(PREVIEW_EM.top)} 0 ${em(PREVIEW_EM.bottom)}`;
846
+ /** Sizes in em (see PREVIEW_EM), so previews scale with the text like on a canvas. */
826
847
  const previewTheme = EditorView.theme({
827
848
  ".sm-image-preview": {
828
849
  display: "flex",
829
850
  flexWrap: "wrap",
830
- gap: "8px",
831
- padding: "4px 0 8px"
851
+ gap: em(PREVIEW_EM.imageGap),
852
+ padding: previewSpacing
832
853
  },
833
854
  ".sm-image-preview img": {
834
855
  maxWidth: "100%",
835
- maxHeight: "320px",
836
- borderRadius: "4px"
856
+ maxHeight: em(PREVIEW_EM.imageMaxHeight),
857
+ borderRadius: em(PREVIEW_EM.radius)
837
858
  },
838
859
  ".sm-image-error, .sm-math-error": {
839
860
  color: "#cf222e",
@@ -841,7 +862,7 @@ const previewTheme = EditorView.theme({
841
862
  },
842
863
  ".sm-math-preview": {
843
864
  textAlign: "center",
844
- padding: "4px 0 8px",
865
+ padding: previewSpacing,
845
866
  overflowX: "auto",
846
867
  overflowY: "hidden"
847
868
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simple-mark/codemirror",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "CodeMirror 6 editor that styles Markdown without hiding its syntax.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -28,7 +28,7 @@
28
28
  "@codemirror/state": "^6.7.6",
29
29
  "@codemirror/view": "^6.43.15",
30
30
  "@lezer/common": "^1.5.3",
31
- "@simple-mark/math": "^0.3.0",
32
- "@simple-mark/model": "^0.3.0"
31
+ "@simple-mark/math": "^0.3.1",
32
+ "@simple-mark/model": "^0.3.1"
33
33
  }
34
34
  }