roosterjs-content-model-api 9.15.0 → 9.16.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.
- package/lib/publicApi/utils/formatSegmentWithContentModel.js +74 -2
- package/lib/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
- package/lib-amd/publicApi/utils/formatSegmentWithContentModel.js +74 -2
- package/lib-amd/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
- package/lib-mjs/publicApi/utils/formatSegmentWithContentModel.js +76 -4
- package/lib-mjs/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
- package/package.json +3 -3
|
@@ -15,9 +15,10 @@ var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
|
|
|
15
15
|
*/
|
|
16
16
|
function formatSegmentWithContentModel(editor, apiName, toggleStyleCallback, segmentHasStyleCallback, includingFormatHolder, afterFormatCallback) {
|
|
17
17
|
editor.formatContentModel(function (model, context) {
|
|
18
|
-
var segmentAndParagraphs = (0, roosterjs_content_model_dom_1.getSelectedSegmentsAndParagraphs)(model, !!includingFormatHolder,
|
|
18
|
+
var segmentAndParagraphs = (0, roosterjs_content_model_dom_1.getSelectedSegmentsAndParagraphs)(model, !!includingFormatHolder, true /*includingEntity*/, true /*mutate*/);
|
|
19
19
|
var isCollapsedSelection = segmentAndParagraphs.length >= 1 &&
|
|
20
20
|
segmentAndParagraphs.every(function (x) { return x[0].segmentType == 'SelectionMarker'; });
|
|
21
|
+
// 1. adjust selection to a word if selection is collapsed
|
|
21
22
|
if (isCollapsedSelection) {
|
|
22
23
|
var para_1 = segmentAndParagraphs[0][1];
|
|
23
24
|
var path_1 = segmentAndParagraphs[0][2];
|
|
@@ -26,24 +27,43 @@ function formatSegmentWithContentModel(editor, apiName, toggleStyleCallback, seg
|
|
|
26
27
|
isCollapsedSelection = false;
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
+
// 2. expand selection for entities if any
|
|
31
|
+
var formatsAndSegments = [];
|
|
32
|
+
var modelsFromEntities = [];
|
|
33
|
+
segmentAndParagraphs.forEach(function (item) {
|
|
34
|
+
if (item[0].segmentType == 'Entity') {
|
|
35
|
+
expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
formatsAndSegments.push([item[0].format, item[0], item[1]]);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
// 3. check if we should turn format on (when not all selection has the required format already)
|
|
42
|
+
// or off (all selections already have the required format)
|
|
30
43
|
var isTurningOff = segmentHasStyleCallback
|
|
31
44
|
? formatsAndSegments.every(function (_a) {
|
|
32
45
|
var _b = (0, tslib_1.__read)(_a, 3), format = _b[0], segment = _b[1], paragraph = _b[2];
|
|
33
46
|
return segmentHasStyleCallback(format, segment, paragraph);
|
|
34
47
|
})
|
|
35
48
|
: false;
|
|
49
|
+
// 4. invoke the callback function to apply the format
|
|
36
50
|
formatsAndSegments.forEach(function (_a) {
|
|
37
51
|
var _b = (0, tslib_1.__read)(_a, 3), format = _b[0], segment = _b[1], paragraph = _b[2];
|
|
38
52
|
toggleStyleCallback(format, !isTurningOff, segment, paragraph);
|
|
39
53
|
});
|
|
54
|
+
// 5. after format is applied to all selections, invoke another callback to do some clean up before write the change back
|
|
40
55
|
afterFormatCallback === null || afterFormatCallback === void 0 ? void 0 : afterFormatCallback(model);
|
|
56
|
+
// 6. finally merge segments if possible, to avoid fragmentation
|
|
41
57
|
formatsAndSegments.forEach(function (_a) {
|
|
42
58
|
var _b = (0, tslib_1.__read)(_a, 3), _ = _b[0], __ = _b[1], paragraph = _b[2];
|
|
43
59
|
if (paragraph) {
|
|
44
60
|
(0, roosterjs_content_model_dom_1.mergeTextSegments)(paragraph);
|
|
45
61
|
}
|
|
46
62
|
});
|
|
63
|
+
// 7. Write back models that we got from entities (if any)
|
|
64
|
+
writeBackEntities(editor, modelsFromEntities);
|
|
65
|
+
// 8. if the selection is still collapsed, it means we didn't actually applied format, set a pending format so it can be applied when user type
|
|
66
|
+
// otherwise, write back to editor
|
|
47
67
|
if (isCollapsedSelection) {
|
|
48
68
|
context.newPendingFormat = segmentAndParagraphs[0][0].format;
|
|
49
69
|
editor.focus();
|
|
@@ -57,4 +77,56 @@ function formatSegmentWithContentModel(editor, apiName, toggleStyleCallback, seg
|
|
|
57
77
|
});
|
|
58
78
|
}
|
|
59
79
|
exports.formatSegmentWithContentModel = formatSegmentWithContentModel;
|
|
80
|
+
function createEditorContextForEntity(editor, entity) {
|
|
81
|
+
var _a;
|
|
82
|
+
var domHelper = editor.getDOMHelper();
|
|
83
|
+
var context = {
|
|
84
|
+
isDarkMode: editor.isDarkMode(),
|
|
85
|
+
defaultFormat: (0, tslib_1.__assign)({}, entity.format),
|
|
86
|
+
darkColorHandler: editor.getColorManager(),
|
|
87
|
+
addDelimiterForEntity: false,
|
|
88
|
+
allowCacheElement: false,
|
|
89
|
+
domIndexer: undefined,
|
|
90
|
+
zoomScale: domHelper.calculateZoomScale(),
|
|
91
|
+
experimentalFeatures: [],
|
|
92
|
+
};
|
|
93
|
+
if (((_a = editor.getDocument().defaultView) === null || _a === void 0 ? void 0 : _a.getComputedStyle(entity.wrapper).direction) == 'rtl') {
|
|
94
|
+
context.isRootRtl = true;
|
|
95
|
+
}
|
|
96
|
+
return context;
|
|
97
|
+
}
|
|
98
|
+
function expandEntitySelections(editor, entity, formatsAndSegments, modelsFromEntities) {
|
|
99
|
+
var _a = entity.entityFormat, id = _a.id, type = _a.entityType, isReadonly = _a.isReadonly;
|
|
100
|
+
if (id && type) {
|
|
101
|
+
var formattableRoots = [];
|
|
102
|
+
var entityOperationEventData = {
|
|
103
|
+
entity: { id: id, type: type, isReadonly: !!isReadonly, wrapper: entity.wrapper },
|
|
104
|
+
operation: 'beforeFormat',
|
|
105
|
+
formattableRoots: formattableRoots,
|
|
106
|
+
};
|
|
107
|
+
editor.triggerEvent('entityOperation', entityOperationEventData);
|
|
108
|
+
formattableRoots.forEach(function (root) {
|
|
109
|
+
if (entity.wrapper.contains(root.element)) {
|
|
110
|
+
var editorContext = createEditorContextForEntity(editor, entity);
|
|
111
|
+
var context = (0, roosterjs_content_model_dom_1.createDomToModelContext)(editorContext, root.domToModelOptions);
|
|
112
|
+
// Treat everything as selected since the parent entity is selected
|
|
113
|
+
context.isInSelection = true;
|
|
114
|
+
var model = (0, roosterjs_content_model_dom_1.domToContentModel)(root.element, context);
|
|
115
|
+
var selections = (0, roosterjs_content_model_dom_1.getSelectedSegmentsAndParagraphs)(model, false /*includingFormatHolder*/, false /*includingEntity*/, true /*mutate*/);
|
|
116
|
+
selections.forEach(function (item) {
|
|
117
|
+
formatsAndSegments.push([item[0].format, item[0], item[1]]);
|
|
118
|
+
});
|
|
119
|
+
modelsFromEntities.push([entity, root, model]);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function writeBackEntities(editor, modelsFromEntities) {
|
|
125
|
+
modelsFromEntities.forEach(function (_a) {
|
|
126
|
+
var _b = (0, tslib_1.__read)(_a, 3), entity = _b[0], root = _b[1], model = _b[2];
|
|
127
|
+
var editorContext = createEditorContextForEntity(editor, entity);
|
|
128
|
+
var modelToDomContext = (0, roosterjs_content_model_dom_1.createModelToDomContext)(editorContext, root.modelToDomOptions);
|
|
129
|
+
(0, roosterjs_content_model_dom_1.contentModelToDom)(editor.getDocument(), root.element, model, modelToDomContext);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
60
132
|
//# sourceMappingURL=formatSegmentWithContentModel.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";;;;AAAA,oFAAmF;AACnF,2EAAkG;AASlG;;;;;;;;GAQG;AACH,SAAgB,6BAA6B,CACzC,MAAe,EACf,OAAe,EACf,mBAKS,EACT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AA4B,EAA3B,MAAM,QAAA,EAAE,OAAO,QAAA,EAAE,SAAS,QAAA;gBACjD,OAAA,uBAAuB,CAAC,MAAM,EAAE,OAAO,EAAE,SAAS,CAAC;YAAnD,CAAmD,CACtD;YACH,CAAC,CAAC,KAAK,CAAC;QAEZ,kBAAkB,CAAC,OAAO,CAAC,UAAC,EAA4B;gBAA5B,KAAA,0BAA4B,EAA3B,MAAM,QAAA,EAAE,OAAO,QAAA,EAAE,SAAS,QAAA;YACnD,mBAAmB,CAAC,MAAM,EAAE,CAAC,YAAY,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;QACnE,CAAC,CAAC,CAAC;QAEH,mBAAmB,aAAnB,mBAAmB,uBAAnB,mBAAmB,CAAG,KAAK,CAAC,CAAC;QAE7B,kBAAkB,CAAC,OAAO,CAAC,UAAC,EAAkB;gBAAlB,KAAA,0BAAkB,EAAjB,CAAC,QAAA,EAAE,EAAE,QAAA,EAAE,SAAS,QAAA;YACzC,IAAI,SAAS,EAAE;gBACX,IAAA,+CAAiB,EAAC,SAAS,CAAC,CAAC;aAChC;QACL,CAAC,CAAC,CAAC;QAEH,IAAI,oBAAoB,EAAE;YACtB,OAAO,CAAC,gBAAgB,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;YAC7D,MAAM,CAAC,KAAK,EAAE,CAAC;YACf,OAAO,KAAK,CAAC;SAChB;aAAM;YACH,OAAO,kBAAkB,CAAC,MAAM,GAAG,CAAC,CAAC;SACxC;IACL,CAAC,EACD;QACI,OAAO,SAAA;KACV,CACJ,CAAC;AACN,CAAC;AA/ED,sEA+EC","sourcesContent":["import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport { getSelectedSegmentsAndParagraphs, mergeTextSegments } from 'roosterjs-content-model-dom';\nimport type {\n ContentModelSegmentFormat,\n IEditor,\n ReadonlyContentModelDocument,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the selected segment using Content Model\n * @param editor The editor object\n * @param apiName Name of API this calling this function. This is mostly for logging.\n * @param toggleStyleCallback The callback to format the segment. It will be called with current selected table. If no table is selected, it will not be called.\n * @param segmentHasStyleCallback The callback used for checking if the given segment already has required format\n * @param includingFormatHolder True to also include format holder of list item when search selected segments\n * @param afterFormatCallback A callback to invoke after format is applied to all selected segments and before the change is applied to DOM tree\n */\nexport function formatSegmentWithContentModel(\n editor: IEditor,\n apiName: string,\n toggleStyleCallback: (\n format: ContentModelSegmentFormat,\n isTuringOn: boolean,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => void,\n segmentHasStyleCallback?: (\n format: ContentModelSegmentFormat,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => boolean,\n includingFormatHolder?: boolean,\n afterFormatCallback?: (model: ReadonlyContentModelDocument) => void\n) {\n editor.formatContentModel(\n (model, context) => {\n let segmentAndParagraphs = getSelectedSegmentsAndParagraphs(\n model,\n !!includingFormatHolder,\n false /*includingEntity*/,\n true /*mutate*/\n );\n let isCollapsedSelection =\n segmentAndParagraphs.length >= 1 &&\n segmentAndParagraphs.every(x => x[0].segmentType == 'SelectionMarker');\n\n if (isCollapsedSelection) {\n const para = segmentAndParagraphs[0][1];\n const path = segmentAndParagraphs[0][2];\n\n segmentAndParagraphs = adjustWordSelection(\n model,\n segmentAndParagraphs[0][0]\n ).map(x => [x, para, path]);\n\n if (segmentAndParagraphs.length > 1) {\n isCollapsedSelection = false;\n }\n }\n\n const formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][] = segmentAndParagraphs.map(item => [item[0].format, item[0], item[1]]);\n\n const isTurningOff = segmentHasStyleCallback\n ? formatsAndSegments.every(([format, segment, paragraph]) =>\n segmentHasStyleCallback(format, segment, paragraph)\n )\n : false;\n\n formatsAndSegments.forEach(([format, segment, paragraph]) => {\n toggleStyleCallback(format, !isTurningOff, segment, paragraph);\n });\n\n afterFormatCallback?.(model);\n\n formatsAndSegments.forEach(([_, __, paragraph]) => {\n if (paragraph) {\n mergeTextSegments(paragraph);\n }\n });\n\n if (isCollapsedSelection) {\n context.newPendingFormat = segmentAndParagraphs[0][0].format;\n editor.focus();\n return false;\n } else {\n return formatsAndSegments.length > 0;\n }\n },\n {\n apiName,\n }\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";;;;AAAA,oFAAmF;AACnF,2EAOqC;AAcrC;;;;;;;;GAQG;AACH,SAAgB,6BAA6B,CACzC,MAAe,EACf,OAAe,EACf,mBAKS,EACT,uBAIY,EACZ,qBAA+B,EAC/B,mBAAmE;IAEnE,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,IAAI,oBAAoB,GAAG,IAAA,8DAAgC,EACvD,KAAK,EACL,CAAC,CAAC,qBAAqB,EACvB,IAAI,CAAC,mBAAmB,EACxB,IAAI,CAAC,UAAU,CAClB,CAAC;QACF,IAAI,oBAAoB,GACpB,oBAAoB,CAAC,MAAM,IAAI,CAAC;YAChC,oBAAoB,CAAC,KAAK,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,IAAI,iBAAiB,EAArC,CAAqC,CAAC,CAAC;QAE3E,0DAA0D;QAC1D,IAAI,oBAAoB,EAAE;YACtB,IAAM,MAAI,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACxC,IAAM,MAAI,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAExC,oBAAoB,GAAG,IAAA,yCAAmB,EACtC,KAAK,EACL,oBAAoB,CAAC,CAAC,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AK,EACL,KAAK,CAAC,yBAAyB,EAC/B,KAAK,CAAC,mBAAmB,EACzB,IAAI,CAAC,UAAU,CAClB,CAAC;gBAEF,UAAU,CAAC,OAAO,CAAC,UAAA,IAAI;oBACnB,kBAAkB,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBAChE,CAAC,CAAC,CAAC;gBAEH,kBAAkB,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;aAClD;QACL,CAAC,CAAC,CAAC;KACN;AACL,CAAC;AAED,SAAS,iBAAiB,CACtB,MAAe,EACf,kBAAiF;IAEjF,kBAAkB,CAAC,OAAO,CAAC,UAAC,EAAqB;YAArB,KAAA,0BAAqB,EAApB,MAAM,QAAA,EAAE,IAAI,QAAA,EAAE,KAAK,QAAA;QAC5C,IAAM,aAAa,GAAG,4BAA4B,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACnE,IAAM,iBAAiB,GAAG,IAAA,qDAAuB,EAAC,aAAa,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;QAEzF,IAAA,+CAAiB,EAAC,MAAM,CAAC,WAAW,EAAE,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC;IACpF,CAAC,CAAC,CAAC;AACP,CAAC","sourcesContent":["import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport {\n contentModelToDom,\n createDomToModelContext,\n createModelToDomContext,\n domToContentModel,\n getSelectedSegmentsAndParagraphs,\n mergeTextSegments,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelDocument,\n ContentModelEntity,\n ContentModelSegmentFormat,\n EditorContext,\n FormattableRoot,\n IEditor,\n PluginEventData,\n ReadonlyContentModelDocument,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the selected segment using Content Model\n * @param editor The editor object\n * @param apiName Name of API this calling this function. This is mostly for logging.\n * @param toggleStyleCallback The callback to format the segment. It will be called with current selected table. If no table is selected, it will not be called.\n * @param segmentHasStyleCallback The callback used for checking if the given segment already has required format\n * @param includingFormatHolder True to also include format holder of list item when search selected segments\n * @param afterFormatCallback A callback to invoke after format is applied to all selected segments and before the change is applied to DOM tree\n */\nexport function formatSegmentWithContentModel(\n editor: IEditor,\n apiName: string,\n toggleStyleCallback: (\n format: ContentModelSegmentFormat,\n isTuringOn: boolean,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => void,\n segmentHasStyleCallback?: (\n format: ContentModelSegmentFormat,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => boolean,\n includingFormatHolder?: boolean,\n afterFormatCallback?: (model: ReadonlyContentModelDocument) => void\n) {\n editor.formatContentModel(\n (model, context) => {\n let segmentAndParagraphs = getSelectedSegmentsAndParagraphs(\n model,\n !!includingFormatHolder,\n true /*includingEntity*/,\n true /*mutate*/\n );\n let isCollapsedSelection =\n segmentAndParagraphs.length >= 1 &&\n segmentAndParagraphs.every(x => x[0].segmentType == 'SelectionMarker');\n\n // 1. adjust selection to a word if selection is collapsed\n if (isCollapsedSelection) {\n const para = segmentAndParagraphs[0][1];\n const path = segmentAndParagraphs[0][2];\n\n segmentAndParagraphs = adjustWordSelection(\n model,\n segmentAndParagraphs[0][0]\n ).map(x => [x, para, path]);\n\n if (segmentAndParagraphs.length > 1) {\n isCollapsedSelection = false;\n }\n }\n\n // 2. expand selection for entities if any\n const formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][] = [];\n const modelsFromEntities: [\n ContentModelEntity,\n FormattableRoot,\n ContentModelDocument\n ][] = [];\n\n segmentAndParagraphs.forEach(item => {\n if (item[0].segmentType == 'Entity') {\n expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities);\n } else {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n }\n });\n\n // 3. check if we should turn format on (when not all selection has the required format already)\n // or off (all selections already have the required format)\n const isTurningOff = segmentHasStyleCallback\n ? formatsAndSegments.every(([format, segment, paragraph]) =>\n segmentHasStyleCallback(format, segment, paragraph)\n )\n : false;\n\n // 4. invoke the callback function to apply the format\n formatsAndSegments.forEach(([format, segment, paragraph]) => {\n toggleStyleCallback(format, !isTurningOff, segment, paragraph);\n });\n\n // 5. after format is applied to all selections, invoke another callback to do some clean up before write the change back\n afterFormatCallback?.(model);\n\n // 6. finally merge segments if possible, to avoid fragmentation\n formatsAndSegments.forEach(([_, __, paragraph]) => {\n if (paragraph) {\n mergeTextSegments(paragraph);\n }\n });\n\n // 7. Write back models that we got from entities (if any)\n writeBackEntities(editor, modelsFromEntities);\n\n // 8. if the selection is still collapsed, it means we didn't actually applied format, set a pending format so it can be applied when user type\n // otherwise, write back to editor\n if (isCollapsedSelection) {\n context.newPendingFormat = segmentAndParagraphs[0][0].format;\n editor.focus();\n return false;\n } else {\n return formatsAndSegments.length > 0;\n }\n },\n {\n apiName,\n }\n );\n}\n\nfunction createEditorContextForEntity(editor: IEditor, entity: ContentModelEntity): EditorContext {\n const domHelper = editor.getDOMHelper();\n const context: EditorContext = {\n isDarkMode: editor.isDarkMode(),\n defaultFormat: { ...entity.format },\n darkColorHandler: editor.getColorManager(),\n addDelimiterForEntity: false,\n allowCacheElement: false,\n domIndexer: undefined,\n zoomScale: domHelper.calculateZoomScale(),\n experimentalFeatures: [],\n };\n\n if (editor.getDocument().defaultView?.getComputedStyle(entity.wrapper).direction == 'rtl') {\n context.isRootRtl = true;\n }\n\n return context;\n}\n\nfunction expandEntitySelections(\n editor: IEditor,\n entity: ContentModelEntity,\n formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][],\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n const { id, entityType: type, isReadonly } = entity.entityFormat;\n\n if (id && type) {\n const formattableRoots: FormattableRoot[] = [];\n const entityOperationEventData: PluginEventData<'entityOperation'> = {\n entity: { id, type, isReadonly: !!isReadonly, wrapper: entity.wrapper },\n operation: 'beforeFormat',\n formattableRoots,\n };\n\n editor.triggerEvent('entityOperation', entityOperationEventData);\n\n formattableRoots.forEach(root => {\n if (entity.wrapper.contains(root.element)) {\n const editorContext = createEditorContextForEntity(editor, entity);\n const context = createDomToModelContext(editorContext, root.domToModelOptions);\n\n // Treat everything as selected since the parent entity is selected\n context.isInSelection = true;\n\n const model = domToContentModel(root.element, context);\n const selections = getSelectedSegmentsAndParagraphs(\n model,\n false /*includingFormatHolder*/,\n false /*includingEntity*/,\n true /*mutate*/\n );\n\n selections.forEach(item => {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n });\n\n modelsFromEntities.push([entity, root, model]);\n }\n });\n }\n}\n\nfunction writeBackEntities(\n editor: IEditor,\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n modelsFromEntities.forEach(([entity, root, model]) => {\n const editorContext = createEditorContextForEntity(editor, entity);\n const modelToDomContext = createModelToDomContext(editorContext, root.modelToDomOptions);\n\n contentModelToDom(editor.getDocument(), root.element, model, modelToDomContext);\n });\n}\n"]}
|
|
@@ -13,9 +13,10 @@ define(["require", "exports", "tslib", "../../modelApi/selection/adjustWordSelec
|
|
|
13
13
|
*/
|
|
14
14
|
function formatSegmentWithContentModel(editor, apiName, toggleStyleCallback, segmentHasStyleCallback, includingFormatHolder, afterFormatCallback) {
|
|
15
15
|
editor.formatContentModel(function (model, context) {
|
|
16
|
-
var segmentAndParagraphs = (0, roosterjs_content_model_dom_1.getSelectedSegmentsAndParagraphs)(model, !!includingFormatHolder,
|
|
16
|
+
var segmentAndParagraphs = (0, roosterjs_content_model_dom_1.getSelectedSegmentsAndParagraphs)(model, !!includingFormatHolder, true /*includingEntity*/, true /*mutate*/);
|
|
17
17
|
var isCollapsedSelection = segmentAndParagraphs.length >= 1 &&
|
|
18
18
|
segmentAndParagraphs.every(function (x) { return x[0].segmentType == 'SelectionMarker'; });
|
|
19
|
+
// 1. adjust selection to a word if selection is collapsed
|
|
19
20
|
if (isCollapsedSelection) {
|
|
20
21
|
var para_1 = segmentAndParagraphs[0][1];
|
|
21
22
|
var path_1 = segmentAndParagraphs[0][2];
|
|
@@ -24,24 +25,43 @@ define(["require", "exports", "tslib", "../../modelApi/selection/adjustWordSelec
|
|
|
24
25
|
isCollapsedSelection = false;
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
|
-
|
|
28
|
+
// 2. expand selection for entities if any
|
|
29
|
+
var formatsAndSegments = [];
|
|
30
|
+
var modelsFromEntities = [];
|
|
31
|
+
segmentAndParagraphs.forEach(function (item) {
|
|
32
|
+
if (item[0].segmentType == 'Entity') {
|
|
33
|
+
expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
formatsAndSegments.push([item[0].format, item[0], item[1]]);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
// 3. check if we should turn format on (when not all selection has the required format already)
|
|
40
|
+
// or off (all selections already have the required format)
|
|
28
41
|
var isTurningOff = segmentHasStyleCallback
|
|
29
42
|
? formatsAndSegments.every(function (_a) {
|
|
30
43
|
var _b = (0, tslib_1.__read)(_a, 3), format = _b[0], segment = _b[1], paragraph = _b[2];
|
|
31
44
|
return segmentHasStyleCallback(format, segment, paragraph);
|
|
32
45
|
})
|
|
33
46
|
: false;
|
|
47
|
+
// 4. invoke the callback function to apply the format
|
|
34
48
|
formatsAndSegments.forEach(function (_a) {
|
|
35
49
|
var _b = (0, tslib_1.__read)(_a, 3), format = _b[0], segment = _b[1], paragraph = _b[2];
|
|
36
50
|
toggleStyleCallback(format, !isTurningOff, segment, paragraph);
|
|
37
51
|
});
|
|
52
|
+
// 5. after format is applied to all selections, invoke another callback to do some clean up before write the change back
|
|
38
53
|
afterFormatCallback === null || afterFormatCallback === void 0 ? void 0 : afterFormatCallback(model);
|
|
54
|
+
// 6. finally merge segments if possible, to avoid fragmentation
|
|
39
55
|
formatsAndSegments.forEach(function (_a) {
|
|
40
56
|
var _b = (0, tslib_1.__read)(_a, 3), _ = _b[0], __ = _b[1], paragraph = _b[2];
|
|
41
57
|
if (paragraph) {
|
|
42
58
|
(0, roosterjs_content_model_dom_1.mergeTextSegments)(paragraph);
|
|
43
59
|
}
|
|
44
60
|
});
|
|
61
|
+
// 7. Write back models that we got from entities (if any)
|
|
62
|
+
writeBackEntities(editor, modelsFromEntities);
|
|
63
|
+
// 8. if the selection is still collapsed, it means we didn't actually applied format, set a pending format so it can be applied when user type
|
|
64
|
+
// otherwise, write back to editor
|
|
45
65
|
if (isCollapsedSelection) {
|
|
46
66
|
context.newPendingFormat = segmentAndParagraphs[0][0].format;
|
|
47
67
|
editor.focus();
|
|
@@ -55,5 +75,57 @@ define(["require", "exports", "tslib", "../../modelApi/selection/adjustWordSelec
|
|
|
55
75
|
});
|
|
56
76
|
}
|
|
57
77
|
exports.formatSegmentWithContentModel = formatSegmentWithContentModel;
|
|
78
|
+
function createEditorContextForEntity(editor, entity) {
|
|
79
|
+
var _a;
|
|
80
|
+
var domHelper = editor.getDOMHelper();
|
|
81
|
+
var context = {
|
|
82
|
+
isDarkMode: editor.isDarkMode(),
|
|
83
|
+
defaultFormat: (0, tslib_1.__assign)({}, entity.format),
|
|
84
|
+
darkColorHandler: editor.getColorManager(),
|
|
85
|
+
addDelimiterForEntity: false,
|
|
86
|
+
allowCacheElement: false,
|
|
87
|
+
domIndexer: undefined,
|
|
88
|
+
zoomScale: domHelper.calculateZoomScale(),
|
|
89
|
+
experimentalFeatures: [],
|
|
90
|
+
};
|
|
91
|
+
if (((_a = editor.getDocument().defaultView) === null || _a === void 0 ? void 0 : _a.getComputedStyle(entity.wrapper).direction) == 'rtl') {
|
|
92
|
+
context.isRootRtl = true;
|
|
93
|
+
}
|
|
94
|
+
return context;
|
|
95
|
+
}
|
|
96
|
+
function expandEntitySelections(editor, entity, formatsAndSegments, modelsFromEntities) {
|
|
97
|
+
var _a = entity.entityFormat, id = _a.id, type = _a.entityType, isReadonly = _a.isReadonly;
|
|
98
|
+
if (id && type) {
|
|
99
|
+
var formattableRoots = [];
|
|
100
|
+
var entityOperationEventData = {
|
|
101
|
+
entity: { id: id, type: type, isReadonly: !!isReadonly, wrapper: entity.wrapper },
|
|
102
|
+
operation: 'beforeFormat',
|
|
103
|
+
formattableRoots: formattableRoots,
|
|
104
|
+
};
|
|
105
|
+
editor.triggerEvent('entityOperation', entityOperationEventData);
|
|
106
|
+
formattableRoots.forEach(function (root) {
|
|
107
|
+
if (entity.wrapper.contains(root.element)) {
|
|
108
|
+
var editorContext = createEditorContextForEntity(editor, entity);
|
|
109
|
+
var context = (0, roosterjs_content_model_dom_1.createDomToModelContext)(editorContext, root.domToModelOptions);
|
|
110
|
+
// Treat everything as selected since the parent entity is selected
|
|
111
|
+
context.isInSelection = true;
|
|
112
|
+
var model = (0, roosterjs_content_model_dom_1.domToContentModel)(root.element, context);
|
|
113
|
+
var selections = (0, roosterjs_content_model_dom_1.getSelectedSegmentsAndParagraphs)(model, false /*includingFormatHolder*/, false /*includingEntity*/, true /*mutate*/);
|
|
114
|
+
selections.forEach(function (item) {
|
|
115
|
+
formatsAndSegments.push([item[0].format, item[0], item[1]]);
|
|
116
|
+
});
|
|
117
|
+
modelsFromEntities.push([entity, root, model]);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function writeBackEntities(editor, modelsFromEntities) {
|
|
123
|
+
modelsFromEntities.forEach(function (_a) {
|
|
124
|
+
var _b = (0, tslib_1.__read)(_a, 3), entity = _b[0], root = _b[1], model = _b[2];
|
|
125
|
+
var editorContext = createEditorContextForEntity(editor, entity);
|
|
126
|
+
var modelToDomContext = (0, roosterjs_content_model_dom_1.createModelToDomContext)(editorContext, root.modelToDomOptions);
|
|
127
|
+
(0, roosterjs_content_model_dom_1.contentModelToDom)(editor.getDocument(), root.element, model, modelToDomContext);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
58
130
|
});
|
|
59
131
|
//# sourceMappingURL=formatSegmentWithContentModel.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";;;;IAUA;;;;;;;;OAQG;IACH,SAAgB,6BAA6B,CACzC,MAAe,EACf,OAAe,EACf,mBAKS,EACT,uBAIY,EACZ,qBAA+B,EAC/B,mBAAmE;QAEnE,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;YACX,IAAI,oBAAoB,GAAG,IAAA,8DAAgC,EACvD,KAAK,EACL,CAAC,CAAC,qBAAqB,EACvB,KAAK,CAAC,mBAAmB,EACzB,IAAI,CAAC,UAAU,CAClB,CAAC;YACF,IAAI,oBAAoB,GACpB,oBAAoB,CAAC,MAAM,IAAI,CAAC;gBAChC,oBAAoB,CAAC,KAAK,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,IAAI,iBAAiB,EAArC,CAAqC,CAAC,CAAC;YAE3E,IAAI,oBAAoB,EAAE;gBACtB,IAAM,MAAI,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBACxC,IAAM,MAAI,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBAExC,oBAAoB,GAAG,IAAA,yCAAmB,EACtC,KAAK,EACL,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAC7B,CAAC,GAAG,CAAC,UAAA,CAAC,IAA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,OAAO,CAAC,gBAAgB,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;gBAC7D,MAAM,CAAC,KAAK,EAAE,CAAC;gBACf,OAAO,KAAK,CAAC;aAChB;iBAAM;gBACH,OAAO,kBAAkB,CAAC,MAAM,GAAG,CAAC,CAAC;aACxC;QACL,CAAC,EACD;YACI,OAAO,SAAA;SACV,CACJ,CAAC;IACN,CAAC;IA/ED,sEA+EC","sourcesContent":["import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport { getSelectedSegmentsAndParagraphs, mergeTextSegments } from 'roosterjs-content-model-dom';\nimport type {\n ContentModelSegmentFormat,\n IEditor,\n ReadonlyContentModelDocument,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the selected segment using Content Model\n * @param editor The editor object\n * @param apiName Name of API this calling this function. This is mostly for logging.\n * @param toggleStyleCallback The callback to format the segment. It will be called with current selected table. If no table is selected, it will not be called.\n * @param segmentHasStyleCallback The callback used for checking if the given segment already has required format\n * @param includingFormatHolder True to also include format holder of list item when search selected segments\n * @param afterFormatCallback A callback to invoke after format is applied to all selected segments and before the change is applied to DOM tree\n */\nexport function formatSegmentWithContentModel(\n editor: IEditor,\n apiName: string,\n toggleStyleCallback: (\n format: ContentModelSegmentFormat,\n isTuringOn: boolean,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => void,\n segmentHasStyleCallback?: (\n format: ContentModelSegmentFormat,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => boolean,\n includingFormatHolder?: boolean,\n afterFormatCallback?: (model: ReadonlyContentModelDocument) => void\n) {\n editor.formatContentModel(\n (model, context) => {\n let segmentAndParagraphs = getSelectedSegmentsAndParagraphs(\n model,\n !!includingFormatHolder,\n false /*includingEntity*/,\n true /*mutate*/\n );\n let isCollapsedSelection =\n segmentAndParagraphs.length >= 1 &&\n segmentAndParagraphs.every(x => x[0].segmentType == 'SelectionMarker');\n\n if (isCollapsedSelection) {\n const para = segmentAndParagraphs[0][1];\n const path = segmentAndParagraphs[0][2];\n\n segmentAndParagraphs = adjustWordSelection(\n model,\n segmentAndParagraphs[0][0]\n ).map(x => [x, para, path]);\n\n if (segmentAndParagraphs.length > 1) {\n isCollapsedSelection = false;\n }\n }\n\n const formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][] = segmentAndParagraphs.map(item => [item[0].format, item[0], item[1]]);\n\n const isTurningOff = segmentHasStyleCallback\n ? formatsAndSegments.every(([format, segment, paragraph]) =>\n segmentHasStyleCallback(format, segment, paragraph)\n )\n : false;\n\n formatsAndSegments.forEach(([format, segment, paragraph]) => {\n toggleStyleCallback(format, !isTurningOff, segment, paragraph);\n });\n\n afterFormatCallback?.(model);\n\n formatsAndSegments.forEach(([_, __, paragraph]) => {\n if (paragraph) {\n mergeTextSegments(paragraph);\n }\n });\n\n if (isCollapsedSelection) {\n context.newPendingFormat = segmentAndParagraphs[0][0].format;\n editor.focus();\n return false;\n } else {\n return formatsAndSegments.length > 0;\n }\n },\n {\n apiName,\n }\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";;;;IAsBA;;;;;;;;OAQG;IACH,SAAgB,6BAA6B,CACzC,MAAe,EACf,OAAe,EACf,mBAKS,EACT,uBAIY,EACZ,qBAA+B,EAC/B,mBAAmE;QAEnE,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;YACX,IAAI,oBAAoB,GAAG,IAAA,8DAAgC,EACvD,KAAK,EACL,CAAC,CAAC,qBAAqB,EACvB,IAAI,CAAC,mBAAmB,EACxB,IAAI,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B,EAAC,aAAa,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;YAEzF,IAAA,+CAAiB,EAAC,MAAM,CAAC,WAAW,EAAE,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC;QACpF,CAAC,CAAC,CAAC;IACP,CAAC","sourcesContent":["import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport {\n contentModelToDom,\n createDomToModelContext,\n createModelToDomContext,\n domToContentModel,\n getSelectedSegmentsAndParagraphs,\n mergeTextSegments,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelDocument,\n ContentModelEntity,\n ContentModelSegmentFormat,\n EditorContext,\n FormattableRoot,\n IEditor,\n PluginEventData,\n ReadonlyContentModelDocument,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the selected segment using Content Model\n * @param editor The editor object\n * @param apiName Name of API this calling this function. This is mostly for logging.\n * @param toggleStyleCallback The callback to format the segment. It will be called with current selected table. If no table is selected, it will not be called.\n * @param segmentHasStyleCallback The callback used for checking if the given segment already has required format\n * @param includingFormatHolder True to also include format holder of list item when search selected segments\n * @param afterFormatCallback A callback to invoke after format is applied to all selected segments and before the change is applied to DOM tree\n */\nexport function formatSegmentWithContentModel(\n editor: IEditor,\n apiName: string,\n toggleStyleCallback: (\n format: ContentModelSegmentFormat,\n isTuringOn: boolean,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => void,\n segmentHasStyleCallback?: (\n format: ContentModelSegmentFormat,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => boolean,\n includingFormatHolder?: boolean,\n afterFormatCallback?: (model: ReadonlyContentModelDocument) => void\n) {\n editor.formatContentModel(\n (model, context) => {\n let segmentAndParagraphs = getSelectedSegmentsAndParagraphs(\n model,\n !!includingFormatHolder,\n true /*includingEntity*/,\n true /*mutate*/\n );\n let isCollapsedSelection =\n segmentAndParagraphs.length >= 1 &&\n segmentAndParagraphs.every(x => x[0].segmentType == 'SelectionMarker');\n\n // 1. adjust selection to a word if selection is collapsed\n if (isCollapsedSelection) {\n const para = segmentAndParagraphs[0][1];\n const path = segmentAndParagraphs[0][2];\n\n segmentAndParagraphs = adjustWordSelection(\n model,\n segmentAndParagraphs[0][0]\n ).map(x => [x, para, path]);\n\n if (segmentAndParagraphs.length > 1) {\n isCollapsedSelection = false;\n }\n }\n\n // 2. expand selection for entities if any\n const formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][] = [];\n const modelsFromEntities: [\n ContentModelEntity,\n FormattableRoot,\n ContentModelDocument\n ][] = [];\n\n segmentAndParagraphs.forEach(item => {\n if (item[0].segmentType == 'Entity') {\n expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities);\n } else {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n }\n });\n\n // 3. check if we should turn format on (when not all selection has the required format already)\n // or off (all selections already have the required format)\n const isTurningOff = segmentHasStyleCallback\n ? formatsAndSegments.every(([format, segment, paragraph]) =>\n segmentHasStyleCallback(format, segment, paragraph)\n )\n : false;\n\n // 4. invoke the callback function to apply the format\n formatsAndSegments.forEach(([format, segment, paragraph]) => {\n toggleStyleCallback(format, !isTurningOff, segment, paragraph);\n });\n\n // 5. after format is applied to all selections, invoke another callback to do some clean up before write the change back\n afterFormatCallback?.(model);\n\n // 6. finally merge segments if possible, to avoid fragmentation\n formatsAndSegments.forEach(([_, __, paragraph]) => {\n if (paragraph) {\n mergeTextSegments(paragraph);\n }\n });\n\n // 7. Write back models that we got from entities (if any)\n writeBackEntities(editor, modelsFromEntities);\n\n // 8. if the selection is still collapsed, it means we didn't actually applied format, set a pending format so it can be applied when user type\n // otherwise, write back to editor\n if (isCollapsedSelection) {\n context.newPendingFormat = segmentAndParagraphs[0][0].format;\n editor.focus();\n return false;\n } else {\n return formatsAndSegments.length > 0;\n }\n },\n {\n apiName,\n }\n );\n}\n\nfunction createEditorContextForEntity(editor: IEditor, entity: ContentModelEntity): EditorContext {\n const domHelper = editor.getDOMHelper();\n const context: EditorContext = {\n isDarkMode: editor.isDarkMode(),\n defaultFormat: { ...entity.format },\n darkColorHandler: editor.getColorManager(),\n addDelimiterForEntity: false,\n allowCacheElement: false,\n domIndexer: undefined,\n zoomScale: domHelper.calculateZoomScale(),\n experimentalFeatures: [],\n };\n\n if (editor.getDocument().defaultView?.getComputedStyle(entity.wrapper).direction == 'rtl') {\n context.isRootRtl = true;\n }\n\n return context;\n}\n\nfunction expandEntitySelections(\n editor: IEditor,\n entity: ContentModelEntity,\n formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][],\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n const { id, entityType: type, isReadonly } = entity.entityFormat;\n\n if (id && type) {\n const formattableRoots: FormattableRoot[] = [];\n const entityOperationEventData: PluginEventData<'entityOperation'> = {\n entity: { id, type, isReadonly: !!isReadonly, wrapper: entity.wrapper },\n operation: 'beforeFormat',\n formattableRoots,\n };\n\n editor.triggerEvent('entityOperation', entityOperationEventData);\n\n formattableRoots.forEach(root => {\n if (entity.wrapper.contains(root.element)) {\n const editorContext = createEditorContextForEntity(editor, entity);\n const context = createDomToModelContext(editorContext, root.domToModelOptions);\n\n // Treat everything as selected since the parent entity is selected\n context.isInSelection = true;\n\n const model = domToContentModel(root.element, context);\n const selections = getSelectedSegmentsAndParagraphs(\n model,\n false /*includingFormatHolder*/,\n false /*includingEntity*/,\n true /*mutate*/\n );\n\n selections.forEach(item => {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n });\n\n modelsFromEntities.push([entity, root, model]);\n }\n });\n }\n}\n\nfunction writeBackEntities(\n editor: IEditor,\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n modelsFromEntities.forEach(([entity, root, model]) => {\n const editorContext = createEditorContextForEntity(editor, entity);\n const modelToDomContext = createModelToDomContext(editorContext, root.modelToDomOptions);\n\n contentModelToDom(editor.getDocument(), root.element, model, modelToDomContext);\n });\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { __read } from "tslib";
|
|
1
|
+
import { __assign, __read } from "tslib";
|
|
2
2
|
import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';
|
|
3
|
-
import { getSelectedSegmentsAndParagraphs, mergeTextSegments } from 'roosterjs-content-model-dom';
|
|
3
|
+
import { contentModelToDom, createDomToModelContext, createModelToDomContext, domToContentModel, getSelectedSegmentsAndParagraphs, mergeTextSegments, } from 'roosterjs-content-model-dom';
|
|
4
4
|
/**
|
|
5
5
|
* Invoke a callback to format the selected segment using Content Model
|
|
6
6
|
* @param editor The editor object
|
|
@@ -12,9 +12,10 @@ import { getSelectedSegmentsAndParagraphs, mergeTextSegments } from 'roosterjs-c
|
|
|
12
12
|
*/
|
|
13
13
|
export function formatSegmentWithContentModel(editor, apiName, toggleStyleCallback, segmentHasStyleCallback, includingFormatHolder, afterFormatCallback) {
|
|
14
14
|
editor.formatContentModel(function (model, context) {
|
|
15
|
-
var segmentAndParagraphs = getSelectedSegmentsAndParagraphs(model, !!includingFormatHolder,
|
|
15
|
+
var segmentAndParagraphs = getSelectedSegmentsAndParagraphs(model, !!includingFormatHolder, true /*includingEntity*/, true /*mutate*/);
|
|
16
16
|
var isCollapsedSelection = segmentAndParagraphs.length >= 1 &&
|
|
17
17
|
segmentAndParagraphs.every(function (x) { return x[0].segmentType == 'SelectionMarker'; });
|
|
18
|
+
// 1. adjust selection to a word if selection is collapsed
|
|
18
19
|
if (isCollapsedSelection) {
|
|
19
20
|
var para_1 = segmentAndParagraphs[0][1];
|
|
20
21
|
var path_1 = segmentAndParagraphs[0][2];
|
|
@@ -23,24 +24,43 @@ export function formatSegmentWithContentModel(editor, apiName, toggleStyleCallba
|
|
|
23
24
|
isCollapsedSelection = false;
|
|
24
25
|
}
|
|
25
26
|
}
|
|
26
|
-
|
|
27
|
+
// 2. expand selection for entities if any
|
|
28
|
+
var formatsAndSegments = [];
|
|
29
|
+
var modelsFromEntities = [];
|
|
30
|
+
segmentAndParagraphs.forEach(function (item) {
|
|
31
|
+
if (item[0].segmentType == 'Entity') {
|
|
32
|
+
expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
formatsAndSegments.push([item[0].format, item[0], item[1]]);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
// 3. check if we should turn format on (when not all selection has the required format already)
|
|
39
|
+
// or off (all selections already have the required format)
|
|
27
40
|
var isTurningOff = segmentHasStyleCallback
|
|
28
41
|
? formatsAndSegments.every(function (_a) {
|
|
29
42
|
var _b = __read(_a, 3), format = _b[0], segment = _b[1], paragraph = _b[2];
|
|
30
43
|
return segmentHasStyleCallback(format, segment, paragraph);
|
|
31
44
|
})
|
|
32
45
|
: false;
|
|
46
|
+
// 4. invoke the callback function to apply the format
|
|
33
47
|
formatsAndSegments.forEach(function (_a) {
|
|
34
48
|
var _b = __read(_a, 3), format = _b[0], segment = _b[1], paragraph = _b[2];
|
|
35
49
|
toggleStyleCallback(format, !isTurningOff, segment, paragraph);
|
|
36
50
|
});
|
|
51
|
+
// 5. after format is applied to all selections, invoke another callback to do some clean up before write the change back
|
|
37
52
|
afterFormatCallback === null || afterFormatCallback === void 0 ? void 0 : afterFormatCallback(model);
|
|
53
|
+
// 6. finally merge segments if possible, to avoid fragmentation
|
|
38
54
|
formatsAndSegments.forEach(function (_a) {
|
|
39
55
|
var _b = __read(_a, 3), _ = _b[0], __ = _b[1], paragraph = _b[2];
|
|
40
56
|
if (paragraph) {
|
|
41
57
|
mergeTextSegments(paragraph);
|
|
42
58
|
}
|
|
43
59
|
});
|
|
60
|
+
// 7. Write back models that we got from entities (if any)
|
|
61
|
+
writeBackEntities(editor, modelsFromEntities);
|
|
62
|
+
// 8. if the selection is still collapsed, it means we didn't actually applied format, set a pending format so it can be applied when user type
|
|
63
|
+
// otherwise, write back to editor
|
|
44
64
|
if (isCollapsedSelection) {
|
|
45
65
|
context.newPendingFormat = segmentAndParagraphs[0][0].format;
|
|
46
66
|
editor.focus();
|
|
@@ -53,4 +73,56 @@ export function formatSegmentWithContentModel(editor, apiName, toggleStyleCallba
|
|
|
53
73
|
apiName: apiName,
|
|
54
74
|
});
|
|
55
75
|
}
|
|
76
|
+
function createEditorContextForEntity(editor, entity) {
|
|
77
|
+
var _a;
|
|
78
|
+
var domHelper = editor.getDOMHelper();
|
|
79
|
+
var context = {
|
|
80
|
+
isDarkMode: editor.isDarkMode(),
|
|
81
|
+
defaultFormat: __assign({}, entity.format),
|
|
82
|
+
darkColorHandler: editor.getColorManager(),
|
|
83
|
+
addDelimiterForEntity: false,
|
|
84
|
+
allowCacheElement: false,
|
|
85
|
+
domIndexer: undefined,
|
|
86
|
+
zoomScale: domHelper.calculateZoomScale(),
|
|
87
|
+
experimentalFeatures: [],
|
|
88
|
+
};
|
|
89
|
+
if (((_a = editor.getDocument().defaultView) === null || _a === void 0 ? void 0 : _a.getComputedStyle(entity.wrapper).direction) == 'rtl') {
|
|
90
|
+
context.isRootRtl = true;
|
|
91
|
+
}
|
|
92
|
+
return context;
|
|
93
|
+
}
|
|
94
|
+
function expandEntitySelections(editor, entity, formatsAndSegments, modelsFromEntities) {
|
|
95
|
+
var _a = entity.entityFormat, id = _a.id, type = _a.entityType, isReadonly = _a.isReadonly;
|
|
96
|
+
if (id && type) {
|
|
97
|
+
var formattableRoots = [];
|
|
98
|
+
var entityOperationEventData = {
|
|
99
|
+
entity: { id: id, type: type, isReadonly: !!isReadonly, wrapper: entity.wrapper },
|
|
100
|
+
operation: 'beforeFormat',
|
|
101
|
+
formattableRoots: formattableRoots,
|
|
102
|
+
};
|
|
103
|
+
editor.triggerEvent('entityOperation', entityOperationEventData);
|
|
104
|
+
formattableRoots.forEach(function (root) {
|
|
105
|
+
if (entity.wrapper.contains(root.element)) {
|
|
106
|
+
var editorContext = createEditorContextForEntity(editor, entity);
|
|
107
|
+
var context = createDomToModelContext(editorContext, root.domToModelOptions);
|
|
108
|
+
// Treat everything as selected since the parent entity is selected
|
|
109
|
+
context.isInSelection = true;
|
|
110
|
+
var model = domToContentModel(root.element, context);
|
|
111
|
+
var selections = getSelectedSegmentsAndParagraphs(model, false /*includingFormatHolder*/, false /*includingEntity*/, true /*mutate*/);
|
|
112
|
+
selections.forEach(function (item) {
|
|
113
|
+
formatsAndSegments.push([item[0].format, item[0], item[1]]);
|
|
114
|
+
});
|
|
115
|
+
modelsFromEntities.push([entity, root, model]);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function writeBackEntities(editor, modelsFromEntities) {
|
|
121
|
+
modelsFromEntities.forEach(function (_a) {
|
|
122
|
+
var _b = __read(_a, 3), entity = _b[0], root = _b[1], model = _b[2];
|
|
123
|
+
var editorContext = createEditorContextForEntity(editor, entity);
|
|
124
|
+
var modelToDomContext = createModelToDomContext(editorContext, root.modelToDomOptions);
|
|
125
|
+
contentModelToDom(editor.getDocument(), root.element, model, modelToDomContext);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
56
128
|
//# sourceMappingURL=formatSegmentWithContentModel.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAC;AACnF,OAAO,EAAE,gCAAgC,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AASlG;;;;;;;;GAQG;AACH,MAAM,UAAU,6BAA6B,CACzC,MAAe,EACf,OAAe,EACf,mBAKS,EACT,uB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M,QAAA,EAAE,OAAO,QAAA,EAAE,SAAS,QAAA;gBACjD,OAAA,uBAAuB,CAAC,MAAM,EAAE,OAAO,EAAE,SAAS,CAAC;YAAnD,CAAmD,CACtD;YACH,CAAC,CAAC,KAAK,CAAC;QAEZ,kBAAkB,CAAC,OAAO,CAAC,UAAC,EAA4B;gBAA5B,KAAA,aAA4B,EAA3B,MAAM,QAAA,EAAE,OAAO,QAAA,EAAE,SAAS,QAAA;YACnD,mBAAmB,CAAC,MAAM,EAAE,CAAC,YAAY,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;QACnE,CAAC,CAAC,CAAC;QAEH,mBAAmB,aAAnB,mBAAmB,uBAAnB,mBAAmB,CAAG,KAAK,CAAC,CAAC;QAE7B,kBAAkB,CAAC,OAAO,CAAC,UAAC,EAAkB;gBAAlB,KAAA,aAAkB,EAAjB,CAAC,QAAA,EAAE,EAAE,QAAA,EAAE,SAAS,QAAA;YACzC,IAAI,SAAS,EAAE;gBACX,iBAAiB,CAAC,SAAS,CAAC,CAAC;aAChC;QACL,CAAC,CAAC,CAAC;QAEH,IAAI,oBAAoB,EAAE;YACtB,OAAO,CAAC,gBAAgB,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;YAC7D,MAAM,CAAC,KAAK,EAAE,CAAC;YACf,OAAO,KAAK,CAAC;SAChB;aAAM;YACH,OAAO,kBAAkB,CAAC,MAAM,GAAG,CAAC,CAAC;SACxC;IACL,CAAC,EACD;QACI,OAAO,SAAA;KACV,CACJ,CAAC;AACN,CAAC","sourcesContent":["import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport { getSelectedSegmentsAndParagraphs, mergeTextSegments } from 'roosterjs-content-model-dom';\nimport type {\n ContentModelSegmentFormat,\n IEditor,\n ReadonlyContentModelDocument,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the selected segment using Content Model\n * @param editor The editor object\n * @param apiName Name of API this calling this function. This is mostly for logging.\n * @param toggleStyleCallback The callback to format the segment. It will be called with current selected table. If no table is selected, it will not be called.\n * @param segmentHasStyleCallback The callback used for checking if the given segment already has required format\n * @param includingFormatHolder True to also include format holder of list item when search selected segments\n * @param afterFormatCallback A callback to invoke after format is applied to all selected segments and before the change is applied to DOM tree\n */\nexport function formatSegmentWithContentModel(\n editor: IEditor,\n apiName: string,\n toggleStyleCallback: (\n format: ContentModelSegmentFormat,\n isTuringOn: boolean,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => void,\n segmentHasStyleCallback?: (\n format: ContentModelSegmentFormat,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => boolean,\n includingFormatHolder?: boolean,\n afterFormatCallback?: (model: ReadonlyContentModelDocument) => void\n) {\n editor.formatContentModel(\n (model, context) => {\n let segmentAndParagraphs = getSelectedSegmentsAndParagraphs(\n model,\n !!includingFormatHolder,\n false /*includingEntity*/,\n true /*mutate*/\n );\n let isCollapsedSelection =\n segmentAndParagraphs.length >= 1 &&\n segmentAndParagraphs.every(x => x[0].segmentType == 'SelectionMarker');\n\n if (isCollapsedSelection) {\n const para = segmentAndParagraphs[0][1];\n const path = segmentAndParagraphs[0][2];\n\n segmentAndParagraphs = adjustWordSelection(\n model,\n segmentAndParagraphs[0][0]\n ).map(x => [x, para, path]);\n\n if (segmentAndParagraphs.length > 1) {\n isCollapsedSelection = false;\n }\n }\n\n const formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][] = segmentAndParagraphs.map(item => [item[0].format, item[0], item[1]]);\n\n const isTurningOff = segmentHasStyleCallback\n ? formatsAndSegments.every(([format, segment, paragraph]) =>\n segmentHasStyleCallback(format, segment, paragraph)\n )\n : false;\n\n formatsAndSegments.forEach(([format, segment, paragraph]) => {\n toggleStyleCallback(format, !isTurningOff, segment, paragraph);\n });\n\n afterFormatCallback?.(model);\n\n formatsAndSegments.forEach(([_, __, paragraph]) => {\n if (paragraph) {\n mergeTextSegments(paragraph);\n }\n });\n\n if (isCollapsedSelection) {\n context.newPendingFormat = segmentAndParagraphs[0][0].format;\n editor.focus();\n return false;\n } else {\n return formatsAndSegments.length > 0;\n }\n },\n {\n apiName,\n }\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"formatSegmentWithContentModel.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/utils/formatSegmentWithContentModel.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAC;AACnF,OAAO,EACH,iBAAiB,EACjB,uBAAuB,EACvB,uBAAuB,EACvB,iBAAiB,EACjB,gCAAgC,EAChC,iBAAiB,GACpB,MAAM,6BAA6B,CAAC;AAcrC;;;;;;;;GAQG;AACH,MAAM,UAAU,6BAA6B,CACzC,MAAe,EACf,OAAe,EACf,mBAKS,EACT,uBAIY,EACZ,qBAA+B,EAC/B,mBAAmE;IAEnE,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,IAAI,oBAAoB,GAAG,gCAAgC,CACvD,KAAK,EACL,CAAC,CAAC,qBAAqB,EACvB,IAAI,CAAC,mBAAmB,EACxB,IAAI,CAAC,UAAU,CAClB,CAAC;QACF,IAAI,oBAAoB,GACpB,oBAAoB,CAAC,MAAM,IAAI,CAAC;YAChC,oBAAoB,CAAC,KAAK,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,IAAI,iBAAiB,EAArC,CAAqC,CAAC,CAAC;QAE3E,0DAA0D;QAC1D,IAAI,oBAAoB,EAAE;YACtB,IAAM,MAAI,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACxC,IAAM,MAAI,GAAG,oBAAoB,CAAC,CAAC,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C,CAAC;gBACvD,IAAM,UAAU,GAAG,gCAAgC,CAC/C,KAAK,EACL,KAAK,CAAC,yBAAyB,EAC/B,KAAK,CAAC,mBAAmB,EACzB,IAAI,CAAC,UAAU,CAClB,CAAC;gBAEF,UAAU,CAAC,OAAO,CAAC,UAAA,IAAI;oBACnB,kBAAkB,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBAChE,CAAC,CAAC,CAAC;gBAEH,kBAAkB,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;aAClD;QACL,CAAC,CAAC,CAAC;KACN;AACL,CAAC;AAED,SAAS,iBAAiB,CACtB,MAAe,EACf,kBAAiF;IAEjF,kBAAkB,CAAC,OAAO,CAAC,UAAC,EAAqB;YAArB,KAAA,aAAqB,EAApB,MAAM,QAAA,EAAE,IAAI,QAAA,EAAE,KAAK,QAAA;QAC5C,IAAM,aAAa,GAAG,4BAA4B,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACnE,IAAM,iBAAiB,GAAG,uBAAuB,CAAC,aAAa,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;QAEzF,iBAAiB,CAAC,MAAM,CAAC,WAAW,EAAE,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC;IACpF,CAAC,CAAC,CAAC;AACP,CAAC","sourcesContent":["import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport {\n contentModelToDom,\n createDomToModelContext,\n createModelToDomContext,\n domToContentModel,\n getSelectedSegmentsAndParagraphs,\n mergeTextSegments,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelDocument,\n ContentModelEntity,\n ContentModelSegmentFormat,\n EditorContext,\n FormattableRoot,\n IEditor,\n PluginEventData,\n ReadonlyContentModelDocument,\n ShallowMutableContentModelParagraph,\n ShallowMutableContentModelSegment,\n} from 'roosterjs-content-model-types';\n\n/**\n * Invoke a callback to format the selected segment using Content Model\n * @param editor The editor object\n * @param apiName Name of API this calling this function. This is mostly for logging.\n * @param toggleStyleCallback The callback to format the segment. It will be called with current selected table. If no table is selected, it will not be called.\n * @param segmentHasStyleCallback The callback used for checking if the given segment already has required format\n * @param includingFormatHolder True to also include format holder of list item when search selected segments\n * @param afterFormatCallback A callback to invoke after format is applied to all selected segments and before the change is applied to DOM tree\n */\nexport function formatSegmentWithContentModel(\n editor: IEditor,\n apiName: string,\n toggleStyleCallback: (\n format: ContentModelSegmentFormat,\n isTuringOn: boolean,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => void,\n segmentHasStyleCallback?: (\n format: ContentModelSegmentFormat,\n segment: ShallowMutableContentModelSegment | null,\n paragraph: ShallowMutableContentModelParagraph | null\n ) => boolean,\n includingFormatHolder?: boolean,\n afterFormatCallback?: (model: ReadonlyContentModelDocument) => void\n) {\n editor.formatContentModel(\n (model, context) => {\n let segmentAndParagraphs = getSelectedSegmentsAndParagraphs(\n model,\n !!includingFormatHolder,\n true /*includingEntity*/,\n true /*mutate*/\n );\n let isCollapsedSelection =\n segmentAndParagraphs.length >= 1 &&\n segmentAndParagraphs.every(x => x[0].segmentType == 'SelectionMarker');\n\n // 1. adjust selection to a word if selection is collapsed\n if (isCollapsedSelection) {\n const para = segmentAndParagraphs[0][1];\n const path = segmentAndParagraphs[0][2];\n\n segmentAndParagraphs = adjustWordSelection(\n model,\n segmentAndParagraphs[0][0]\n ).map(x => [x, para, path]);\n\n if (segmentAndParagraphs.length > 1) {\n isCollapsedSelection = false;\n }\n }\n\n // 2. expand selection for entities if any\n const formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][] = [];\n const modelsFromEntities: [\n ContentModelEntity,\n FormattableRoot,\n ContentModelDocument\n ][] = [];\n\n segmentAndParagraphs.forEach(item => {\n if (item[0].segmentType == 'Entity') {\n expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities);\n } else {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n }\n });\n\n // 3. check if we should turn format on (when not all selection has the required format already)\n // or off (all selections already have the required format)\n const isTurningOff = segmentHasStyleCallback\n ? formatsAndSegments.every(([format, segment, paragraph]) =>\n segmentHasStyleCallback(format, segment, paragraph)\n )\n : false;\n\n // 4. invoke the callback function to apply the format\n formatsAndSegments.forEach(([format, segment, paragraph]) => {\n toggleStyleCallback(format, !isTurningOff, segment, paragraph);\n });\n\n // 5. after format is applied to all selections, invoke another callback to do some clean up before write the change back\n afterFormatCallback?.(model);\n\n // 6. finally merge segments if possible, to avoid fragmentation\n formatsAndSegments.forEach(([_, __, paragraph]) => {\n if (paragraph) {\n mergeTextSegments(paragraph);\n }\n });\n\n // 7. Write back models that we got from entities (if any)\n writeBackEntities(editor, modelsFromEntities);\n\n // 8. if the selection is still collapsed, it means we didn't actually applied format, set a pending format so it can be applied when user type\n // otherwise, write back to editor\n if (isCollapsedSelection) {\n context.newPendingFormat = segmentAndParagraphs[0][0].format;\n editor.focus();\n return false;\n } else {\n return formatsAndSegments.length > 0;\n }\n },\n {\n apiName,\n }\n );\n}\n\nfunction createEditorContextForEntity(editor: IEditor, entity: ContentModelEntity): EditorContext {\n const domHelper = editor.getDOMHelper();\n const context: EditorContext = {\n isDarkMode: editor.isDarkMode(),\n defaultFormat: { ...entity.format },\n darkColorHandler: editor.getColorManager(),\n addDelimiterForEntity: false,\n allowCacheElement: false,\n domIndexer: undefined,\n zoomScale: domHelper.calculateZoomScale(),\n experimentalFeatures: [],\n };\n\n if (editor.getDocument().defaultView?.getComputedStyle(entity.wrapper).direction == 'rtl') {\n context.isRootRtl = true;\n }\n\n return context;\n}\n\nfunction expandEntitySelections(\n editor: IEditor,\n entity: ContentModelEntity,\n formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][],\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n const { id, entityType: type, isReadonly } = entity.entityFormat;\n\n if (id && type) {\n const formattableRoots: FormattableRoot[] = [];\n const entityOperationEventData: PluginEventData<'entityOperation'> = {\n entity: { id, type, isReadonly: !!isReadonly, wrapper: entity.wrapper },\n operation: 'beforeFormat',\n formattableRoots,\n };\n\n editor.triggerEvent('entityOperation', entityOperationEventData);\n\n formattableRoots.forEach(root => {\n if (entity.wrapper.contains(root.element)) {\n const editorContext = createEditorContextForEntity(editor, entity);\n const context = createDomToModelContext(editorContext, root.domToModelOptions);\n\n // Treat everything as selected since the parent entity is selected\n context.isInSelection = true;\n\n const model = domToContentModel(root.element, context);\n const selections = getSelectedSegmentsAndParagraphs(\n model,\n false /*includingFormatHolder*/,\n false /*includingEntity*/,\n true /*mutate*/\n );\n\n selections.forEach(item => {\n formatsAndSegments.push([item[0].format, item[0], item[1]]);\n });\n\n modelsFromEntities.push([entity, root, model]);\n }\n });\n }\n}\n\nfunction writeBackEntities(\n editor: IEditor,\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][]\n) {\n modelsFromEntities.forEach(([entity, root, model]) => {\n const editorContext = createEditorContextForEntity(editor, entity);\n const modelToDomContext = createModelToDomContext(editorContext, root.modelToDomOptions);\n\n contentModelToDom(editor.getDocument(), root.element, model, modelToDomContext);\n });\n}\n"]}
|
package/package.json
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
"description": "Format API for roosterjs",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"tslib": "^2.3.1",
|
|
6
|
-
"roosterjs-content-model-dom": "^9.
|
|
7
|
-
"roosterjs-content-model-types": "^9.
|
|
6
|
+
"roosterjs-content-model-dom": "^9.16.0",
|
|
7
|
+
"roosterjs-content-model-types": "^9.16.0"
|
|
8
8
|
},
|
|
9
|
-
"version": "9.
|
|
9
|
+
"version": "9.16.0",
|
|
10
10
|
"main": "./lib/index.js",
|
|
11
11
|
"typings": "./lib/index.d.ts",
|
|
12
12
|
"module": "./lib-mjs/index.js",
|