@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.
- package/package.json +58 -23
- package/src/constants/agentMemory.ts +14 -45
- package/src/constants/agentNote.ts +212 -0
- package/src/constants/aiChat.ts +5 -0
- package/src/constants/billing.ts +126 -0
- package/src/constants/campaign.ts +156 -47
- package/src/constants/contact.ts +72 -0
- package/src/constants/csvExport.ts +133 -0
- package/src/constants/dataTable.ts +12 -38
- package/src/constants/dataTableFieldTypes.generated.ts +3 -7
- package/src/constants/docAccess.ts +3 -2
- package/src/constants/dt.ts +234 -0
- package/src/constants/email.ts +114 -12
- package/src/constants/fieldDoc.ts +74 -4
- package/src/constants/graphqlReactivity.ts +108 -25
- package/src/constants/log.ts +9 -0
- package/src/constants/logDisplay/README.md +88 -0
- package/src/constants/logDisplay/defaults.ts +39 -0
- package/src/constants/logDisplay/index.ts +27 -0
- package/src/constants/logDisplay/operations/arable.ts +16 -0
- package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
- package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
- package/src/constants/logDisplay/shared/accounting.ts +13 -0
- package/src/constants/logDisplay/shared/interactions.ts +13 -0
- package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
- package/src/constants/logTaxonomy.generated.ts +1834 -730
- package/src/constants/organization.ts +13 -14
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +65 -101
- package/src/constants/sms.ts +18 -0
- package/src/constants/storage.ts +33 -9
- package/src/constants/usage.ts +3 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +106 -283
- package/src/schemas/inboundContact.ts +5 -5
- package/src/types/agentMemory.d.ts +38 -14
- package/src/types/agentNote.d.ts +443 -0
- package/src/types/billing.d.ts +537 -0
- package/src/types/campaign.d.ts +1043 -193
- package/src/types/contact.d.ts +15 -0
- package/src/types/csvExport.d.ts +213 -0
- package/src/types/dataAnalysis.d.ts +44 -0
- package/src/types/dataTable.d.ts +102 -309
- package/src/types/definitionElement.d.ts +35 -0
- package/src/types/dt.d.ts +257 -0
- package/src/types/email.d.ts +144 -55
- package/src/types/emailReplyReport.d.ts +76 -0
- package/src/types/fieldDoc.d.ts +579 -150
- package/src/types/fieldDocContract.d.ts +90 -0
- package/src/types/inboundContact.d.ts +8 -0
- package/src/types/log.d.ts +94 -3
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +8 -3
- package/src/types/qrCode.d.ts +96 -0
- package/src/types/richText.d.ts +81 -0
- package/src/types/sheet.d.ts +180 -463
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +266 -27
- package/src/types/workflow.d.ts +196 -66
- package/src/utils/agentMemory.ts +15 -0
- package/src/utils/agentNote.ts +850 -0
- package/src/utils/aiChat.ts +31 -14
- package/src/utils/aiChatWork.ts +64 -0
- package/src/utils/billing.ts +37 -0
- package/src/utils/campaign.ts +642 -209
- package/src/utils/campaignAI.ts +72 -0
- package/src/utils/campaignAccess.ts +239 -0
- package/src/utils/campaignEmail.ts +601 -0
- package/src/utils/campaignEmailCompiler.ts +372 -0
- package/src/utils/campaignEmailImages.ts +60 -0
- package/src/utils/campaignEmailStyles.ts +544 -0
- package/src/utils/campaignLifecycle.ts +104 -0
- package/src/utils/campaignPreview.ts +285 -0
- package/src/utils/contact.ts +50 -0
- package/src/utils/csvExport.ts +19 -0
- package/src/utils/dataTable.ts +18 -556
- package/src/utils/dataTableFieldTypeRegistry.ts +1 -0
- package/src/utils/dataTableRelativeDate.ts +447 -0
- package/src/utils/definitionElement.ts +84 -0
- package/src/utils/email.ts +208 -1
- package/src/utils/emailReplyReport.test.ts +187 -0
- package/src/utils/emailReplyReport.ts +143 -0
- package/src/utils/fieldDoc.ts +81 -37
- package/src/utils/fieldDocAnswers.ts +128 -0
- package/src/utils/fieldDocDefaults.ts +36 -0
- package/src/utils/fieldDocProgram.ts +36 -0
- package/src/utils/fieldDocSummary.ts +30 -0
- package/src/utils/log.ts +232 -141
- package/src/utils/logDisplay.helpers.ts +177 -0
- package/src/utils/logDisplay.ts +87 -0
- package/src/utils/logStoreRead.ts +9 -13
- package/src/utils/logTaxonomy.helpers.ts +17 -0
- package/src/utils/logTaxonomyDisplay.ts +80 -124
- package/src/utils/noteDoc.ts +66 -284
- package/src/utils/organization.ts +117 -283
- package/src/utils/qrCode.helpers.ts +189 -0
- package/src/utils/qrCode.test.ts +519 -0
- package/src/utils/qrCode.ts +273 -0
- package/src/utils/richTextHTML.ts +385 -0
- package/src/utils/sheet.ts +123 -4834
- package/src/utils/sheetAxisMove.ts +77 -291
- package/src/utils/sheetBootstrap.ts +771 -1138
- package/src/utils/sheetCellLiteral.ts +101 -0
- package/src/utils/sheetCommand.ts +23 -88
- package/src/utils/sheetDesign.ts +705 -0
- package/src/utils/sheetExport.ts +17 -69
- package/src/utils/sheetFeatureReference.ts +183 -0
- package/src/utils/sheetFormulaArgs.ts +168 -0
- package/src/utils/sheetFormulaCompile.ts +475 -0
- package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
- package/src/utils/sheetFormulaCriteria.ts +656 -0
- package/src/utils/sheetFormulaDate.ts +444 -0
- package/src/utils/sheetFormulaEvaluation.ts +388 -403
- package/src/utils/sheetFormulaEvaluator.ts +589 -444
- package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
- package/src/utils/sheetFormulaLogical.ts +116 -0
- package/src/utils/sheetFormulaLookup.ts +488 -0
- package/src/utils/sheetFormulaMath.ts +48 -0
- package/src/utils/sheetFormulaParser.ts +729 -0
- package/src/utils/sheetFormulaRange.ts +330 -0
- package/src/utils/sheetFormulaReference.ts +703 -0
- package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
- package/src/utils/sheetFormulaReferenceDates.ts +237 -0
- package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
- package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
- package/src/utils/sheetFormulaReferenceMath.ts +95 -0
- package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
- package/src/utils/sheetFormulaReferenceText.ts +462 -0
- package/src/utils/sheetFormulaSyntax.ts +1027 -0
- package/src/utils/sheetFormulaText.ts +430 -0
- package/src/utils/sheetJournal.ts +140 -18
- package/src/utils/sheetNumberFormat.ts +68 -267
- package/src/utils/sheetPreview.ts +68 -0
- package/src/utils/sheetSequence.ts +13 -43
- package/src/utils/sheetSnapshot.ts +176 -429
- package/src/utils/sheetStructureEdit.ts +537 -907
- package/src/utils/sheetStructureReferences.ts +389 -0
- package/src/utils/storage.ts +49 -3
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +199 -73
- package/src/utils/workflowLogFingerprint.ts +302 -0
- package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
- package/src/utils/workflowProgress.ts +280 -0
- package/src/utils/workflowTriggers.ts +61 -0
- package/tests/agentNote.test.ts +395 -0
- package/tests/campaignAI.test.ts +119 -0
- package/tests/campaignAccess.test.ts +239 -0
- package/tests/campaignEmail.test.ts +441 -0
- package/tests/campaignLifecycle.test.ts +134 -0
- package/tests/campaignPublication.test.ts +309 -0
- package/tests/dataTableFieldTypeRegistry.test.ts +2 -1
- package/tests/dataTableRelativeDate.test.ts +412 -0
- package/tests/email.test.ts +39 -0
- package/tests/fieldDocProgram.test.ts +114 -0
- package/tests/logDisplay.fixtures.ts +67 -0
- package/tests/logDisplay.test.ts +227 -0
- package/tests/noteDoc.test.ts +37 -192
- package/tests/richTextHTML.test.ts +185 -0
- package/tests/sheetAxisMove.test.ts +49 -40
- package/tests/sheetBootstrap.test.ts +1967 -0
- package/tests/sheetFeatureReference.test.ts +22 -0
- package/tests/sheetFormulaCriteria.test.ts +269 -0
- package/tests/sheetFormulaEvaluator.test.ts +93 -200
- package/tests/sheetFormulaFunctions.test.ts +538 -0
- package/tests/sheetFormulaLanguage.test.ts +361 -0
- package/tests/sheetFormulaParity.fixtures.ts +872 -0
- package/tests/sheetFormulaParity.test.ts +188 -0
- package/tests/sheetFormulaRange.test.ts +167 -0
- package/tests/sheetFormulaReference.test.ts +78 -0
- package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
- package/tests/sheetFormulaTextDate.test.ts +512 -0
- package/tests/sheetSequence.test.ts +4 -23
- package/tests/sheetSnapshot.test.ts +134 -0
- package/tests/sheetStructureEdit.test.ts +492 -37
- package/tests/sheetStructureReferences.test.ts +589 -0
- package/tests/workflowLifecycleStatus.test.ts +60 -0
- package/tests/workflowLogFingerprint.test.ts +203 -0
- package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
- package/tests/workflowProgress.test.ts +180 -0
- package/tests/workflowTriggers.test.ts +29 -0
- package/src/constants/agentDoc.ts +0 -12
- package/src/constants/logActivityGroupColors.generated.ts +0 -19
- package/src/constants/schedule.ts +0 -220
- package/src/constants/scheduleRuleTemplateSeed.ts +0 -138
- package/src/constants/scheduleRuleTemplates.generated.ts +0 -145
- package/src/constants/sheetRegionSources.generated.ts +0 -578
- package/src/schemas/fieldDocsQuery.ts +0 -12
- package/src/schemas/organization.ts +0 -68
- package/src/types/agentDoc.d.ts +0 -64
- package/src/types/noteDoc.d.ts +0 -86
- package/src/types/schedule.d.ts +0 -391
- package/src/utils/agentDoc.ts +0 -109
- package/src/utils/campaignFormEditor.ts +0 -871
- package/src/utils/dataTableRealtimeAuthority.ts +0 -209
- package/src/utils/dataTableRealtimeSelection.ts +0 -134
- package/src/utils/dataTableRowDocument.ts +0 -508
- package/src/utils/dataTableSource.ts +0 -230
- package/src/utils/schedule.ts +0 -799
- package/src/utils/scheduleEvaluation.ts +0 -1994
- package/src/utils/scheduleICS.ts +0 -246
- package/src/utils/scheduleRuleTemplates.ts +0 -188
- package/src/utils/sheetRegion.ts +0 -374
- package/src/utils/sheetRegionFilter.ts +0 -1259
- package/src/utils/sheetRegionSourceRegistry.ts +0 -594
- package/tests/schedule.test.ts +0 -693
- package/tests/scheduleEvaluation.test.ts +0 -1970
- package/tests/scheduleICS.test.ts +0 -208
- package/tests/scheduleRuleTemplates.test.ts +0 -176
- package/tests/scheduleSweepInvariants.test.ts +0 -428
- package/tests/sheetRegionFilter.test.ts +0 -1135
package/src/utils/noteDoc.ts
CHANGED
|
@@ -17,14 +17,14 @@ import {
|
|
|
17
17
|
NOTE_NESTING_RULES,
|
|
18
18
|
} from '../constants/noteDoc.ts';
|
|
19
19
|
import type {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
} from '
|
|
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():
|
|
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():
|
|
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):
|
|
95
|
+
function sanitizeMarks(rawMarks: unknown): RichTextMark[] | undefined {
|
|
96
96
|
if (!Array.isArray(rawMarks)) {
|
|
97
97
|
return undefined;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
const marks:
|
|
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
|
|
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
|
|
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?:
|
|
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
|
-
|
|
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:
|
|
265
|
+
type: RichTextContentType,
|
|
286
266
|
depth: number,
|
|
287
267
|
rawDepth: number,
|
|
288
268
|
budget: SanitizeBudget,
|
|
289
|
-
):
|
|
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
|
|
329
|
-
//
|
|
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
|
-
|
|
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:
|
|
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' |
|
|
367
|
+
containerType: 'doc' | RichTextContentType,
|
|
405
368
|
depth: number,
|
|
406
369
|
rawDepth: number,
|
|
407
370
|
budget: SanitizeBudget,
|
|
408
|
-
):
|
|
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:
|
|
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
|
|
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
|
-
|
|
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):
|
|
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:
|
|
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<
|
|
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
|
|
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
|
|
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):
|
|
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):
|
|
758
|
-
const blocks:
|
|
541
|
+
export function plainTextToNoteDoc(text: string): RichTextDocument {
|
|
542
|
+
const blocks: RichTextContent[] = [];
|
|
759
543
|
let listType: 'bulletList' | 'orderedList' | null = null;
|
|
760
|
-
let listItems:
|
|
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
|
|
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
|
}
|