Skip to content

Commit 9622750

Browse files
fix(fields): share section pages resolver
1 parent cf40a27 commit 9622750

4 files changed

Lines changed: 38 additions & 25 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { formatPageNumber, type PageNumberFormat } from '@superdoc/contracts';
2+
import type { Editor } from '../../core/Editor.js';
3+
4+
export function resolveSectionPageCountFieldValue(
5+
editor: Editor,
6+
node: { attrs?: Record<string, unknown> },
7+
): string | null {
8+
const sectionPageCount = editor.options?.sectionPageCount;
9+
if (sectionPageCount == null) return null;
10+
11+
const pageNumberFormat = node.attrs?.pageNumberFormat;
12+
if (typeof pageNumberFormat === 'string' && pageNumberFormat) {
13+
return formatPageNumber(Number(sectionPageCount) || 1, pageNumberFormat as PageNumberFormat);
14+
}
15+
return String(sectionPageCount);
16+
}

packages/super-editor/src/editors/v1/document-api-adapters/plan-engine/field-wrappers.section-pages.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ const schema = new Schema({
3333
},
3434
});
3535

36-
function createEditorWithSectionPageCount(sectionPageCount?: number, initialValue = '1'): Editor {
36+
function createEditorWithSectionPageCount(
37+
sectionPageCount?: number,
38+
initialValue = '1',
39+
pageNumberFormat?: string,
40+
): Editor {
3741
const field = schema.nodes['section-page-count'].create(
38-
{ instruction: 'SECTIONPAGES', resolvedText: initialValue },
42+
{ instruction: 'SECTIONPAGES', resolvedText: initialValue, pageNumberFormat },
3943
schema.text(initialValue),
4044
);
4145
const paragraph = schema.nodes.paragraph.create({ sdBlockId: 'block-1' }, field);
@@ -70,6 +74,20 @@ describe('fieldsRebuildWrapper SECTIONPAGES fields', () => {
7074
expect(updatedField?.textContent).toBe('4');
7175
});
7276

77+
it('formats rebuilt section-page-count values with pageNumberFormat', () => {
78+
const editor = createEditorWithSectionPageCount(4, '1', 'upperRoman');
79+
80+
const result = fieldsRebuildWrapper(editor, {
81+
target: { kind: 'field', blockId: 'block-1', occurrenceIndex: 0, nestingDepth: 0 },
82+
});
83+
84+
expect(result.success).toBe(true);
85+
const updatedField = editor.state.doc.nodeAt(1);
86+
expect(updatedField?.type.name).toBe('section-page-count');
87+
expect(updatedField?.attrs.resolvedText).toBe('IV');
88+
expect(updatedField?.textContent).toBe('IV');
89+
});
90+
7391
it('preserves existing section-page-count text when section page context is unavailable', () => {
7492
const editor = createEditorWithSectionPageCount(undefined, '3');
7593

packages/super-editor/src/editors/v1/document-api-adapters/plan-engine/field-wrappers.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44

55
import type { Editor } from '../../core/Editor.js';
6-
import { formatPageNumber, type PageNumberFormat } from '@superdoc/contracts';
76
import type {
87
FieldListInput,
98
FieldGetInput,
@@ -30,6 +29,7 @@ import { rejectTrackedMode } from '../helpers/mutation-helpers.js';
3029
import { clearIndexCache } from '../helpers/index-cache.js';
3130
import { DocumentApiAdapterError } from '../errors.js';
3231
import { getWordStatistics, resolveDocumentStatFieldValue, resolveMainBodyEditor } from '../helpers/word-statistics.js';
32+
import { resolveSectionPageCountFieldValue } from '../helpers/section-page-count.js';
3333

3434
// ---------------------------------------------------------------------------
3535
// Result helpers
@@ -404,17 +404,6 @@ function rebuildSectionPageCount(
404404
return fieldSuccess(address);
405405
}
406406

407-
function resolveSectionPageCountFieldValue(editor: Editor, node: { attrs?: Record<string, unknown> }): string | null {
408-
const sectionPageCount = editor.options?.sectionPageCount;
409-
if (sectionPageCount == null) return null;
410-
411-
const pageNumberFormat = node.attrs?.pageNumberFormat;
412-
if (typeof pageNumberFormat === 'string' && pageNumberFormat) {
413-
return formatPageNumber(Number(sectionPageCount) || 1, pageNumberFormat as PageNumberFormat);
414-
}
415-
return String(sectionPageCount);
416-
}
417-
418407
export function fieldsRemoveWrapper(
419408
editor: Editor,
420409
input: FieldRemoveInput,

packages/super-editor/src/editors/v1/extensions/field-update/field-update.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
resolveDocumentStatFieldValue,
77
resolveMainBodyEditor,
88
} from '../../document-api-adapters/helpers/word-statistics.js';
9-
import { formatPageNumber } from '@superdoc/contracts';
9+
import { resolveSectionPageCountFieldValue } from '../../document-api-adapters/helpers/section-page-count.js';
1010

1111
/** Stat-field types refreshed by F9 when the doc has no TOCs. */
1212
const UPDATABLE_FIELD_TYPES = new Set(['NUMWORDS', 'NUMCHARS', 'NUMPAGES', 'SECTIONPAGES']);
@@ -142,13 +142,3 @@ export const FieldUpdate = Extension.create({
142142
};
143143
},
144144
});
145-
146-
function resolveSectionPageCountFieldValue(editor, node) {
147-
const sectionPageCount = editor?.options?.sectionPageCount;
148-
if (sectionPageCount == null) return null;
149-
150-
if (node?.attrs?.pageNumberFormat) {
151-
return formatPageNumber(Number(sectionPageCount) || 1, node.attrs.pageNumberFormat);
152-
}
153-
return String(sectionPageCount);
154-
}

0 commit comments

Comments
 (0)