Skip to content

Commit 69f45cf

Browse files
committed
feat(super-editor): support alignment in format.apply mutation step
1 parent 98a1609 commit 69f45cf

1 file changed

Lines changed: 60 additions & 2 deletions

File tree

  • packages/super-editor/src/editors/v1/document-api-adapters/plan-engine

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,43 @@ export function executeTextDelete(
804804
return { changed: true };
805805
}
806806

807+
/** Alignment API value → OOXML justification value */
808+
const ALIGNMENT_TO_JUSTIFICATION: Record<string, string> = {
809+
left: 'left',
810+
center: 'center',
811+
right: 'right',
812+
justify: 'both',
813+
};
814+
815+
/**
816+
* Applies alignment to the paragraph node(s) that contain the given range.
817+
* Uses the same mechanism as paragraphsSetAlignmentWrapper: updates
818+
* paragraphProperties.justification via tr.setNodeMarkup.
819+
*/
820+
function applyAlignmentToRange(tr: Transaction, absFrom: number, absTo: number, alignment: string): boolean {
821+
const justification = ALIGNMENT_TO_JUSTIFICATION[alignment];
822+
if (!justification) return false;
823+
824+
let changed = false;
825+
const doc = tr.doc;
826+
827+
doc.nodesBetween(absFrom, absTo, (node, pos) => {
828+
// Only set alignment on textblock nodes (paragraphs, headings)
829+
if (!node.isTextblock) return;
830+
831+
const existing = (node.attrs as Record<string, unknown>).paragraphProperties as Record<string, unknown> | undefined;
832+
const currentJustification = existing?.justification;
833+
834+
if (currentJustification === justification) return;
835+
836+
const updated = { ...(existing ?? {}), justification };
837+
tr.setNodeMarkup(pos, undefined, { ...node.attrs, paragraphProperties: updated });
838+
changed = true;
839+
});
840+
841+
return changed;
842+
}
843+
807844
export function executeStyleApply(
808845
editor: Editor,
809846
tr: Transaction,
@@ -813,7 +850,18 @@ export function executeStyleApply(
813850
): { changed: boolean } {
814851
const absFrom = mapping.map(target.absFrom);
815852
const absTo = mapping.map(target.absTo);
816-
return { changed: applyInlinePatchToRange(editor, tr, absFrom, absTo, step.args.inline) };
853+
854+
let changed = false;
855+
856+
if (step.args.inline) {
857+
changed = applyInlinePatchToRange(editor, tr, absFrom, absTo, step.args.inline) || changed;
858+
}
859+
860+
if (step.args.alignment) {
861+
changed = applyAlignmentToRange(tr, absFrom, absTo, step.args.alignment) || changed;
862+
}
863+
864+
return { changed };
817865
}
818866

819867
// ---------------------------------------------------------------------------
@@ -957,7 +1005,17 @@ export function executeSpanStyleApply(
9571005
const absFrom = mapping.map(firstSeg.absFrom, 1);
9581006
const absTo = mapping.map(lastSeg.absTo, -1);
9591007

960-
return { changed: applyInlinePatchToRange(editor, tr, absFrom, absTo, step.args.inline) };
1008+
let changed = false;
1009+
1010+
if (step.args.inline) {
1011+
changed = applyInlinePatchToRange(editor, tr, absFrom, absTo, step.args.inline) || changed;
1012+
}
1013+
1014+
if (step.args.alignment) {
1015+
changed = applyAlignmentToRange(tr, absFrom, absTo, step.args.alignment) || changed;
1016+
}
1017+
1018+
return { changed };
9611019
}
9621020

9631021
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)