@jsb188/mday 1.1.11 → 1.1.12

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 (210) hide show
  1. package/package.json +58 -23
  2. package/src/constants/agentMemory.ts +14 -45
  3. package/src/constants/agentNote.ts +212 -0
  4. package/src/constants/aiChat.ts +5 -0
  5. package/src/constants/billing.ts +126 -0
  6. package/src/constants/campaign.ts +156 -47
  7. package/src/constants/contact.ts +72 -0
  8. package/src/constants/csvExport.ts +133 -0
  9. package/src/constants/dataTable.ts +12 -38
  10. package/src/constants/dataTableFieldTypes.generated.ts +3 -7
  11. package/src/constants/docAccess.ts +3 -2
  12. package/src/constants/dt.ts +234 -0
  13. package/src/constants/email.ts +114 -12
  14. package/src/constants/fieldDoc.ts +74 -4
  15. package/src/constants/graphqlReactivity.ts +108 -25
  16. package/src/constants/log.ts +9 -0
  17. package/src/constants/logDisplay/README.md +88 -0
  18. package/src/constants/logDisplay/defaults.ts +39 -0
  19. package/src/constants/logDisplay/index.ts +27 -0
  20. package/src/constants/logDisplay/operations/arable.ts +16 -0
  21. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  22. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  23. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  24. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  25. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  26. package/src/constants/logTaxonomy.generated.ts +1834 -730
  27. package/src/constants/organization.ts +13 -14
  28. package/src/constants/qrCode.ts +30 -0
  29. package/src/constants/sheet.ts +65 -101
  30. package/src/constants/sms.ts +18 -0
  31. package/src/constants/storage.ts +33 -9
  32. package/src/constants/usage.ts +3 -0
  33. package/src/constants/workflowSteps.ts +60 -0
  34. package/src/schemas/editLogEntry.ts +106 -283
  35. package/src/schemas/inboundContact.ts +5 -5
  36. package/src/types/agentMemory.d.ts +38 -14
  37. package/src/types/agentNote.d.ts +443 -0
  38. package/src/types/billing.d.ts +537 -0
  39. package/src/types/campaign.d.ts +1043 -193
  40. package/src/types/contact.d.ts +15 -0
  41. package/src/types/csvExport.d.ts +213 -0
  42. package/src/types/dataAnalysis.d.ts +44 -0
  43. package/src/types/dataTable.d.ts +102 -309
  44. package/src/types/definitionElement.d.ts +35 -0
  45. package/src/types/dt.d.ts +257 -0
  46. package/src/types/email.d.ts +144 -55
  47. package/src/types/emailReplyReport.d.ts +76 -0
  48. package/src/types/fieldDoc.d.ts +579 -150
  49. package/src/types/fieldDocContract.d.ts +90 -0
  50. package/src/types/inboundContact.d.ts +8 -0
  51. package/src/types/log.d.ts +94 -3
  52. package/src/types/logDisplay.types.ts +85 -0
  53. package/src/types/organization.d.ts +8 -3
  54. package/src/types/qrCode.d.ts +96 -0
  55. package/src/types/richText.d.ts +81 -0
  56. package/src/types/sheet.d.ts +180 -463
  57. package/src/types/sms.d.ts +38 -0
  58. package/src/types/storage.d.ts +266 -27
  59. package/src/types/workflow.d.ts +196 -66
  60. package/src/utils/agentMemory.ts +15 -0
  61. package/src/utils/agentNote.ts +850 -0
  62. package/src/utils/aiChat.ts +31 -14
  63. package/src/utils/aiChatWork.ts +64 -0
  64. package/src/utils/billing.ts +37 -0
  65. package/src/utils/campaign.ts +642 -209
  66. package/src/utils/campaignAI.ts +72 -0
  67. package/src/utils/campaignAccess.ts +239 -0
  68. package/src/utils/campaignEmail.ts +601 -0
  69. package/src/utils/campaignEmailCompiler.ts +372 -0
  70. package/src/utils/campaignEmailImages.ts +60 -0
  71. package/src/utils/campaignEmailStyles.ts +544 -0
  72. package/src/utils/campaignLifecycle.ts +104 -0
  73. package/src/utils/campaignPreview.ts +285 -0
  74. package/src/utils/contact.ts +50 -0
  75. package/src/utils/csvExport.ts +19 -0
  76. package/src/utils/dataTable.ts +18 -556
  77. package/src/utils/dataTableFieldTypeRegistry.ts +1 -0
  78. package/src/utils/dataTableRelativeDate.ts +447 -0
  79. package/src/utils/definitionElement.ts +84 -0
  80. package/src/utils/email.ts +208 -1
  81. package/src/utils/emailReplyReport.test.ts +187 -0
  82. package/src/utils/emailReplyReport.ts +143 -0
  83. package/src/utils/fieldDoc.ts +81 -37
  84. package/src/utils/fieldDocAnswers.ts +128 -0
  85. package/src/utils/fieldDocDefaults.ts +36 -0
  86. package/src/utils/fieldDocProgram.ts +36 -0
  87. package/src/utils/fieldDocSummary.ts +30 -0
  88. package/src/utils/log.ts +232 -141
  89. package/src/utils/logDisplay.helpers.ts +177 -0
  90. package/src/utils/logDisplay.ts +87 -0
  91. package/src/utils/logStoreRead.ts +9 -13
  92. package/src/utils/logTaxonomy.helpers.ts +17 -0
  93. package/src/utils/logTaxonomyDisplay.ts +80 -124
  94. package/src/utils/noteDoc.ts +66 -284
  95. package/src/utils/organization.ts +117 -283
  96. package/src/utils/qrCode.helpers.ts +189 -0
  97. package/src/utils/qrCode.test.ts +519 -0
  98. package/src/utils/qrCode.ts +273 -0
  99. package/src/utils/richTextHTML.ts +385 -0
  100. package/src/utils/sheet.ts +123 -4834
  101. package/src/utils/sheetAxisMove.ts +77 -291
  102. package/src/utils/sheetBootstrap.ts +771 -1138
  103. package/src/utils/sheetCellLiteral.ts +101 -0
  104. package/src/utils/sheetCommand.ts +23 -88
  105. package/src/utils/sheetDesign.ts +705 -0
  106. package/src/utils/sheetExport.ts +17 -69
  107. package/src/utils/sheetFeatureReference.ts +183 -0
  108. package/src/utils/sheetFormulaArgs.ts +168 -0
  109. package/src/utils/sheetFormulaCompile.ts +475 -0
  110. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  111. package/src/utils/sheetFormulaCriteria.ts +656 -0
  112. package/src/utils/sheetFormulaDate.ts +444 -0
  113. package/src/utils/sheetFormulaEvaluation.ts +388 -403
  114. package/src/utils/sheetFormulaEvaluator.ts +589 -444
  115. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  116. package/src/utils/sheetFormulaLogical.ts +116 -0
  117. package/src/utils/sheetFormulaLookup.ts +488 -0
  118. package/src/utils/sheetFormulaMath.ts +48 -0
  119. package/src/utils/sheetFormulaParser.ts +729 -0
  120. package/src/utils/sheetFormulaRange.ts +330 -0
  121. package/src/utils/sheetFormulaReference.ts +703 -0
  122. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  123. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  124. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  125. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  126. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  127. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  128. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  129. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  130. package/src/utils/sheetFormulaText.ts +430 -0
  131. package/src/utils/sheetJournal.ts +140 -18
  132. package/src/utils/sheetNumberFormat.ts +68 -267
  133. package/src/utils/sheetPreview.ts +68 -0
  134. package/src/utils/sheetSequence.ts +13 -43
  135. package/src/utils/sheetSnapshot.ts +176 -429
  136. package/src/utils/sheetStructureEdit.ts +537 -907
  137. package/src/utils/sheetStructureReferences.ts +389 -0
  138. package/src/utils/storage.ts +49 -3
  139. package/src/utils/time.ts +12 -0
  140. package/src/utils/workflow.ts +199 -73
  141. package/src/utils/workflowLogFingerprint.ts +302 -0
  142. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  143. package/src/utils/workflowProgress.ts +280 -0
  144. package/src/utils/workflowTriggers.ts +61 -0
  145. package/tests/agentNote.test.ts +395 -0
  146. package/tests/campaignAI.test.ts +119 -0
  147. package/tests/campaignAccess.test.ts +239 -0
  148. package/tests/campaignEmail.test.ts +441 -0
  149. package/tests/campaignLifecycle.test.ts +134 -0
  150. package/tests/campaignPublication.test.ts +309 -0
  151. package/tests/dataTableFieldTypeRegistry.test.ts +2 -1
  152. package/tests/dataTableRelativeDate.test.ts +412 -0
  153. package/tests/email.test.ts +39 -0
  154. package/tests/fieldDocProgram.test.ts +114 -0
  155. package/tests/logDisplay.fixtures.ts +67 -0
  156. package/tests/logDisplay.test.ts +227 -0
  157. package/tests/noteDoc.test.ts +37 -192
  158. package/tests/richTextHTML.test.ts +185 -0
  159. package/tests/sheetAxisMove.test.ts +49 -40
  160. package/tests/sheetBootstrap.test.ts +1967 -0
  161. package/tests/sheetFeatureReference.test.ts +22 -0
  162. package/tests/sheetFormulaCriteria.test.ts +269 -0
  163. package/tests/sheetFormulaEvaluator.test.ts +93 -200
  164. package/tests/sheetFormulaFunctions.test.ts +538 -0
  165. package/tests/sheetFormulaLanguage.test.ts +361 -0
  166. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  167. package/tests/sheetFormulaParity.test.ts +188 -0
  168. package/tests/sheetFormulaRange.test.ts +167 -0
  169. package/tests/sheetFormulaReference.test.ts +78 -0
  170. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  171. package/tests/sheetFormulaTextDate.test.ts +512 -0
  172. package/tests/sheetSequence.test.ts +4 -23
  173. package/tests/sheetSnapshot.test.ts +134 -0
  174. package/tests/sheetStructureEdit.test.ts +492 -37
  175. package/tests/sheetStructureReferences.test.ts +589 -0
  176. package/tests/workflowLifecycleStatus.test.ts +60 -0
  177. package/tests/workflowLogFingerprint.test.ts +203 -0
  178. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  179. package/tests/workflowProgress.test.ts +180 -0
  180. package/tests/workflowTriggers.test.ts +29 -0
  181. package/src/constants/agentDoc.ts +0 -12
  182. package/src/constants/logActivityGroupColors.generated.ts +0 -19
  183. package/src/constants/schedule.ts +0 -220
  184. package/src/constants/scheduleRuleTemplateSeed.ts +0 -138
  185. package/src/constants/scheduleRuleTemplates.generated.ts +0 -145
  186. package/src/constants/sheetRegionSources.generated.ts +0 -578
  187. package/src/schemas/fieldDocsQuery.ts +0 -12
  188. package/src/schemas/organization.ts +0 -68
  189. package/src/types/agentDoc.d.ts +0 -64
  190. package/src/types/noteDoc.d.ts +0 -86
  191. package/src/types/schedule.d.ts +0 -391
  192. package/src/utils/agentDoc.ts +0 -109
  193. package/src/utils/campaignFormEditor.ts +0 -871
  194. package/src/utils/dataTableRealtimeAuthority.ts +0 -209
  195. package/src/utils/dataTableRealtimeSelection.ts +0 -134
  196. package/src/utils/dataTableRowDocument.ts +0 -508
  197. package/src/utils/dataTableSource.ts +0 -230
  198. package/src/utils/schedule.ts +0 -799
  199. package/src/utils/scheduleEvaluation.ts +0 -1994
  200. package/src/utils/scheduleICS.ts +0 -246
  201. package/src/utils/scheduleRuleTemplates.ts +0 -188
  202. package/src/utils/sheetRegion.ts +0 -374
  203. package/src/utils/sheetRegionFilter.ts +0 -1259
  204. package/src/utils/sheetRegionSourceRegistry.ts +0 -594
  205. package/tests/schedule.test.ts +0 -693
  206. package/tests/scheduleEvaluation.test.ts +0 -1970
  207. package/tests/scheduleICS.test.ts +0 -208
  208. package/tests/scheduleRuleTemplates.test.ts +0 -176
  209. package/tests/scheduleSweepInvariants.test.ts +0 -428
  210. package/tests/sheetRegionFilter.test.ts +0 -1135
@@ -17,14 +17,14 @@ import {
17
17
  NOTE_NESTING_RULES,
18
18
  } from '../constants/noteDoc.ts';
19
19
  import type {
20
- NoteBlockNode,
21
- NoteBlockType,
22
- NoteDoc,
23
- NoteHeadingLevel,
24
- NoteInlineNode,
25
- NoteMark,
26
- NoteMarkType,
27
- } from '../types/noteDoc.d.ts';
20
+ RichTextContent,
21
+ RichTextContentType,
22
+ RichTextDocument,
23
+ RichTextHeadingLevel,
24
+ RichTextInlineContent,
25
+ RichTextMark,
26
+ RichTextMarkType,
27
+ } from '@jsb188/mday/types/richText.d.ts';
28
28
 
29
29
  const FILE_CONSTANTS = {
30
30
  // Maximum raw JSON nesting levels one sanitize walk may descend; beyond
@@ -65,7 +65,7 @@ function isContainerBlockType(type: string): boolean {
65
65
  * Build one empty paragraph block (the non-empty guarantee filler).
66
66
  */
67
67
 
68
- function createEmptyParagraph(): NoteBlockNode {
68
+ function createEmptyParagraph(): RichTextContent {
69
69
  return {
70
70
  type: 'paragraph',
71
71
  content: [],
@@ -76,7 +76,7 @@ function createEmptyParagraph(): NoteBlockNode {
76
76
  * Build one empty note document: a single empty paragraph, current version.
77
77
  */
78
78
 
79
- export function createEmptyNoteDoc(): NoteDoc {
79
+ export function createEmptyNoteDoc(): RichTextDocument {
80
80
  return {
81
81
  type: 'doc',
82
82
  version: NOTE_DOC_VERSION,
@@ -92,12 +92,12 @@ export function createEmptyNoteDoc(): NoteDoc {
92
92
  * survives so clean runs omit the "marks" key entirely.
93
93
  */
94
94
 
95
- function sanitizeMarks(rawMarks: unknown): NoteMark[] | undefined {
95
+ function sanitizeMarks(rawMarks: unknown): RichTextMark[] | undefined {
96
96
  if (!Array.isArray(rawMarks)) {
97
97
  return undefined;
98
98
  }
99
99
 
100
- const marks: NoteMark[] = [];
100
+ const marks: RichTextMark[] = [];
101
101
  const seenTypes = new Set<string>();
102
102
 
103
103
  for (const raw of rawMarks) {
@@ -106,10 +106,7 @@ function sanitizeMarks(rawMarks: unknown): NoteMark[] | undefined {
106
106
  }
107
107
 
108
108
  const type = raw.type;
109
- if (
110
- !(NOTE_MARK_TYPES as readonly string[]).includes(type) ||
111
- seenTypes.has(type)
112
- ) {
109
+ if (!(NOTE_MARK_TYPES as readonly string[]).includes(type) || seenTypes.has(type)) {
113
110
  continue;
114
111
  }
115
112
 
@@ -119,7 +116,7 @@ function sanitizeMarks(rawMarks: unknown): NoteMark[] | undefined {
119
116
  if (typeof token !== 'string' || !NOTE_COLOR_TOKENS.includes(token)) {
120
117
  continue;
121
118
  }
122
- marks.push({ type: type as NoteMarkType, attrs: { token } });
119
+ marks.push({ type: type as RichTextMarkType, attrs: { token } });
123
120
  } else if (type === 'link') {
124
121
  // Only http(s)/mailto links survive; anything else is dropped
125
122
  const rawHref = isNodeObj(raw.attrs) ? raw.attrs.href : undefined;
@@ -130,7 +127,7 @@ function sanitizeMarks(rawMarks: unknown): NoteMark[] | undefined {
130
127
  marks.push({ type: 'link', attrs: { href } });
131
128
  } else {
132
129
  // bold/italic/underline/strike/code carry no attrs
133
- marks.push({ type: type as NoteMarkType });
130
+ marks.push({ type: type as RichTextMarkType });
134
131
  }
135
132
 
136
133
  seenTypes.add(type);
@@ -144,14 +141,12 @@ function sanitizeMarks(rawMarks: unknown): NoteMark[] | undefined {
144
141
  * to merge adjacent runs with identical styling.
145
142
  */
146
143
 
147
- function getMarkSetKey(marks?: NoteMark[]): string {
144
+ function getMarkSetKey(marks?: RichTextMark[]): string {
148
145
  if (!marks?.length) {
149
146
  return '';
150
147
  }
151
148
  return marks
152
- .map(
153
- (mark) => mark.type + ':' + (mark.attrs?.token || mark.attrs?.href || ''),
154
- )
149
+ .map((mark) => `${mark.type}:${mark.attrs?.token || mark.attrs?.href || ''}`)
155
150
  .sort()
156
151
  .join('|');
157
152
  }
@@ -164,11 +159,8 @@ function getMarkSetKey(marks?: NoteMark[]): string {
164
159
  * mark sets.
165
160
  */
166
161
 
167
- function sanitizeInlineContent(
168
- rawContent: unknown,
169
- budget: SanitizeBudget,
170
- ): NoteInlineNode[] {
171
- const inline: NoteInlineNode[] = [];
162
+ function sanitizeInlineContent(rawContent: unknown, budget: SanitizeBudget): RichTextInlineContent[] {
163
+ const inline: RichTextInlineContent[] = [];
172
164
  if (!Array.isArray(rawContent)) {
173
165
  return inline;
174
166
  }
@@ -194,10 +186,7 @@ function sanitizeInlineContent(
194
186
  }
195
187
 
196
188
  // Truncate the tail once the doc-wide character cap is spent
197
- const text =
198
- raw.text.length > budget.textChars
199
- ? raw.text.slice(0, budget.textChars)
200
- : raw.text;
189
+ const text = raw.text.length > budget.textChars ? raw.text.slice(0, budget.textChars) : raw.text;
201
190
  if (!text) {
202
191
  continue;
203
192
  }
@@ -206,10 +195,7 @@ function sanitizeInlineContent(
206
195
  const marks = sanitizeMarks(raw.marks);
207
196
  const prev = inline[inline.length - 1];
208
197
 
209
- if (
210
- prev?.type === 'text' &&
211
- getMarkSetKey(prev.marks) === getMarkSetKey(marks)
212
- ) {
198
+ if (prev?.type === 'text' && getMarkSetKey(prev.marks) === getMarkSetKey(marks)) {
213
199
  // Merge adjacent runs with identical styling
214
200
  prev.text += text;
215
201
  } else if (marks) {
@@ -246,9 +232,7 @@ function collectRawInlineNodes(rawBlock: Record<string, any>): any[] {
246
232
 
247
233
  // Text-bearing blocks contribute their inline children as one chunk
248
234
  const inlineChildren = node.content.filter(
249
- (child: any) =>
250
- isNodeObj(child) &&
251
- (child.type === 'text' || child.type === 'hardBreak'),
235
+ (child: any) => isNodeObj(child) && (child.type === 'text' || child.type === 'hardBreak'),
252
236
  );
253
237
  if (inlineChildren.length) {
254
238
  if (out.length) {
@@ -260,11 +244,7 @@ function collectRawInlineNodes(rawBlock: Record<string, any>): any[] {
260
244
  // Descend into block children in document order (reverse push)
261
245
  for (let i = node.content.length - 1; i >= 0; i--) {
262
246
  const child = node.content[i];
263
- if (
264
- isNodeObj(child) &&
265
- child.type !== 'text' &&
266
- child.type !== 'hardBreak'
267
- ) {
247
+ if (isNodeObj(child) && child.type !== 'text' && child.type !== 'hardBreak') {
268
248
  stack.push(child);
269
249
  }
270
250
  }
@@ -282,11 +262,11 @@ function collectRawInlineNodes(rawBlock: Record<string, any>): any[] {
282
262
 
283
263
  function sanitizeBlockNode(
284
264
  raw: Record<string, any>,
285
- type: NoteBlockType,
265
+ type: RichTextContentType,
286
266
  depth: number,
287
267
  rawDepth: number,
288
268
  budget: SanitizeBudget,
289
- ): NoteBlockNode[] {
269
+ ): RichTextContent[] {
290
270
  if (budget.blocks <= 0) {
291
271
  // Block cap reached; truncate the tail
292
272
  return [];
@@ -305,11 +285,7 @@ function sanitizeBlockNode(
305
285
  if (type === 'heading') {
306
286
  // Clamp the heading level into 1-3 (garbage coerces to 1)
307
287
  const rawLevel = isNodeObj(raw.attrs) ? Number(raw.attrs.level) : NaN;
308
- const level = (
309
- Number.isFinite(rawLevel)
310
- ? Math.min(3, Math.max(1, Math.round(rawLevel)))
311
- : 1
312
- ) as NoteHeadingLevel;
288
+ const level = (Number.isFinite(rawLevel) ? Math.min(3, Math.max(1, Math.round(rawLevel))) : 1) as RichTextHeadingLevel;
313
289
  return [
314
290
  {
315
291
  type: 'heading',
@@ -324,17 +300,20 @@ function sanitizeBlockNode(
324
300
  }
325
301
 
326
302
  if (type === 'image') {
303
+ if (raw._uploading === true) {
304
+ // The signed upload is not authoritative yet. Omitting the block lets
305
+ // unrelated edits persist while a later upload-complete revision adds
306
+ // the validated image.
307
+ budget.blocks += 1;
308
+ return [];
309
+ }
310
+
327
311
  // Image blocks must carry a storage id and file uri; everything else
328
- // (alt, width) is optional and anything unknown (_uploading, etc)
329
- // is stripped by rebuilding attrs from scratch
312
+ // (alt, width) is optional and unknown fields are stripped by rebuilding
313
+ // attrs from scratch.
330
314
  const rawAttrs = isNodeObj(raw.attrs) ? raw.attrs : {};
331
- const storageId =
332
- typeof rawAttrs.storageId === 'string' ||
333
- typeof rawAttrs.storageId === 'number'
334
- ? String(rawAttrs.storageId)
335
- : '';
336
- const fileUri =
337
- typeof rawAttrs.fileUri === 'string' ? rawAttrs.fileUri : '';
315
+ const storageId = typeof rawAttrs.storageId === 'string' ? rawAttrs.storageId : '';
316
+ const fileUri = typeof rawAttrs.fileUri === 'string' ? rawAttrs.fileUri : '';
338
317
 
339
318
  if (!storageId || !fileUri) {
340
319
  // Unclaimed/incomplete images (mid-upload) never persist
@@ -342,31 +321,21 @@ function sanitizeBlockNode(
342
321
  return [];
343
322
  }
344
323
 
345
- const block: NoteBlockNode = {
324
+ const block: RichTextContent = {
346
325
  type: 'image',
347
326
  attrs: { storageId, fileUri },
348
327
  };
349
328
  if (typeof rawAttrs.alt === 'string' && rawAttrs.alt) {
350
329
  block.attrs!.alt = rawAttrs.alt;
351
330
  }
352
- if (
353
- typeof rawAttrs.width === 'number' &&
354
- Number.isFinite(rawAttrs.width) &&
355
- rawAttrs.width > 0
356
- ) {
331
+ if (typeof rawAttrs.width === 'number' && Number.isFinite(rawAttrs.width) && rawAttrs.width > 0) {
357
332
  block.attrs!.width = rawAttrs.width;
358
333
  }
359
334
  return [block];
360
335
  }
361
336
 
362
337
  if (type === 'bulletList' || type === 'orderedList') {
363
- const children = sanitizeBlockContent(
364
- raw.content,
365
- type,
366
- depth + 1,
367
- rawDepth + 1,
368
- budget,
369
- );
338
+ const children = sanitizeBlockContent(raw.content, type, depth + 1, rawDepth + 1, budget);
370
339
  if (!children.length) {
371
340
  // A list with no surviving listItems renders nothing; drop it
372
341
  return [];
@@ -375,13 +344,7 @@ function sanitizeBlockNode(
375
344
  }
376
345
 
377
346
  // listItem/blockquote: containers that must never be left empty
378
- const children = sanitizeBlockContent(
379
- raw.content,
380
- type,
381
- depth + 1,
382
- rawDepth + 1,
383
- budget,
384
- );
347
+ const children = sanitizeBlockContent(raw.content, type, depth + 1, rawDepth + 1, budget);
385
348
  if (!children.length) {
386
349
  children.push(createEmptyParagraph());
387
350
  }
@@ -401,17 +364,17 @@ function sanitizeBlockNode(
401
364
 
402
365
  function sanitizeBlockContent(
403
366
  rawContent: unknown,
404
- containerType: 'doc' | NoteBlockType,
367
+ containerType: 'doc' | RichTextContentType,
405
368
  depth: number,
406
369
  rawDepth: number,
407
370
  budget: SanitizeBudget,
408
- ): NoteBlockNode[] {
371
+ ): RichTextContent[] {
409
372
  if (!Array.isArray(rawContent) || rawDepth > FILE_CONSTANTS.maxRawNesting) {
410
373
  return [];
411
374
  }
412
375
 
413
376
  const allowedTypes = NOTE_NESTING_RULES[containerType] || [];
414
- const blocks: NoteBlockNode[] = [];
377
+ const blocks: RichTextContent[] = [];
415
378
  let strayInline: any[] = [];
416
379
 
417
380
  /*
@@ -425,25 +388,9 @@ function sanitizeBlockContent(
425
388
  const rawParagraph = { type: 'paragraph', content: strayInline };
426
389
  strayInline = [];
427
390
  if (allowedTypes.includes('paragraph')) {
428
- blocks.push(
429
- ...sanitizeBlockNode(
430
- rawParagraph,
431
- 'paragraph',
432
- depth,
433
- rawDepth,
434
- budget,
435
- ),
436
- );
391
+ blocks.push(...sanitizeBlockNode(rawParagraph, 'paragraph', depth, rawDepth, budget));
437
392
  } else if (allowedTypes.includes('listItem')) {
438
- blocks.push(
439
- ...sanitizeBlockNode(
440
- { type: 'listItem', content: [rawParagraph] },
441
- 'listItem',
442
- depth,
443
- rawDepth,
444
- budget,
445
- ),
446
- );
393
+ blocks.push(...sanitizeBlockNode({ type: 'listItem', content: [rawParagraph] }, 'listItem', depth, rawDepth, budget));
447
394
  }
448
395
  };
449
396
 
@@ -465,7 +412,7 @@ function sanitizeBlockContent(
465
412
  }
466
413
 
467
414
  let raw = rawChild;
468
- let type = rawChild.type as NoteBlockType;
415
+ let type = rawChild.type as RichTextContentType;
469
416
 
470
417
  if (isContainerBlockType(type) && depth >= NOTE_MAX_DEPTH) {
471
418
  // Container past the depth cap: flatten its subtree into one
@@ -476,47 +423,17 @@ function sanitizeBlockContent(
476
423
 
477
424
  if (allowedTypes.includes(type)) {
478
425
  blocks.push(...sanitizeBlockNode(raw, type, depth, rawDepth, budget));
479
- } else if (
480
- (type === 'heading' || type === 'paragraph') &&
481
- allowedTypes.includes('paragraph')
482
- ) {
426
+ } else if ((type === 'heading' || type === 'paragraph') && allowedTypes.includes('paragraph')) {
483
427
  // Text block where only paragraphs fit converts to a paragraph
484
- blocks.push(
485
- ...sanitizeBlockNode(
486
- { ...raw, type: 'paragraph' },
487
- 'paragraph',
488
- depth,
489
- rawDepth,
490
- budget,
491
- ),
492
- );
428
+ blocks.push(...sanitizeBlockNode({ ...raw, type: 'paragraph' }, 'paragraph', depth, rawDepth, budget));
493
429
  } else if (isContainerBlockType(type)) {
494
430
  // Illegal container: unwrap it and re-process its children
495
431
  // against this same container (lists in blockquotes end up as
496
432
  // paragraphs, listItems under doc splice their children in, ...)
497
- blocks.push(
498
- ...sanitizeBlockContent(
499
- raw.content,
500
- containerType,
501
- depth,
502
- rawDepth + 1,
503
- budget,
504
- ),
505
- );
506
- } else if (
507
- (type === 'heading' || type === 'paragraph') &&
508
- allowedTypes.includes('listItem')
509
- ) {
433
+ blocks.push(...sanitizeBlockContent(raw.content, containerType, depth, rawDepth + 1, budget));
434
+ } else if ((type === 'heading' || type === 'paragraph') && allowedTypes.includes('listItem')) {
510
435
  // Text block directly inside a list wraps into its own listItem
511
- blocks.push(
512
- ...sanitizeBlockNode(
513
- { type: 'listItem', content: [raw] },
514
- 'listItem',
515
- depth,
516
- rawDepth,
517
- budget,
518
- ),
519
- );
436
+ blocks.push(...sanitizeBlockNode({ type: 'listItem', content: [raw] }, 'listItem', depth, rawDepth, budget));
520
437
  }
521
438
  // Anything left (divider/image with no legal spot here) is dropped
522
439
  }
@@ -537,7 +454,7 @@ function sanitizeBlockContent(
537
454
  * client runs this before save; the server runs it on mutation input.
538
455
  */
539
456
 
540
- export function sanitizeNoteDoc(input: unknown): NoteDoc {
457
+ export function sanitizeNoteDoc(input: unknown): RichTextDocument {
541
458
  if (!isNodeObj(input) || input.type !== 'doc') {
542
459
  // Unknown roots coerce to a fresh empty document
543
460
  return createEmptyNoteDoc();
@@ -556,133 +473,6 @@ export function sanitizeNoteDoc(input: unknown): NoteDoc {
556
473
  };
557
474
  }
558
475
 
559
- /**
560
- * Recursively drop image blocks whose storageId is missing from the rels
561
- * id set, keeping the container non-empty guarantees: a listItem or
562
- * blockquote emptied by the filter gets one empty paragraph and a list
563
- * emptied of its listItems is dropped.
564
- */
565
-
566
- function filterImageBlocksDeep(
567
- blocks: NoteBlockNode[],
568
- storageIdSet: Set<string>,
569
- ): NoteBlockNode[] {
570
- const out: NoteBlockNode[] = [];
571
-
572
- for (const block of blocks) {
573
- if (!isNodeObj(block)) {
574
- continue;
575
- }
576
-
577
- if (block.type === 'image') {
578
- // notes_storage_rels is the source of truth: no rel, no image
579
- const storageId = block.attrs?.storageId;
580
- if (
581
- storageId === undefined ||
582
- storageId === null ||
583
- !storageIdSet.has(String(storageId))
584
- ) {
585
- continue;
586
- }
587
- out.push(block);
588
- continue;
589
- }
590
-
591
- if (isContainerBlockType(block.type) && Array.isArray(block.content)) {
592
- const children = filterImageBlocksDeep(
593
- block.content as NoteBlockNode[],
594
- storageIdSet,
595
- );
596
- if (
597
- (block.type === 'bulletList' || block.type === 'orderedList') &&
598
- !children.length
599
- ) {
600
- continue;
601
- }
602
- if (!children.length) {
603
- children.push(createEmptyParagraph());
604
- }
605
- out.push({ ...block, content: children });
606
- continue;
607
- }
608
-
609
- out.push(block);
610
- }
611
-
612
- return out;
613
- }
614
-
615
- /**
616
- * Drop every image block whose storageId (string-compared) has no row in
617
- * notes_storage_rels — the rels table is the source of truth for embedded
618
- * files. Pure: returns a new document, never leaves the doc (or any
619
- * container) empty, and never mutates the input.
620
- */
621
-
622
- export function filterImageNodesByStorageRels(
623
- doc: NoteDoc,
624
- storageIdSet: Set<string>,
625
- ): NoteDoc {
626
- const blocks =
627
- isNodeObj(doc) && Array.isArray(doc.content) ? doc.content : [];
628
- const content = filterImageBlocksDeep(blocks, storageIdSet);
629
-
630
- return {
631
- type: 'doc',
632
- version:
633
- isNodeObj(doc) && typeof doc.version === 'number'
634
- ? doc.version
635
- : NOTE_DOC_VERSION,
636
- content: content.length ? content : [createEmptyParagraph()],
637
- };
638
- }
639
-
640
- /**
641
- * Collect the storageId of every image block in document order, deduped.
642
- * The client sends this list with every content save so the server can
643
- * reconcile notes_storage_rels.
644
- */
645
-
646
- export function collectNoteImageStorageIds(doc: NoteDoc): string[] {
647
- const ids: string[] = [];
648
- const seen = new Set<string>();
649
-
650
- /*
651
- * Walk one block list depth-first, gathering image storage ids.
652
- */
653
- const walk = (blocks: Array<NoteBlockNode | NoteInlineNode>) => {
654
- for (const node of blocks) {
655
- if (!isNodeObj(node)) {
656
- continue;
657
- }
658
- const block = node as NoteBlockNode;
659
- const storageId = block.attrs?.storageId;
660
- if (
661
- block.type === 'image' &&
662
- storageId !== undefined &&
663
- storageId !== null &&
664
- String(storageId)
665
- ) {
666
- const id = String(storageId);
667
- if (!seen.has(id)) {
668
- seen.add(id);
669
- ids.push(id);
670
- }
671
- } else if (
672
- isContainerBlockType(block.type) &&
673
- Array.isArray(block.content)
674
- ) {
675
- walk(block.content);
676
- }
677
- }
678
- };
679
-
680
- if (isNodeObj(doc) && Array.isArray(doc.content)) {
681
- walk(doc.content);
682
- }
683
- return ids;
684
- }
685
-
686
476
  /**
687
477
  * Extract one plain-text rendition of a note document, for previews and
688
478
  * search indexing: text runs concatenate, hardBreaks become newlines, each
@@ -690,37 +480,31 @@ export function collectNoteImageStorageIds(doc: NoteDoc): string[] {
690
480
  * plus empty blocks contribute nothing.
691
481
  */
692
482
 
693
- export function noteDocToPlainText(doc: NoteDoc): string {
483
+ export function noteDocToPlainText(doc: RichTextDocument): string {
694
484
  const lines: string[] = [];
695
485
 
696
486
  /*
697
487
  * Walk one block list depth-first, pushing one line per text-bearing block.
698
488
  */
699
- const walk = (blocks: Array<NoteBlockNode | NoteInlineNode>) => {
489
+ const walk = (blocks: Array<RichTextContent | RichTextInlineContent>) => {
700
490
  for (const node of blocks) {
701
491
  if (!isNodeObj(node)) {
702
492
  continue;
703
493
  }
704
- const block = node as NoteBlockNode;
494
+ const block = node as RichTextContent;
705
495
  if (isContainerBlockType(block.type) && Array.isArray(block.content)) {
706
496
  walk(block.content);
707
497
  continue;
708
498
  }
709
- if (
710
- (block.type === 'paragraph' || block.type === 'heading') &&
711
- Array.isArray(block.content)
712
- ) {
499
+ if ((block.type === 'paragraph' || block.type === 'heading') && Array.isArray(block.content)) {
713
500
  let line = '';
714
- for (const inline of block.content as NoteInlineNode[]) {
501
+ for (const inline of block.content as RichTextInlineContent[]) {
715
502
  if (!isNodeObj(inline)) {
716
503
  continue;
717
504
  }
718
505
  if (inline.type === 'hardBreak') {
719
506
  line += '\n';
720
- } else if (
721
- inline.type === 'text' &&
722
- typeof inline.text === 'string'
723
- ) {
507
+ } else if (inline.type === 'text' && typeof inline.text === 'string') {
724
508
  line += inline.text;
725
509
  }
726
510
  }
@@ -741,7 +525,7 @@ export function noteDocToPlainText(doc: NoteDoc): string {
741
525
  * Build one text inline node, or an empty content list for a blank line.
742
526
  */
743
527
 
744
- function createTextContent(text: string): NoteInlineNode[] {
528
+ function createTextContent(text: string): RichTextInlineContent[] {
745
529
  return text ? [{ type: 'text', text }] : [];
746
530
  }
747
531
 
@@ -754,10 +538,10 @@ function createTextContent(text: string): NoteInlineNode[] {
754
538
  * rules apply exactly as they do for human-written documents.
755
539
  */
756
540
 
757
- export function plainTextToNoteDoc(text: string): NoteDoc {
758
- const blocks: NoteBlockNode[] = [];
541
+ export function plainTextToNoteDoc(text: string): RichTextDocument {
542
+ const blocks: RichTextContent[] = [];
759
543
  let listType: 'bulletList' | 'orderedList' | null = null;
760
- let listItems: NoteBlockNode[] = [];
544
+ let listItems: RichTextContent[] = [];
761
545
 
762
546
  /*
763
547
  * Close the open list run, if any, into a single list block.
@@ -783,7 +567,7 @@ export function plainTextToNoteDoc(text: string): NoteDoc {
783
567
  flushList();
784
568
  blocks.push({
785
569
  type: 'heading',
786
- attrs: { level: headingMatch[1].length as NoteHeadingLevel },
570
+ attrs: { level: headingMatch[1].length as RichTextHeadingLevel },
787
571
  content: createTextContent(headingMatch[2].trim()),
788
572
  });
789
573
  continue;
@@ -806,9 +590,7 @@ export function plainTextToNoteDoc(text: string): NoteDoc {
806
590
  }
807
591
  listItems.push({
808
592
  type: 'listItem',
809
- content: [
810
- { type: 'paragraph', content: createTextContent(itemText.trim()) },
811
- ],
593
+ content: [{ type: 'paragraph', content: createTextContent(itemText.trim()) }],
812
594
  });
813
595
  continue;
814
596
  }