@stll/folio-core 0.42.0 → 0.44.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 (206) hide show
  1. package/dist/ai-edits/headless.js +6 -5
  2. package/dist/ai-edits/index.d.ts +2 -2
  3. package/dist/ai-edits/index.js +2 -2
  4. package/dist/ai-edits/snapshot.js +13 -9
  5. package/dist/compare/content-alignment.js +16 -1
  6. package/dist/compare/inline-atoms.js +1 -1
  7. package/dist/compare/style-resources.js +6 -0
  8. package/dist/compat/eigenpal.d.ts +2 -2
  9. package/dist/controller/layoutPipeline.d.ts +2 -1
  10. package/dist/controller/layoutPipeline.js +5 -2
  11. package/dist/controller/layoutSession.d.ts +2 -1
  12. package/dist/controller/layoutSession.js +1 -0
  13. package/dist/docx/appVersionNormalization.d.ts +0 -18
  14. package/dist/docx/blockContentParser.js +10 -1
  15. package/dist/docx/blockRangeMarkers.d.ts +36 -0
  16. package/dist/docx/blockRangeMarkers.js +59 -0
  17. package/dist/docx/bookmarkParser.d.ts +2 -20
  18. package/dist/docx/bookmarkParser.js +6 -30
  19. package/dist/docx/borderParser.d.ts +13 -0
  20. package/dist/docx/borderParser.js +71 -0
  21. package/dist/docx/builtInStyles.d.ts +165 -0
  22. package/dist/docx/builtInStyles.js +239 -0
  23. package/dist/docx/commentIdNormalization.d.ts +10 -0
  24. package/dist/docx/commentIdNormalization.js +33 -0
  25. package/dist/docx/commentParser.d.ts +2 -1
  26. package/dist/docx/commentParser.js +34 -7
  27. package/dist/docx/commentReferenceNormalization.d.ts +4 -1
  28. package/dist/docx/commentReferenceNormalization.js +23 -14
  29. package/dist/docx/danglingRelationshipReferences.d.ts +15 -0
  30. package/dist/docx/danglingRelationshipReferences.js +30 -0
  31. package/dist/docx/defaultParagraphStyle.d.ts +39 -0
  32. package/dist/docx/defaultParagraphStyle.js +54 -0
  33. package/dist/docx/documentParser.d.ts +2 -1
  34. package/dist/docx/documentParser.js +2 -2
  35. package/dist/docx/drawingUtils.d.ts +8 -1
  36. package/dist/docx/drawingUtils.js +12 -3
  37. package/dist/docx/fieldParser.js +3 -5
  38. package/dist/docx/footnoteParser.d.ts +3 -2
  39. package/dist/docx/footnoteParser.js +19 -2
  40. package/dist/docx/groupDrawingParser.js +1 -1
  41. package/dist/docx/headerFooterRefParser.d.ts +15 -3
  42. package/dist/docx/headerFooterRefParser.js +51 -14
  43. package/dist/docx/headerFooterReferenceNormalization.d.ts +4 -1
  44. package/dist/docx/headerFooterReferenceNormalization.js +5 -1
  45. package/dist/docx/hyperlinkParser.js +11 -15
  46. package/dist/docx/imageParser.d.ts +1 -1
  47. package/dist/docx/imageParser.js +22 -18
  48. package/dist/docx/imageRawXml.js +5 -5
  49. package/dist/docx/markupRangeMarker.d.ts +15 -0
  50. package/dist/docx/markupRangeMarker.js +44 -0
  51. package/dist/docx/noteReferenceStyles.d.ts +29 -0
  52. package/dist/docx/noteReferenceStyles.js +70 -0
  53. package/dist/docx/numberingParser.js +2 -1
  54. package/dist/docx/numberingReference.d.ts +21 -0
  55. package/dist/docx/numberingReference.js +21 -0
  56. package/dist/docx/numberingReferenceNormalization.d.ts +14 -2
  57. package/dist/docx/numberingReferenceNormalization.js +51 -9
  58. package/dist/docx/paraIdRangeNormalization.d.ts +0 -19
  59. package/dist/docx/paragraphParser.js +69 -101
  60. package/dist/docx/paragraphPropertySource.js +1 -0
  61. package/dist/docx/paragraphTraversal.d.ts +37 -1
  62. package/dist/docx/paragraphTraversal.js +84 -1
  63. package/dist/docx/parseContext.d.ts +37 -0
  64. package/dist/docx/parseContext.js +67 -0
  65. package/dist/docx/parseWarningMessage.d.ts +6 -0
  66. package/dist/docx/parseWarningMessage.js +44 -0
  67. package/dist/docx/parser.js +86 -24
  68. package/dist/docx/relsParser.d.ts +28 -11
  69. package/dist/docx/relsParser.js +26 -13
  70. package/dist/docx/revisionIdNormalization.js +81 -7
  71. package/dist/docx/rezip.js +85 -27
  72. package/dist/docx/runConsolidator.js +1 -2
  73. package/dist/docx/runParser.d.ts +8 -1
  74. package/dist/docx/runParser.js +30 -48
  75. package/dist/docx/sectionParser.d.ts +2 -1
  76. package/dist/docx/sectionParser.js +21 -65
  77. package/dist/docx/serializer/borderSerializer.d.ts +1 -2
  78. package/dist/docx/serializer/commentSerializer.js +22 -9
  79. package/dist/docx/serializer/documentSerializer.d.ts +1 -5
  80. package/dist/docx/serializer/documentSerializer.js +6 -16
  81. package/dist/docx/serializer/headerFooterSerializer.js +5 -0
  82. package/dist/docx/serializer/markupRangeAttributes.d.ts +8 -0
  83. package/dist/docx/serializer/markupRangeAttributes.js +24 -0
  84. package/dist/docx/serializer/noteSerializer.js +5 -0
  85. package/dist/docx/serializer/paragraphSerializer.d.ts +1 -5
  86. package/dist/docx/serializer/paragraphSerializer.js +29 -35
  87. package/dist/docx/serializer/runSerializer.js +13 -7
  88. package/dist/docx/serializer/tableSerializer.js +28 -13
  89. package/dist/docx/serializer/textFormattingSerializer.d.ts +2 -3
  90. package/dist/docx/server/build.js +8 -1
  91. package/dist/docx/server/createBilingualDocument.js +15 -22
  92. package/dist/docx/server/extractDocxText.js +3 -4
  93. package/dist/docx/shadingParser.d.ts +6 -0
  94. package/dist/docx/shadingParser.js +32 -0
  95. package/dist/docx/shapeParser.js +3 -3
  96. package/dist/docx/styleParser.js +15 -89
  97. package/dist/docx/styleReferenceResolution.d.ts +36 -0
  98. package/dist/docx/styleReferenceResolution.js +51 -0
  99. package/dist/docx/tableLook.d.ts +57 -0
  100. package/dist/docx/tableLook.js +63 -0
  101. package/dist/docx/tableParser.d.ts +7 -9
  102. package/dist/docx/tableParser.js +64 -110
  103. package/dist/docx/textBoxParser.js +4 -4
  104. package/dist/docx/trackedMoveRangeNormalization.d.ts +3 -1
  105. package/dist/docx/trackedMoveRangeNormalization.js +11 -21
  106. package/dist/docx/transitionalSpelling.d.ts +13 -2
  107. package/dist/docx/transitionalSpelling.js +23 -1
  108. package/dist/docx/verbatimCapture.js +4 -11
  109. package/dist/docx/vmlImageParser.js +2 -2
  110. package/dist/docx/watermarkParser.js +2 -2
  111. package/dist/docx/xmlParser.d.ts +22 -32
  112. package/dist/docx/xmlParser.js +36 -21
  113. package/dist/index.d.ts +2 -2
  114. package/dist/internal/pageBreakRunSourceDescendantIndex.d.ts +2 -0
  115. package/dist/internal/pageBreakRunSourceDescendantIndex.js +9 -6
  116. package/dist/internal/paragraphFormattingSerialization.d.ts +2 -3
  117. package/dist/internal/paragraphFormattingSerialization.js +26 -6
  118. package/dist/layout-bridge/convert/footnoteLayout.js +2 -7
  119. package/dist/layout-bridge/convert/templatePreviewFlow.d.ts +19 -11
  120. package/dist/layout-bridge/convert/templatePreviewFlow.js +103 -38
  121. package/dist/layout-bridge/convert/toFlowBlocks.js +12 -3
  122. package/dist/layout-engine/index.d.ts +2 -2
  123. package/dist/layout-engine/index.js +2 -2
  124. package/dist/layout-engine/measure/measureBlocks.js +1 -6
  125. package/dist/layout-engine/types.d.ts +8 -2
  126. package/dist/layout-engine/types.js +35 -2
  127. package/dist/markdown/index.js +1 -1
  128. package/dist/markdown/internals.d.ts +6 -1
  129. package/dist/markdown/internals.js +14 -1
  130. package/dist/markdown/renderBlock.js +35 -21
  131. package/dist/markdown/renderParagraph.js +14 -5
  132. package/dist/markdown/renderRuns.js +4 -3
  133. package/dist/markdown/renderTable.js +4 -3
  134. package/dist/markdown/trailers.js +41 -7
  135. package/dist/markdown/types.d.ts +3 -7
  136. package/dist/prosemirror/attrs/index.js +2 -5
  137. package/dist/prosemirror/bookmarkBoundaryAttrs.d.ts +11 -1
  138. package/dist/prosemirror/bookmarkBoundaryAttrs.js +18 -3
  139. package/dist/prosemirror/commands/index.d.ts +3 -3
  140. package/dist/prosemirror/commands/index.js +2 -2
  141. package/dist/prosemirror/commands/pageBreak.js +12 -1
  142. package/dist/prosemirror/commands/paragraph.d.ts +3 -3
  143. package/dist/prosemirror/commands/paragraph.js +2 -2
  144. package/dist/prosemirror/commentIdAllocator.js +2 -7
  145. package/dist/prosemirror/conversion/fromProseDoc.js +131 -41
  146. package/dist/prosemirror/conversion/toProseDoc.d.ts +1 -14
  147. package/dist/prosemirror/conversion/toProseDoc.js +402 -328
  148. package/dist/prosemirror/extensions/core/ParagraphExtension.d.ts +14 -1
  149. package/dist/prosemirror/extensions/core/ParagraphExtension.js +11 -6
  150. package/dist/prosemirror/extensions/features/EmptyParagraphFormatExtension.js +3 -3
  151. package/dist/prosemirror/extensions/features/ListExtension.js +42 -4
  152. package/dist/prosemirror/extensions/features/PasteCleanupExtension.d.ts +4 -1
  153. package/dist/prosemirror/extensions/features/PasteCleanupExtension.js +6 -2
  154. package/dist/prosemirror/extensions/features/pastedHeadingStyles.d.ts +7 -0
  155. package/dist/prosemirror/extensions/features/pastedHeadingStyles.js +74 -0
  156. package/dist/prosemirror/extensions/marks/markUtils.d.ts +11 -3
  157. package/dist/prosemirror/extensions/marks/markUtils.js +98 -19
  158. package/dist/prosemirror/extensions/nodes/BookmarkBoundaryExtension.js +7 -3
  159. package/dist/prosemirror/extensions/nodes/ImageExtension.js +2 -1
  160. package/dist/prosemirror/extensions/nodes/ShapeExtension.js +1 -0
  161. package/dist/prosemirror/extensions/nodes/TableExtension.js +15 -1
  162. package/dist/prosemirror/extensions/types.d.ts +2 -2
  163. package/dist/prosemirror/index.d.ts +3 -3
  164. package/dist/prosemirror/index.js +3 -3
  165. package/dist/prosemirror/insertOperations.d.ts +9 -2
  166. package/dist/prosemirror/insertOperations.js +9 -4
  167. package/dist/prosemirror/listMarker.js +2 -1
  168. package/dist/prosemirror/numberedRefFields.js +2 -1
  169. package/dist/prosemirror/pageBreakRunProjection.d.ts +11 -3
  170. package/dist/prosemirror/pageBreakRunProjection.js +16 -8
  171. package/dist/prosemirror/paragraphFormattingProvenance.d.ts +159 -0
  172. package/dist/prosemirror/paragraphFormattingProvenance.js +106 -0
  173. package/dist/prosemirror/plugins/documentStyles.d.ts +9 -1
  174. package/dist/prosemirror/plugins/documentStyles.js +11 -1
  175. package/dist/prosemirror/plugins/index.d.ts +2 -2
  176. package/dist/prosemirror/plugins/index.js +2 -2
  177. package/dist/prosemirror/plugins/revisionIds.d.ts +11 -2
  178. package/dist/prosemirror/plugins/revisionIds.js +21 -6
  179. package/dist/prosemirror/plugins/templatePreviewValues.d.ts +42 -1
  180. package/dist/prosemirror/plugins/templatePreviewValues.js +217 -14
  181. package/dist/prosemirror/runFormattingReconciliation.js +3 -2
  182. package/dist/prosemirror/runStyleFormatting.d.ts +1 -1
  183. package/dist/prosemirror/schema/nodes.d.ts +31 -0
  184. package/dist/prosemirror/styles/resolvedStyleAttrs.js +4 -1
  185. package/dist/prosemirror/styles/styleResolver.d.ts +9 -0
  186. package/dist/prosemirror/styles/styleResolver.js +15 -6
  187. package/dist/prosemirror/utils/visualLineNavigation.d.ts +22 -2
  188. package/dist/prosemirror/utils/visualLineNavigation.js +80 -63
  189. package/dist/style-engine/styleEngine.d.ts +4 -1
  190. package/dist/style-engine/styleEngine.js +3 -0
  191. package/dist/style-sets/extract.js +35 -11
  192. package/dist/style-sets/stellaStyle.js +46 -39
  193. package/dist/style-sets/styleSetNormalization.d.ts +19 -0
  194. package/dist/style-sets/styleSetNormalization.js +99 -0
  195. package/dist/types/content.d.ts +2 -2
  196. package/dist/utils/createDocument.js +145 -20
  197. package/dist/utils/headingCollector.d.ts +8 -5
  198. package/dist/utils/headingCollector.js +23 -25
  199. package/dist/utils/tableOfContentsStyle.js +9 -2
  200. package/package.json +3 -2
  201. package/dist/docx/textWhitespace.d.ts +0 -4
  202. package/dist/docx/textWhitespace.js +0 -4
  203. package/dist/layout-bridge/engine/tableWidthUtils.d.ts +0 -6
  204. package/dist/layout-bridge/engine/tableWidthUtils.js +0 -25
  205. package/dist/markdown/headings.d.ts +0 -13
  206. package/dist/markdown/headings.js +0 -20
@@ -25,6 +25,19 @@ type TemplatePreviewValues = {
25
25
  values: Record<string, TemplatePreviewValue>;
26
26
  /** `highlighted` marks substitutions with the preview accent. */
27
27
  mode: "highlighted" | "plain";
28
+ /**
29
+ * `if` expression → whether its block applies. The key is the expression
30
+ * exactly as written between `{% if` and `%}`, trimmed; a tag that carries a
31
+ * filter chain (`{% if consented | checkbox | label("…") %}`) also answers to
32
+ * the bare path in front of the chain, so a host that keys by field path need
33
+ * not repeat the chain. An expression the map does not mention is left as
34
+ * authored. The host decides truthiness: folio evaluates nothing.
35
+ *
36
+ * `false` hides the block, opener through closer. `true` keeps the body and,
37
+ * in `plain` mode, drops the block's tag paragraphs so the reader sees what
38
+ * will be generated; `highlighted` mode keeps them.
39
+ */
40
+ conditions?: Record<string, boolean>;
28
41
  };
29
42
  /**
30
43
  * One `{{path}}` marker matched by an active preview value. Exposed on
@@ -41,9 +54,37 @@ type TemplatePreviewEntry = {
41
54
  /** The typed value displayed in place of the marker. */
42
55
  value: TemplatePreviewValue;
43
56
  };
57
+ /**
58
+ * One span the preview hides: a `false` block from its opener through its
59
+ * closer, or a single directive tag of a `true` block in `plain` mode. Exposed
60
+ * on the plugin state beside {@link TemplatePreviewEntry} so a surface that
61
+ * never sees PM decorations can drop the same spans.
62
+ */
63
+ type TemplatePreviewHiddenRange = {
64
+ /** Inclusive PM doc position of the span start. */
65
+ from: number;
66
+ /** Exclusive PM doc position of the span end. */
67
+ to: number;
68
+ /** The `if` expression of the block this span belongs to, as authored. */
69
+ expr: string;
70
+ };
71
+ /**
72
+ * Whether a hidden span swallows a block whole, i.e. covers every position of
73
+ * its content, leaving it nothing to render. `pmStart`/`pmEnd` are the block
74
+ * node's own positions, so its content is `[pmStart + 1, pmEnd - 1)`.
75
+ *
76
+ * The decoration path and the paged flow both drop a block on this predicate,
77
+ * so the two surfaces hide exactly the same blocks; a block only partly covered
78
+ * fails it and keeps its text, and the covered span alone is hidden.
79
+ */
80
+ declare const templatePreviewHidesWholeBlock: (range: TemplatePreviewHiddenRange, block: {
81
+ pmStart: number;
82
+ pmEnd: number;
83
+ }) => boolean;
44
84
  type TemplatePreviewState = {
45
85
  preview: TemplatePreviewValues | null;
46
86
  entries: TemplatePreviewEntry[];
87
+ hidden: TemplatePreviewHiddenRange[];
47
88
  decorationSet: DecorationSet;
48
89
  };
49
90
  declare const templatePreviewValuesKey: PluginKey<TemplatePreviewState>;
@@ -51,4 +92,4 @@ declare function createTemplatePreviewValuesPlugin(): Plugin<TemplatePreviewStat
51
92
  /** Push (or clear, with `null`) the live fill preview into the editor. */
52
93
  declare const setTemplatePreviewValues: (view: EditorView, preview: TemplatePreviewValues | null) => void;
53
94
  //#endregion
54
- export { TemplatePreviewEntry, TemplatePreviewSpan, TemplatePreviewValue, TemplatePreviewValues, createTemplatePreviewValuesPlugin, setTemplatePreviewValues, templatePreviewValueFingerprint, templatePreviewValueText, templatePreviewValuesKey };
95
+ export { TemplatePreviewEntry, TemplatePreviewHiddenRange, TemplatePreviewSpan, TemplatePreviewValue, TemplatePreviewValues, createTemplatePreviewValuesPlugin, setTemplatePreviewValues, templatePreviewHidesWholeBlock, templatePreviewValueFingerprint, templatePreviewValueText, templatePreviewValuesKey };
@@ -1,6 +1,7 @@
1
1
  import { scanDirectives } from "./templateDirectives.js";
2
2
  import { Plugin, PluginKey } from "prosemirror-state";
3
3
  import { Decoration, DecorationSet } from "prosemirror-view";
4
+ import { classifyMarker, isFieldPath } from "@stll/template-conditions";
4
5
  //#region src/prosemirror/plugins/templatePreviewValues.ts
5
6
  /** Concatenated plain text of a preview value. */
6
7
  const templatePreviewValueText = (value) => typeof value === "string" ? value : value.runs.map((run) => run.text).join("");
@@ -11,6 +12,20 @@ const templatePreviewValueFingerprint = (value) => {
11
12
  if (typeof value === "string") return value;
12
13
  return value.runs.map((run) => `${run.bold === true ? "b" : ""}${run.italic === true ? "i" : ""}:${run.text}`).join("\0");
13
14
  };
15
+ /**
16
+ * Whether a hidden span swallows a block whole, i.e. covers every position of
17
+ * its content, leaving it nothing to render. `pmStart`/`pmEnd` are the block
18
+ * node's own positions, so its content is `[pmStart + 1, pmEnd - 1)`.
19
+ *
20
+ * The decoration path and the paged flow both drop a block on this predicate,
21
+ * so the two surfaces hide exactly the same blocks; a block only partly covered
22
+ * fails it and keeps its text, and the covered span alone is hidden.
23
+ */
24
+ const templatePreviewHidesWholeBlock = (range, block) => range.from <= block.pmStart + 1 && range.to >= block.pmEnd - 1;
25
+ /** Hides the marker text a preview value stands in for. */
26
+ const ORIGINAL_CLASS = "folio-template-preview-original";
27
+ /** Hides a conditional block the host reported as not applying. */
28
+ const HIDDEN_CLASS = "folio-template-preview-hidden";
14
29
  const KEY_HOLDER_SYMBOL = Symbol.for("stll.folio.templatePreviewValuesKey");
15
30
  const keyHolder = globalThis;
16
31
  const templatePreviewValuesKey = keyHolder[KEY_HOLDER_SYMBOL] ?? (keyHolder[KEY_HOLDER_SYMBOL] = new PluginKey("templatePreviewValues"));
@@ -29,6 +44,13 @@ function buildSpanNode(span) {
29
44
  }
30
45
  return node;
31
46
  }
47
+ /**
48
+ * The widget standing in for a marker. A value carrying newlines breaks its
49
+ * lines inside the paragraph, not into new ones: here through the widget class's
50
+ * `white-space: pre-wrap`, and on the paged canvas through the line break runs
51
+ * `applyTemplatePreviewToBlocks` emits. The two must keep agreeing — an inline
52
+ * `{{ path }}` placeholder cannot introduce paragraphs on either surface.
53
+ */
32
54
  function buildValueWidget(value, mode) {
33
55
  return () => {
34
56
  const span = document.createElement("span");
@@ -42,13 +64,142 @@ function buildValueWidget(value, mode) {
42
64
  return span;
43
65
  };
44
66
  }
45
- function collectPreviewEntries(doc, preview) {
46
- if (!preview || Object.keys(preview.values).length === 0) return [];
67
+ /** Loop alias the bare-path probe binds. Any identifier does; it is discarded. */
68
+ const CHAIN_PROBE_ALIAS = "__folioCondition";
69
+ /** A field path opens with a letter or underscore, so `9x` and `-x` are not one. */
70
+ const FIELD_PATH_HEAD_RE = /^[\p{L}_]/u;
71
+ /**
72
+ * The bare field path a condition's filter chain hangs off, or undefined when
73
+ * the expression is not a path plus a chain.
74
+ *
75
+ * A host may write the tag the way `{% for x in xs | chain %}` already carries
76
+ * one — `{% if buyer_is_a_consumer | checkbox | ai("Is the buyer …") %}` — while
77
+ * keying `conditions` by the bare path. Splitting on `|` would cut a quoted
78
+ * argument such as `label("a | b")` in half, so the expression is classified as
79
+ * that very `for` chain instead and the grammar's own argument-aware scan
80
+ * reports the path. An expression that is not a path plus a chain of known
81
+ * filters (`a and b`, `items|length > 0`, `ai("a|b")`) classifies as nothing,
82
+ * which is what keeps a real expression on exact matching.
83
+ */
84
+ const filterChainPath = (expr) => {
85
+ const meta = classifyMarker(`for ${CHAIN_PROBE_ALIAS} in ${expr}`, "statement");
86
+ if (meta?.kind !== "for" || !isFieldPath(meta.path) || !FIELD_PATH_HEAD_RE.test(meta.path)) return;
87
+ return meta.path;
88
+ };
89
+ /**
90
+ * The verdict stored under one key. Only a key the host set itself, holding a
91
+ * real boolean, counts: an inherited property (`constructor`, or a boolean the
92
+ * host's prototype carries) and a value from an untyped host are read as
93
+ * silence rather than as a verdict.
94
+ */
95
+ const ownVerdict = (conditions, key) => {
96
+ if (!Object.hasOwn(conditions, key)) return;
97
+ const value = conditions[key];
98
+ return typeof value === "boolean" ? value : void 0;
99
+ };
100
+ /**
101
+ * The host's verdict on one condition: keyed by the expression as written, else
102
+ * by the bare path its filter chain hangs off. `undefined` means the host said
103
+ * nothing about this block, which leaves it exactly as authored.
104
+ */
105
+ const conditionVerdict = (conditions, expr) => {
106
+ const exact = ownVerdict(conditions, expr);
107
+ if (exact !== void 0) return exact;
108
+ const path = filterChainPath(expr);
109
+ return path === void 0 ? void 0 : ownVerdict(conditions, path);
110
+ };
111
+ /**
112
+ * The spans the host's verdicts hide, in document order and without their
113
+ * nested hidden blocks: hiding a block already hides everything inside it.
114
+ *
115
+ * A `false` block is hidden whole, opener through closer. A `true` block keeps
116
+ * its body, and in `plain` mode loses its tag paragraphs — opener, any
117
+ * `{% elif %}` / `{% else %}`, closer — so the reader sees the document as it
118
+ * will be generated rather than its scaffolding. `highlighted` mode exists to
119
+ * show that scaffolding, so it keeps the tags. A block no verdict mentions is
120
+ * left exactly as authored.
121
+ *
122
+ * Openers and closers pair off a kind-aware stack — `{% endif %}` closes the
123
+ * nearest open `{% if %}`, `{% endfor %}` the nearest `{% for %}` — so a
124
+ * mid-edit template with an unpaired opener or a stray closer hides nothing
125
+ * rather than guessing a span. Inline markers pair like block ones: the fill
126
+ * engine resolves an inline conditional within its paragraph, so the preview
127
+ * follows. An `{% elif %}` or `{% else %}` branch of a `false` block sits
128
+ * inside its span and is hidden with it.
129
+ */
130
+ function collectHiddenRanges(ranges, conditions, mode) {
131
+ if (conditions === void 0) return [];
132
+ const open = [];
133
+ const paired = [];
134
+ for (const range of [...ranges].sort((a, b) => a.from - b.from)) {
135
+ if (range.kind === "if" || range.kind === "for") {
136
+ open.push({
137
+ opener: range,
138
+ branches: []
139
+ });
140
+ continue;
141
+ }
142
+ if (range.kind === "elif" || range.kind === "else") {
143
+ const innermost = open.at(-1);
144
+ if (innermost?.opener.kind === "if") innermost.branches.push(range);
145
+ continue;
146
+ }
147
+ if (range.kind !== "endif" && range.kind !== "endfor") continue;
148
+ const wanted = range.kind === "endif" ? "if" : "for";
149
+ let block;
150
+ for (let index = open.length - 1; index >= 0; index -= 1) {
151
+ const candidate = open[index];
152
+ if (candidate?.opener.kind === wanted) {
153
+ block = candidate;
154
+ open.length = index;
155
+ break;
156
+ }
157
+ }
158
+ if (block === void 0 || block.opener.kind !== "if") continue;
159
+ const { opener, branches } = block;
160
+ const verdict = conditionVerdict(conditions, opener.expr);
161
+ if (verdict === void 0) continue;
162
+ if (!verdict) {
163
+ paired.push({
164
+ from: opener.from,
165
+ to: range.to,
166
+ expr: opener.expr
167
+ });
168
+ continue;
169
+ }
170
+ if (mode !== "plain") continue;
171
+ paired.push({
172
+ from: opener.from,
173
+ to: opener.to,
174
+ expr: opener.expr
175
+ });
176
+ for (const branch of branches) paired.push({
177
+ from: branch.from,
178
+ to: branch.to,
179
+ expr: opener.expr
180
+ });
181
+ paired.push({
182
+ from: range.from,
183
+ to: range.to,
184
+ expr: opener.expr
185
+ });
186
+ }
187
+ paired.sort((a, b) => a.from - b.from || b.to - a.to);
188
+ const outermost = [];
189
+ for (const range of paired) {
190
+ const previous = outermost.at(-1);
191
+ if (previous && range.to <= previous.to) continue;
192
+ outermost.push(range);
193
+ }
194
+ return outermost;
195
+ }
196
+ function collectPreviewEntries(ranges, values, hidden) {
47
197
  const entries = [];
48
- for (const range of scanDirectives(doc)) {
198
+ for (const range of ranges) {
49
199
  if (range.kind !== "placeholder" && range.kind !== "clause") continue;
50
- const value = preview.values[range.expr];
200
+ const value = values[range.expr];
51
201
  if (value === void 0 || templatePreviewValueText(value) === "") continue;
202
+ if (hidden.some((block) => range.from >= block.from && range.from < block.to)) continue;
52
203
  entries.push({
53
204
  from: range.from,
54
205
  to: range.to,
@@ -58,10 +209,59 @@ function collectPreviewEntries(doc, preview) {
58
209
  }
59
210
  return entries;
60
211
  }
61
- function buildDecorationSet(doc, entries, mode) {
62
- if (entries.length === 0) return DecorationSet.empty;
212
+ function projectPreview(doc, preview) {
213
+ if (!preview) return {
214
+ entries: [],
215
+ hidden: []
216
+ };
217
+ const hasValues = Object.keys(preview.values).length > 0;
218
+ const hasConditions = preview.conditions !== void 0 && Object.keys(preview.conditions).length > 0;
219
+ if (!hasValues && !hasConditions) return {
220
+ entries: [],
221
+ hidden: []
222
+ };
223
+ const ranges = scanDirectives(doc);
224
+ const hidden = collectHiddenRanges(ranges, preview.conditions, preview.mode);
225
+ return {
226
+ entries: hasValues ? collectPreviewEntries(ranges, preview.values, hidden) : [],
227
+ hidden
228
+ };
229
+ }
230
+ /**
231
+ * Hide one span: a block the span covers whole is hidden as a node, leaving no
232
+ * empty line behind; a block it covers in part is hidden over the covered text
233
+ * only, so an inline `{% if %}` in a paragraph of running text takes its own
234
+ * span and nothing else.
235
+ */
236
+ function pushHiddenDecorations(doc, range, out) {
237
+ const { from, to } = range;
238
+ doc.nodesBetween(from, to, (node, pos) => {
239
+ if (!node.isBlock) return false;
240
+ const nodeEnd = pos + node.nodeSize;
241
+ if (templatePreviewHidesWholeBlock(range, {
242
+ pmStart: pos,
243
+ pmEnd: nodeEnd
244
+ })) {
245
+ out.push(Decoration.node(pos, nodeEnd, { class: HIDDEN_CLASS }));
246
+ return false;
247
+ }
248
+ const contentFrom = pos + 1;
249
+ const contentTo = nodeEnd - 1;
250
+ if (!node.isTextblock) return true;
251
+ const spanFrom = Math.max(from, contentFrom);
252
+ const spanTo = Math.min(to, contentTo);
253
+ if (spanTo > spanFrom) out.push(Decoration.inline(spanFrom, spanTo, { class: HIDDEN_CLASS }, {
254
+ inclusiveStart: false,
255
+ inclusiveEnd: false
256
+ }));
257
+ return false;
258
+ });
259
+ }
260
+ function buildDecorationSet(doc, { entries, hidden }, mode) {
261
+ if (entries.length === 0 && hidden.length === 0) return DecorationSet.empty;
63
262
  const decorations = [];
64
- for (const entry of entries) decorations.push(Decoration.inline(entry.from, entry.to, { class: "folio-template-preview-original" }, {
263
+ for (const range of hidden) pushHiddenDecorations(doc, range, decorations);
264
+ for (const entry of entries) decorations.push(Decoration.inline(entry.from, entry.to, { class: ORIGINAL_CLASS }, {
65
265
  inclusiveStart: false,
66
266
  inclusiveEnd: false
67
267
  }), Decoration.widget(entry.from, buildValueWidget(entry.value, mode), {
@@ -80,25 +280,28 @@ function createTemplatePreviewValuesPlugin() {
80
280
  return {
81
281
  preview: null,
82
282
  entries: [],
283
+ hidden: [],
83
284
  decorationSet: DecorationSet.empty
84
285
  };
85
286
  },
86
287
  apply(tr, prev, _oldState, newState) {
87
288
  const meta = tr.getMeta(templatePreviewValuesKey);
88
289
  if (meta !== void 0) {
89
- const entries = collectPreviewEntries(newState.doc, meta.preview);
290
+ const projection = projectPreview(newState.doc, meta.preview);
90
291
  return {
91
292
  preview: meta.preview,
92
- entries,
93
- decorationSet: buildDecorationSet(newState.doc, entries, meta.preview?.mode ?? "plain")
293
+ entries: projection.entries,
294
+ hidden: projection.hidden,
295
+ decorationSet: buildDecorationSet(newState.doc, projection, meta.preview?.mode ?? "plain")
94
296
  };
95
297
  }
96
298
  if (tr.docChanged && prev.preview) {
97
- const entries = collectPreviewEntries(newState.doc, prev.preview);
299
+ const projection = projectPreview(newState.doc, prev.preview);
98
300
  return {
99
301
  preview: prev.preview,
100
- entries,
101
- decorationSet: buildDecorationSet(newState.doc, entries, prev.preview.mode)
302
+ entries: projection.entries,
303
+ hidden: projection.hidden,
304
+ decorationSet: buildDecorationSet(newState.doc, projection, prev.preview.mode)
102
305
  };
103
306
  }
104
307
  return prev;
@@ -114,4 +317,4 @@ const setTemplatePreviewValues = (view, preview) => {
114
317
  view.dispatch(view.state.tr.setMeta(templatePreviewValuesKey, { preview }));
115
318
  };
116
319
  //#endregion
117
- export { createTemplatePreviewValuesPlugin, setTemplatePreviewValues, templatePreviewValueFingerprint, templatePreviewValueText, templatePreviewValuesKey };
320
+ export { createTemplatePreviewValuesPlugin, setTemplatePreviewValues, templatePreviewHidesWholeBlock, templatePreviewValueFingerprint, templatePreviewValueText, templatePreviewValuesKey };
@@ -1,9 +1,10 @@
1
1
  import { mergeTextFormatting } from "../utils/textFormattingMerge.js";
2
2
  import { expectCharacterStyleMarkAttrs } from "./attrs/index.js";
3
3
  import { marksToTextFormatting } from "./conversion/fromProseDoc.js";
4
- import { textFormattingToMarks } from "./conversion/toProseDoc.js";
4
+ import { textFormattingToMarks } from "./extensions/marks/markUtils.js";
5
5
  import { RUN_FORMATTING_MARK_NAMES } from "./runFormattingMarkNames.js";
6
6
  import { getParagraphMarkSuppressionOverrides, hasDirectRunFormatting, paragraphFormattingForRun, resolveEffectiveRunStyleFormatting } from "./runStyleFormatting.js";
7
+ import { schema } from "./schema/index.js";
7
8
  import { Mark } from "prosemirror-model";
8
9
  //#region src/prosemirror/runFormattingReconciliation.ts
9
10
  /** Read authored run properties without confusing inherited visuals for direct formatting. */
@@ -36,7 +37,7 @@ const reconcileRunFormattingMarks = ({ authoredFormatting, context, node, styleR
36
37
  marks: styleResolutionMarks,
37
38
  paragraphFormatting,
38
39
  ...styleResolver !== void 0 ? { styleResolver } : {}
39
- }), authoredFormatting), {
40
+ }), authoredFormatting), schema, {
40
41
  overrideFormatting: mergeTextFormatting(getParagraphMarkSuppressionOverrides({
41
42
  directFormatting: authoredFormatting,
42
43
  paragraphMarkFormatting: context.paragraphMarkFormatting,
@@ -26,7 +26,7 @@ type ParagraphRunStyleContext = {
26
26
  paragraphMarkFormatting: document_d_exports.TextFormatting | undefined;
27
27
  paragraphMarkPrecedesStyle: boolean;
28
28
  };
29
- type RunStyleResolver = Pick<StyleEngine, "getDefaultCharacterStyle" | "getDefaultParagraphStyle" | "getDocDefaults" | "getRunStyleOwnProperties" | "getStyle" | "resolveParagraphStyle">;
29
+ type RunStyleResolver = Pick<StyleEngine, "builtInStyles" | "getDefaultCharacterStyle" | "getDefaultParagraphStyle" | "getDocDefaults" | "getRunStyleOwnProperties" | "getStyle" | "resolveParagraphStyle">;
30
30
  type ResolveParagraphBodyRunFormattingOptions = {
31
31
  styleId: string | undefined;
32
32
  styleResolver?: RunStyleResolver | null | undefined;
@@ -27,9 +27,11 @@ type BookmarkBoundaryAttrs = {
27
27
  name: string;
28
28
  colFirst?: number;
29
29
  colLast?: number;
30
+ displacedByCustomXml?: document_d_exports.DisplacedByCustomXml;
30
31
  } | {
31
32
  type: "end";
32
33
  id: number;
34
+ displacedByCustomXml?: document_d_exports.DisplacedByCustomXml;
33
35
  };
34
36
  /**
35
37
  * Paragraph node attributes - maps to ParagraphFormatting
@@ -189,6 +191,19 @@ type ParagraphAttrs = {
189
191
  /** Original inline paragraph formatting from DOCX (pre-style-resolution).
190
192
  * Used by fromProseDoc for lossless round-trip serialization. */
191
193
  _originalFormatting?: document_d_exports.ParagraphFormatting;
194
+ /**
195
+ * The `w:pPr` the style cascade resolves to for this paragraph: document
196
+ * defaults, the enclosing table style, then the `w:pStyle` chain. PM-only;
197
+ * never serialized.
198
+ *
199
+ * The formatting attrs above hold the EFFECTIVE value, because that is what
200
+ * the editor renders with. A save must write only what the paragraph states
201
+ * itself, so it needs this companion to tell an inherited value from an
202
+ * authored one — writing an inherited value back as direct `w:pPr` outranks
203
+ * the style it came from, and a later edit to that style stops reaching the
204
+ * paragraph. `TableCellAttrs._resolvedBorders` is the same device.
205
+ */
206
+ _resolvedFormatting?: document_d_exports.ParagraphFormatting;
192
207
  /** Import-effective spacing baseline for HTML auto-spacing detection.
193
208
  * PM-only; never serialized back into DOCX formatting. */
194
209
  _autospacingBase?: {
@@ -334,6 +349,14 @@ type ImageAttrs = {
334
349
  _docxRawImageFingerprint?: string;
335
350
  /** Embedded-object previews use their authored box as the exact line height. */
336
351
  _docxObjectPreview?: boolean;
352
+ /**
353
+ * The `w:rPr` of the run this atom came from. Inline atoms do not carry the
354
+ * run's formatting marks (see `withRunBoundaryMarks`), so without this the
355
+ * run properties of an embedded object, picture or shape are lost on save
356
+ * (`content[].content[].formatting: object became absent` in the corpus
357
+ * census).
358
+ */
359
+ _docxRunFormatting?: document_d_exports.TextFormatting;
337
360
  };
338
361
  /**
339
362
  * Field node attributes
@@ -507,6 +530,14 @@ type ShapeAttrs = {
507
530
  glowColor?: string;
508
531
  /** Glow radius in pixels */
509
532
  glowRadius?: number;
533
+ /**
534
+ * The `w:rPr` of the run this atom came from. Inline atoms do not carry the
535
+ * run's formatting marks (see `withRunBoundaryMarks`), so without this the
536
+ * run properties of an embedded object, picture or shape are lost on save
537
+ * (`content[].content[].formatting: object became absent` in the corpus
538
+ * census).
539
+ */
540
+ _docxRunFormatting?: document_d_exports.TextFormatting;
510
541
  };
511
542
  /**
512
543
  * Text box node attributes
@@ -1,8 +1,10 @@
1
1
  import { computeListRendering, numberingLevelHasMarkerSlot } from "../../docx/numberingParser.js";
2
+ import { isNumberingReference } from "../../docx/numberingReference.js";
2
3
  import { tableOfContentsStyleLevel } from "../../utils/tableOfContentsStyle.js";
3
4
  import { setAutospacingBaseValue } from "../autospacingBase.js";
4
5
  import { CLEARED_LIST_RENDERING_ATTRS } from "../listMarker.js";
5
6
  import { listRenderingAttrPatch } from "../listRenderingAttrs.js";
7
+ import { styleResolvedParagraphFormatting } from "../paragraphFormattingProvenance.js";
6
8
  //#region src/prosemirror/styles/resolvedStyleAttrs.ts
7
9
  /**
8
10
  * Shared helper for projecting a resolved paragraph style onto ProseMirror
@@ -44,6 +46,7 @@ function paragraphAttrsFromResolvedStyle(resolved, identity) {
44
46
  outlineLevel: ppr?.outlineLevel ?? null,
45
47
  borders: ppr?.borders ?? null,
46
48
  defaultTextFormatting: hasRunFormatting ? runFormatting : null,
49
+ _resolvedFormatting: styleResolvedParagraphFormatting(ppr),
47
50
  _autospacingBase: autospacingBaseFromResolvedParagraphFormatting(ppr),
48
51
  _tableOfContentsLevel: tableOfContentsStyleLevel(identity) ?? null
49
52
  };
@@ -74,7 +77,7 @@ function autospacingBaseFromResolvedParagraphFormatting(ppr) {
74
77
  */
75
78
  function listAttrsFromResolvedStyle(resolved, numbering) {
76
79
  const numPr = resolved.paragraphFormatting?.numPr;
77
- if (!numPr || numPr.numId === void 0 || numPr.numId === 0) return null;
80
+ if (!numPr || !isNumberingReference(numPr.numId)) return null;
78
81
  const attrs = listAttrsFromNumbering({
79
82
  numId: numPr.numId,
80
83
  ilvl: numPr.ilvl ?? 0
@@ -1,4 +1,5 @@
1
1
  import { document_d_exports } from "../../types/document.js";
2
+ import { BuiltInStyleIndex } from "../../docx/builtInStyles.js";
2
3
  //#region src/prosemirror/styles/styleResolver.d.ts
3
4
  /**
4
5
  * Resolved style properties ready for rendering
@@ -27,6 +28,7 @@ type TableCellParagraphSpacingOverlay = Pick<document_d_exports.ParagraphFormatt
27
28
  */
28
29
  declare class StyleResolver {
29
30
  private readonly stylesById;
31
+ private builtInStyleIndex;
30
32
  private readonly docDefaults;
31
33
  private readonly defaultParagraphStyle;
32
34
  private readonly defaultCharacterStyle;
@@ -36,6 +38,13 @@ declare class StyleResolver {
36
38
  * Get a style by ID
37
39
  */
38
40
  getStyle(styleId: string): document_d_exports.Style | undefined;
41
+ /**
42
+ * This document's styles indexed by the built-in they are, so a consumer can
43
+ * ask whether a paragraph is a heading without matching an English style id.
44
+ * Built on first use and kept: the resolver outlives a single query, and the
45
+ * outline sidebar rebuilds its heading list on every transaction.
46
+ */
47
+ get builtInStyles(): BuiltInStyleIndex;
39
48
  /**
40
49
  * Get all available paragraph styles (for toolbar dropdown)
41
50
  */
@@ -1,3 +1,5 @@
1
+ import { createBuiltInStyleIndex } from "../../docx/builtInStyles.js";
2
+ import { BUILT_IN_DEFAULT_PARAGRAPH_FORMATTING, resolveDefaultParagraphStyle } from "../../docx/defaultParagraphStyle.js";
1
3
  import { mergeParagraphFormatting } from "../../utils/paragraphFormattingMerge.js";
2
4
  import { cascadeStyleTextFormatting } from "./styleToggleCascade.js";
3
5
  //#region src/prosemirror/styles/styleResolver.ts
@@ -19,17 +21,14 @@ const BUILTIN_NORMAL_STYLE = {
19
21
  type: "paragraph",
20
22
  name: "Normal",
21
23
  default: true,
22
- pPr: {
23
- spaceAfter: 160,
24
- lineSpacing: 259,
25
- lineSpacingRule: "auto"
26
- }
24
+ pPr: BUILT_IN_DEFAULT_PARAGRAPH_FORMATTING
27
25
  };
28
26
  /**
29
27
  * StyleResolver provides efficient access to resolved style properties
30
28
  */
31
29
  var StyleResolver = class {
32
30
  stylesById;
31
+ builtInStyleIndex;
33
32
  docDefaults;
34
33
  defaultParagraphStyle;
35
34
  defaultCharacterStyle;
@@ -51,6 +50,16 @@ var StyleResolver = class {
51
50
  return this.stylesById.get(styleId);
52
51
  }
53
52
  /**
53
+ * This document's styles indexed by the built-in they are, so a consumer can
54
+ * ask whether a paragraph is a heading without matching an English style id.
55
+ * Built on first use and kept: the resolver outlives a single query, and the
56
+ * outline sidebar rebuilds its heading list on every transaction.
57
+ */
58
+ get builtInStyles() {
59
+ this.builtInStyleIndex ??= createBuiltInStyleIndex(this.stylesById.values(), this.docDefaults);
60
+ return this.builtInStyleIndex;
61
+ }
62
+ /**
54
63
  * Get all available paragraph styles (for toolbar dropdown)
55
64
  */
56
65
  getParagraphStyles() {
@@ -213,8 +222,8 @@ var StyleResolver = class {
213
222
  return this.stylesById.has(styleId);
214
223
  }
215
224
  findDefaultStyle(type) {
225
+ if (type === "paragraph") return resolveDefaultParagraphStyle(this.stylesById.values()) ?? (this.docDefaults ? void 0 : BUILTIN_NORMAL_STYLE);
216
226
  for (const style of this.stylesById.values()) if (style.type === type && style.default) return style;
217
- if (type === "paragraph") return this.stylesById.get("Normal") ?? (this.docDefaults ? void 0 : BUILTIN_NORMAL_STYLE);
218
227
  }
219
228
  };
220
229
  /**
@@ -1,9 +1,29 @@
1
1
  import { EditorView } from "prosemirror-view";
2
2
  //#region src/prosemirror/utils/visualLineNavigation.d.ts
3
+ /**
4
+ * Where the previous vertical step left the caret: the column later steps keep
5
+ * (`stickyX`), the visual line it settled on, and the caret position both
6
+ * describe. The line index is not redundant with the position — a soft-wrap
7
+ * boundary position belongs to two visual lines (the end of the wrapped line
8
+ * and the start of the next), so the painted DOM alone cannot say which one the
9
+ * caret sits on.
10
+ *
11
+ * @internal
12
+ */
13
+ type VisualLineStep = {
14
+ stickyX: number;
15
+ lineIndex: number;
16
+ pmPos: number;
17
+ };
3
18
  /** @internal */
4
19
  type VisualLineState = {
5
- stickyX: number | null;
6
- lastVisualLineIndex: number;
20
+ /**
21
+ * Only describes the caret while it is still at `step.pmPos`. Any other move
22
+ * (a click, a find result, an agent edit, typing) leaves the caret elsewhere,
23
+ * and the next vertical step re-resolves both column and line from the DOM
24
+ * instead of continuing from a line the caret has left.
25
+ */
26
+ step: VisualLineStep | null;
7
27
  };
8
28
  /** @internal */
9
29
  declare function createVisualLineState(): VisualLineState;