roosterjs-content-model-api 9.42.0 → 9.44.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.
Files changed (49) hide show
  1. package/lib/index.d.ts +1 -1
  2. package/lib/index.js +2 -1
  3. package/lib/index.js.map +1 -1
  4. package/lib/modelApi/block/setModelIndentation.js +7 -1
  5. package/lib/modelApi/block/setModelIndentation.js.map +1 -1
  6. package/lib/modelApi/link/getLinkUrl.js +4 -1
  7. package/lib/modelApi/link/getLinkUrl.js.map +1 -1
  8. package/lib/modelApi/link/promoteLink.d.ts +8 -1
  9. package/lib/modelApi/link/promoteLink.js +24 -7
  10. package/lib/modelApi/link/promoteLink.js.map +1 -1
  11. package/lib/modelApi/list/setListType.js +3 -2
  12. package/lib/modelApi/list/setListType.js.map +1 -1
  13. package/lib/publicApi/block/setIndentation.js +0 -4
  14. package/lib/publicApi/block/setIndentation.js.map +1 -1
  15. package/lib/publicApi/utils/formatSegmentWithContentModel.js +5 -2
  16. package/lib/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
  17. package/lib-amd/index.d.ts +1 -1
  18. package/lib-amd/index.js +2 -1
  19. package/lib-amd/index.js.map +1 -1
  20. package/lib-amd/modelApi/block/setModelIndentation.js +7 -1
  21. package/lib-amd/modelApi/block/setModelIndentation.js.map +1 -1
  22. package/lib-amd/modelApi/link/getLinkUrl.js +4 -1
  23. package/lib-amd/modelApi/link/getLinkUrl.js.map +1 -1
  24. package/lib-amd/modelApi/link/promoteLink.d.ts +8 -1
  25. package/lib-amd/modelApi/link/promoteLink.js +24 -7
  26. package/lib-amd/modelApi/link/promoteLink.js.map +1 -1
  27. package/lib-amd/modelApi/list/setListType.js +3 -2
  28. package/lib-amd/modelApi/list/setListType.js.map +1 -1
  29. package/lib-amd/publicApi/block/setIndentation.js +1 -4
  30. package/lib-amd/publicApi/block/setIndentation.js.map +1 -1
  31. package/lib-amd/publicApi/utils/formatSegmentWithContentModel.js +5 -2
  32. package/lib-amd/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
  33. package/lib-mjs/index.d.ts +1 -1
  34. package/lib-mjs/index.js +1 -1
  35. package/lib-mjs/index.js.map +1 -1
  36. package/lib-mjs/modelApi/block/setModelIndentation.js +8 -2
  37. package/lib-mjs/modelApi/block/setModelIndentation.js.map +1 -1
  38. package/lib-mjs/modelApi/link/getLinkUrl.js +4 -1
  39. package/lib-mjs/modelApi/link/getLinkUrl.js.map +1 -1
  40. package/lib-mjs/modelApi/link/promoteLink.d.ts +8 -1
  41. package/lib-mjs/modelApi/link/promoteLink.js +22 -6
  42. package/lib-mjs/modelApi/link/promoteLink.js.map +1 -1
  43. package/lib-mjs/modelApi/list/setListType.js +3 -2
  44. package/lib-mjs/modelApi/list/setListType.js.map +1 -1
  45. package/lib-mjs/publicApi/block/setIndentation.js +0 -4
  46. package/lib-mjs/publicApi/block/setIndentation.js.map +1 -1
  47. package/lib-mjs/publicApi/utils/formatSegmentWithContentModel.js +5 -2
  48. package/lib-mjs/publicApi/utils/formatSegmentWithContentModel.js.map +1 -1
  49. package/package.json +3 -3
@@ -15,14 +15,12 @@ export function promoteLink(segment, paragraph, autoLinkOptions) {
15
15
  if (segment.link) {
16
16
  return null;
17
17
  }
18
- var link = segment.text.split(' ').pop();
19
- var url = link === null || link === void 0 ? void 0 : link.trim();
20
- var linkUrl = undefined;
21
- if (url && link && (linkUrl = getLinkUrl(url, autoLinkOptions))) {
22
- var linkSegment = splitTextSegment(segment, paragraph, segment.text.length - link.trimLeft().length, segment.text.trimRight().length);
18
+ var promotedLink = getPromoteLink(segment, autoLinkOptions);
19
+ if (promotedLink) {
20
+ var linkSegment = splitTextSegment(segment, paragraph, segment.text.length - promotedLink.label.trimLeft().length, segment.text.trimRight().length);
23
21
  linkSegment.link = {
24
22
  format: {
25
- href: linkUrl,
23
+ href: promotedLink.href,
26
24
  underline: true,
27
25
  },
28
26
  dataset: {},
@@ -31,4 +29,22 @@ export function promoteLink(segment, paragraph, autoLinkOptions) {
31
29
  }
32
30
  return null;
33
31
  }
32
+ /**
33
+ * Verify if the link can be promoted
34
+ * @param segment The text segment to search link text from
35
+ * @param options Options of auto link
36
+ * @returns if a link can be promoted
37
+ */
38
+ export function getPromoteLink(segment, autoLinkOptions) {
39
+ var link = segment.text.split(' ').pop();
40
+ var url = link === null || link === void 0 ? void 0 : link.trim();
41
+ var linkUrl = undefined;
42
+ if (url && link && (linkUrl = getLinkUrl(url, autoLinkOptions))) {
43
+ return {
44
+ label: link,
45
+ href: linkUrl,
46
+ };
47
+ }
48
+ return undefined;
49
+ }
34
50
  //# sourceMappingURL=promoteLink.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"promoteLink.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/link/promoteLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAO5E;;;;;;;;;;GAUG;AACH,MAAM,UAAU,WAAW,CACvB,OAAyB,EACzB,SAA8C,EAC9C,eAAgC;IAEhC,IAAI,OAAO,CAAC,IAAI,EAAE;QACd,OAAO,IAAI,CAAC;KACf;IACD,IAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;IAC3C,IAAM,GAAG,GAAG,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,IAAI,EAAE,CAAC;IACzB,IAAI,OAAO,GAAuB,SAAS,CAAC;IAE5C,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC,EAAE;QAC7D,IAAM,WAAW,GAAG,gBAAgB,CAChC,OAAO,EACP,SAAS,EACT,OAAO,CAAC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC,MAAM,EAC5C,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,MAAM,CAClC,CAAC;QACF,WAAW,CAAC,IAAI,GAAG;YACf,MAAM,EAAE;gBACJ,IAAI,EAAE,OAAO;gBACb,SAAS,EAAE,IAAI;aAClB;YACD,OAAO,EAAE,EAAE;SACd,CAAC;QAEF,OAAO,WAAW,CAAC;KACtB;IAED,OAAO,IAAI,CAAC;AAChB,CAAC","sourcesContent":["import { getLinkUrl } from './getLinkUrl';\nimport { splitTextSegment } from '../../publicApi/segment/splitTextSegment';\nimport type {\n AutoLinkOptions,\n ContentModelText,\n ShallowMutableContentModelParagraph,\n} from 'roosterjs-content-model-types';\n\n/**\n * Promote the given text segment to a hyper link when the segment text is ending with a valid link format.\n * When the whole text segment if of a link, promote the whole segment.\n * When the text segment ends with a link format, split the segment and promote the second part\n * When link is in middle of the text segment, no action.\n * This is mainly used for doing auto link when there is a link before cursor\n * @param segment The text segment to search link text from\n * @param paragraph Parent paragraph of the segment\n * @param options Options of auto link\n * @returns If a link is promoted, return this segment. Otherwise return null\n */\nexport function promoteLink(\n segment: ContentModelText,\n paragraph: ShallowMutableContentModelParagraph,\n autoLinkOptions: AutoLinkOptions\n): ContentModelText | null {\n if (segment.link) {\n return null;\n }\n const link = segment.text.split(' ').pop();\n const url = link?.trim();\n let linkUrl: string | undefined = undefined;\n\n if (url && link && (linkUrl = getLinkUrl(url, autoLinkOptions))) {\n const linkSegment = splitTextSegment(\n segment,\n paragraph,\n segment.text.length - link.trimLeft().length,\n segment.text.trimRight().length\n );\n linkSegment.link = {\n format: {\n href: linkUrl,\n underline: true,\n },\n dataset: {},\n };\n\n return linkSegment;\n }\n\n return null;\n}\n"]}
1
+ {"version":3,"file":"promoteLink.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/link/promoteLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,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Segment } from '../../publicApi/segment/splitTextSegment';\nimport type {\n AutoLinkOptions,\n ContentModelText,\n PromotedLink,\n ShallowMutableContentModelParagraph,\n} from 'roosterjs-content-model-types';\n\n/**\n * Promote the given text segment to a hyper link when the segment text is ending with a valid link format.\n * When the whole text segment if of a link, promote the whole segment.\n * When the text segment ends with a link format, split the segment and promote the second part\n * When link is in middle of the text segment, no action.\n * This is mainly used for doing auto link when there is a link before cursor\n * @param segment The text segment to search link text from\n * @param paragraph Parent paragraph of the segment\n * @param options Options of auto link\n * @returns If a link is promoted, return this segment. Otherwise return null\n */\nexport function promoteLink(\n segment: ContentModelText,\n paragraph: ShallowMutableContentModelParagraph,\n autoLinkOptions: AutoLinkOptions\n): ContentModelText | null {\n if (segment.link) {\n return null;\n }\n\n const promotedLink = getPromoteLink(segment, autoLinkOptions);\n\n if (promotedLink) {\n const linkSegment = splitTextSegment(\n segment,\n paragraph,\n segment.text.length - promotedLink.label.trimLeft().length,\n segment.text.trimRight().length\n );\n linkSegment.link = {\n format: {\n href: promotedLink.href,\n underline: true,\n },\n dataset: {},\n };\n\n return linkSegment;\n }\n\n return null;\n}\n\n/**\n * Verify if the link can be promoted\n * @param segment The text segment to search link text from\n * @param options Options of auto link\n * @returns if a link can be promoted\n */\nexport function getPromoteLink(\n segment: ContentModelText,\n autoLinkOptions: AutoLinkOptions\n): PromotedLink | undefined {\n const link = segment.text.split(' ').pop();\n const url = link?.trim();\n let linkUrl: string | undefined = undefined;\n if (url && link && (linkUrl = getLinkUrl(url, autoLinkOptions))) {\n return {\n label: link,\n href: linkUrl,\n };\n }\n return undefined;\n}\n"]}
@@ -24,7 +24,8 @@ export function setListType(model, listType, removeMargins) {
24
24
  var _b, _c;
25
25
  var block = _a.block, parent = _a.parent;
26
26
  if (isBlockGroupOfType(block, 'ListItem')) {
27
- var level = block.levels.pop();
27
+ var mutableBlock = mutateBlock(block);
28
+ var level = mutableBlock.levels.pop();
28
29
  if (!alreadyInExpectedType && level) {
29
30
  level.listType = listType;
30
31
  updateListMetadata(level, function (metadata) {
@@ -32,7 +33,7 @@ export function setListType(model, listType, removeMargins) {
32
33
  applyListStyleFromLevel: true,
33
34
  });
34
35
  });
35
- block.levels.push(level);
36
+ mutableBlock.levels.push(level);
36
37
  }
37
38
  else if (block.blocks.length == 1) {
38
39
  setParagraphNotImplicit(block.blocks[0]);
@@ -1 +1 @@
1
- {"version":3,"file":"setListType.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/list/setListType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,EAAE,0BAA0B,EAAE,MAAM,qCAAqC,CAAC;AACjF,OAAO,EACH,UAAU,EACV,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,kBAAkB,EAClB,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,WAAW,EACX,qBAAqB,EACrB,uBAAuB,EACvB,kBAAkB,GACrB,MAAM,6BAA6B,CAAC;AAUrC;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CACvB,KAAmC,EACnC,QAAqB,EACrB,aAA8B;IAA9B,8BAAA,EAAA,qBAA8B;IAE9B,0BAA0B,CAAC,KAAK,CAAC,CAAC;IAElC,IAAM,oBAAoB,GAAG,oBAAoB,CAC7C,KAAK,EACL,CAAC,UAAU,CAAC,EACZ,EAAE,CAAC,8IAA8I;KACpJ,CAAC;IACF,IAAM,qBAAqB,GAAG,oBAAoB,CAAC,KAAK,CAAC,UAAC,EAAS;;YAAP,KAAK,WAAA;QAC7D,OAAA,kBAAkB,CAAuB,KAAK,EAAE,UAAU,CAAC;YACvD,CAAC,CAAC,CAAA,MAAA,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,0CAAE,QAAQ,KAAI,QAAQ;YAC7D,CAAC,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAA;KAAA,CACjC,CAAC;IACF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KAAK,CACvB,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,WAAW,IAAI,IAAI,IAAI,CAAC,CAAC,WAAW,IAAI,iBAAiB,EAA3D,CAA2D,CACnE,CAAC;QACN;YACI,OAAO,IAAI,CAAC;KACnB;AACL,CAAC","sourcesContent":["import { adjustListIndentation } from '../common/adjustIndentation';\nimport { splitSelectedParagraphByBr } from '../block/splitSelectedParagraphByBr';\nimport {\n copyFormat,\n createListItem,\n createListLevel,\n getOperationalBlocks,\n isBlockGroupOfType,\n ListFormats,\n ListFormatsToKeep,\n ListFormatsToMove,\n mutateBlock,\n normalizeContentModel,\n setParagraphNotImplicit,\n updateListMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelBlockFormat,\n ContentModelListItem,\n ReadonlyContentModelBlock,\n ReadonlyContentModelDocument,\n ReadonlyContentModelListItem,\n ShallowMutableContentModelListItem,\n} from 'roosterjs-content-model-types';\n\n/**\n * Set a list type to content model\n * @param model the model document\n * @param listType the list type OL | UL\n * @param removeMargins true to remove margins, false to keep margins @default false\n */\nexport function setListType(\n model: ReadonlyContentModelDocument,\n listType: 'OL' | 'UL',\n removeMargins: boolean = false\n) {\n splitSelectedParagraphByBr(model);\n\n const paragraphOrListItems = getOperationalBlocks<ContentModelListItem>(\n model,\n ['ListItem'],\n [] // Set stop types to be empty so we can find list items even cross the boundary of table, then we can always operation on the list item if any\n );\n const alreadyInExpectedType = paragraphOrListItems.every(({ block }) =>\n isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')\n ? block.levels[block.levels.length - 1]?.listType == listType\n : shouldIgnoreBlock(block)\n );\n let existingListItems: ReadonlyContentModelListItem[] = [];\n\n paragraphOrListItems.forEach(({ block, parent }, itemIndex) => {\n if (isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')) {\n const level = block.levels.pop();\n\n if (!alreadyInExpectedType && level) {\n level.listType = listType;\n\n updateListMetadata(level, metadata =>\n Object.assign({}, metadata, {\n applyListStyleFromLevel: true,\n })\n );\n\n block.levels.push(level);\n } else if (block.blocks.length == 1) {\n setParagraphNotImplicit(block.blocks[0]);\n }\n\n if (alreadyInExpectedType) {\n // if the list item has margins or textAlign, we need to apply them to the block to preserve the indention and alignment\n block.blocks.forEach(x => {\n copyFormat<ContentModelBlockFormat>(x.format, block.format, ListFormats);\n });\n }\n } else {\n const index = parent.blocks.indexOf(block);\n\n if (index >= 0) {\n if (paragraphOrListItems.length == 1 || !shouldIgnoreBlock(block)) {\n const prevBlock = parent.blocks[index - 1];\n const segmentFormat =\n (block.blockType == 'Paragraph' && block.segments[0]?.format) || {};\n const newListItem: ShallowMutableContentModelListItem = createListItem(\n [\n createListLevel(listType, {\n startNumberOverride:\n itemIndex > 0 ||\n (prevBlock?.blockType == 'BlockGroup' &&\n prevBlock.blockGroupType == 'ListItem' &&\n prevBlock.levels[0]?.listType == 'OL')\n ? undefined\n : 1,\n direction: block.format.direction,\n textAlign: block.format.textAlign,\n marginBottom: removeMargins ? '0px' : undefined,\n marginTop: removeMargins ? '0px' : undefined,\n }),\n ],\n // For list bullet, we only want to carry over these formats from segments:\n {\n fontFamily: segmentFormat.fontFamily,\n fontSize: segmentFormat.fontSize,\n textColor: segmentFormat.textColor,\n }\n );\n\n if (block.blockType == 'Paragraph') {\n setParagraphNotImplicit(block);\n }\n\n const mutableBlock = mutateBlock(block);\n\n newListItem.blocks.push(mutableBlock);\n\n adjustListIndentation(newListItem);\n\n copyFormat<ContentModelBlockFormat>(\n newListItem.format,\n mutableBlock.format,\n ListFormatsToMove,\n true /*deleteOriginalFormat*/\n );\n copyFormat<ContentModelBlockFormat>(\n newListItem.format,\n mutableBlock.format,\n ListFormatsToKeep\n );\n\n mutateBlock(parent).blocks.splice(index, 1, newListItem);\n existingListItems.push(newListItem);\n\n const levelIndex = newListItem.levels.length - 1;\n const level = mutateBlock(newListItem).levels[levelIndex];\n\n if (level) {\n updateListMetadata(level, metadata =>\n Object.assign({}, metadata, {\n applyListStyleFromLevel: true,\n })\n );\n }\n } else {\n existingListItems.forEach(\n x => (mutateBlock(x).levels[0].format.marginBottom = '0px')\n );\n existingListItems = [];\n }\n }\n }\n });\n\n normalizeContentModel(model);\n\n return paragraphOrListItems.length > 0;\n}\n\nfunction shouldIgnoreBlock(block: ReadonlyContentModelBlock) {\n switch (block.blockType) {\n case 'Table':\n return false;\n case 'Paragraph':\n return block.segments.every(\n x => x.segmentType == 'Br' || x.segmentType == 'SelectionMarker'\n );\n default:\n return true;\n }\n}\n"]}
1
+ {"version":3,"file":"setListType.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/list/setListType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,EAAE,0BAA0B,EAAE,MAAM,qCAAqC,CAAC;AACjF,OAAO,EACH,UAAU,EACV,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,kBAAkB,EAClB,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,WAAW,EACX,qBAAqB,EACrB,uBAAuB,EACvB,kBAAkB,GACrB,MAAM,6BAA6B,CAAC;AASrC;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CACvB,KAAmC,EACnC,QAAqB,EACrB,aAA8B;IAA9B,8BAAA,EAAA,qBAA8B;IAE9B,0BAA0B,CAAC,KAAK,CAAC,CAAC;IAElC,IAAM,oBAAoB,GAAG,oBAAoB,CAC7C,KAAK,EACL,CAAC,UAAU,CAAC,EACZ,EAAE,CAAC,8IAA8I;KACpJ,CAAC;IACF,IAAM,qBAAqB,GAAG,oBAAoB,CAAC,KAAK,CAAC,UAAC,EAAS;;YAAP,KAAK,WAAA;QAC7D,OAAA,kBAAkB,CAA+B,KAAK,EAAE,UAAU,CAAC;YAC/D,CAAC,CAAC,CAAA,MAAA,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,0CAAE,QAAQ,KAAI,QAAQ;YAC7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AAK,CAAC,SAAS,EAAE;QACrB,KAAK,OAAO;YACR,OAAO,KAAK,CAAC;QACjB,KAAK,WAAW;YACZ,OAAO,KAAK,CAAC,QAAQ,CAAC,KAAK,CACvB,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,WAAW,IAAI,IAAI,IAAI,CAAC,CAAC,WAAW,IAAI,iBAAiB,EAA3D,CAA2D,CACnE,CAAC;QACN;YACI,OAAO,IAAI,CAAC;KACnB;AACL,CAAC","sourcesContent":["import { adjustListIndentation } from '../common/adjustIndentation';\nimport { splitSelectedParagraphByBr } from '../block/splitSelectedParagraphByBr';\nimport {\n copyFormat,\n createListItem,\n createListLevel,\n getOperationalBlocks,\n isBlockGroupOfType,\n ListFormats,\n ListFormatsToKeep,\n ListFormatsToMove,\n mutateBlock,\n normalizeContentModel,\n setParagraphNotImplicit,\n updateListMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelBlockFormat,\n ReadonlyContentModelBlock,\n ReadonlyContentModelDocument,\n ReadonlyContentModelListItem,\n ShallowMutableContentModelListItem,\n} from 'roosterjs-content-model-types';\n\n/**\n * Set a list type to content model\n * @param model the model document\n * @param listType the list type OL | UL\n * @param removeMargins true to remove margins, false to keep margins @default false\n */\nexport function setListType(\n model: ReadonlyContentModelDocument,\n listType: 'OL' | 'UL',\n removeMargins: boolean = false\n) {\n splitSelectedParagraphByBr(model);\n\n const paragraphOrListItems = getOperationalBlocks<ReadonlyContentModelListItem>(\n model,\n ['ListItem'],\n [] // Set stop types to be empty so we can find list items even cross the boundary of table, then we can always operation on the list item if any\n );\n const alreadyInExpectedType = paragraphOrListItems.every(({ block }) =>\n isBlockGroupOfType<ReadonlyContentModelListItem>(block, 'ListItem')\n ? block.levels[block.levels.length - 1]?.listType == listType\n : shouldIgnoreBlock(block)\n );\n let existingListItems: ReadonlyContentModelListItem[] = [];\n\n paragraphOrListItems.forEach(({ block, parent }, itemIndex) => {\n if (isBlockGroupOfType<ReadonlyContentModelListItem>(block, 'ListItem')) {\n const mutableBlock = mutateBlock(block);\n const level = mutableBlock.levels.pop();\n\n if (!alreadyInExpectedType && level) {\n level.listType = listType;\n\n updateListMetadata(level, metadata =>\n Object.assign({}, metadata, {\n applyListStyleFromLevel: true,\n })\n );\n\n mutableBlock.levels.push(level);\n } else if (block.blocks.length == 1) {\n setParagraphNotImplicit(block.blocks[0]);\n }\n\n if (alreadyInExpectedType) {\n // if the list item has margins or textAlign, we need to apply them to the block to preserve the indention and alignment\n block.blocks.forEach(x => {\n copyFormat<ContentModelBlockFormat>(x.format, block.format, ListFormats);\n });\n }\n } else {\n const index = parent.blocks.indexOf(block);\n\n if (index >= 0) {\n if (paragraphOrListItems.length == 1 || !shouldIgnoreBlock(block)) {\n const prevBlock = parent.blocks[index - 1];\n const segmentFormat =\n (block.blockType == 'Paragraph' && block.segments[0]?.format) || {};\n const newListItem: ShallowMutableContentModelListItem = createListItem(\n [\n createListLevel(listType, {\n startNumberOverride:\n itemIndex > 0 ||\n (prevBlock?.blockType == 'BlockGroup' &&\n prevBlock.blockGroupType == 'ListItem' &&\n prevBlock.levels[0]?.listType == 'OL')\n ? undefined\n : 1,\n direction: block.format.direction,\n textAlign: block.format.textAlign,\n marginBottom: removeMargins ? '0px' : undefined,\n marginTop: removeMargins ? '0px' : undefined,\n }),\n ],\n // For list bullet, we only want to carry over these formats from segments:\n {\n fontFamily: segmentFormat.fontFamily,\n fontSize: segmentFormat.fontSize,\n textColor: segmentFormat.textColor,\n }\n );\n\n if (block.blockType == 'Paragraph') {\n setParagraphNotImplicit(block);\n }\n\n const mutableBlock = mutateBlock(block);\n\n newListItem.blocks.push(mutableBlock);\n\n adjustListIndentation(newListItem);\n\n copyFormat<ContentModelBlockFormat>(\n newListItem.format,\n mutableBlock.format,\n ListFormatsToMove,\n true /*deleteOriginalFormat*/\n );\n copyFormat<ContentModelBlockFormat>(\n newListItem.format,\n mutableBlock.format,\n ListFormatsToKeep\n );\n\n mutateBlock(parent).blocks.splice(index, 1, newListItem);\n existingListItems.push(newListItem);\n\n const levelIndex = newListItem.levels.length - 1;\n const level = mutateBlock(newListItem).levels[levelIndex];\n\n if (level) {\n updateListMetadata(level, metadata =>\n Object.assign({}, metadata, {\n applyListStyleFromLevel: true,\n })\n );\n }\n } else {\n existingListItems.forEach(\n x => (mutateBlock(x).levels[0].format.marginBottom = '0px')\n );\n existingListItems = [];\n }\n }\n }\n });\n\n normalizeContentModel(model);\n\n return paragraphOrListItems.length > 0;\n}\n\nfunction shouldIgnoreBlock(block: ReadonlyContentModelBlock) {\n switch (block.blockType) {\n case 'Table':\n return false;\n case 'Paragraph':\n return block.segments.every(\n x => x.segmentType == 'Br' || x.segmentType == 'SelectionMarker'\n );\n default:\n return true;\n }\n}\n"]}
@@ -1,4 +1,3 @@
1
- import { normalizeContentModel } from 'roosterjs-content-model-dom';
2
1
  import { setModelIndentation } from '../../modelApi/block/setModelIndentation';
3
2
  /**
4
3
  * Indent or outdent to selected paragraphs
@@ -10,9 +9,6 @@ export function setIndentation(editor, indentation, length) {
10
9
  editor.focus();
11
10
  editor.formatContentModel(function (model, context) {
12
11
  var result = setModelIndentation(model, indentation, length);
13
- if (result) {
14
- normalizeContentModel(model);
15
- }
16
12
  context.newPendingFormat = 'preserve';
17
13
  return result;
18
14
  }, {
@@ -1 +1 @@
1
- {"version":3,"file":"setIndentation.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setIndentation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAG/E;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAC1B,MAAe,EACf,WAAiC,EACjC,MAAe;IAEf,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,IAAM,MAAM,GAAG,mBAAmB,CAAC,KAAK,EAAE,WAAW,EAAE,MAAM,CAAC,CAAC;QAE/D,IAAI,MAAM,EAAE;YACR,qBAAqB,CAAC,KAAK,CAAC,CAAC;SAChC;QAED,OAAO,CAAC,gBAAgB,GAAG,UAAU,CAAC;QAEtC,OAAO,MAAM,CAAC;IAClB,CAAC,EACD;QACI,OAAO,EAAE,gBAAgB;KAC5B,CACJ,CAAC;AACN,CAAC","sourcesContent":["import { normalizeContentModel } from 'roosterjs-content-model-dom';\nimport { setModelIndentation } from '../../modelApi/block/setModelIndentation';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Indent or outdent to selected paragraphs\n * @param editor The editor to operate on\n * @param indentation Whether indent or outdent\n * @param length The length of pixel to indent/outdent @default 40\n */\nexport function setIndentation(\n editor: IEditor,\n indentation: 'indent' | 'outdent',\n length?: number\n) {\n editor.focus();\n\n editor.formatContentModel(\n (model, context) => {\n const result = setModelIndentation(model, indentation, length);\n\n if (result) {\n normalizeContentModel(model);\n }\n\n context.newPendingFormat = 'preserve';\n\n return result;\n },\n {\n apiName: 'setIndentation',\n }\n );\n}\n"]}
1
+ {"version":3,"file":"setIndentation.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setIndentation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAG/E;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAC1B,MAAe,EACf,WAAiC,EACjC,MAAe;IAEf,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;QACX,IAAM,MAAM,GAAG,mBAAmB,CAAC,KAAK,EAAE,WAAW,EAAE,MAAM,CAAC,CAAC;QAE/D,OAAO,CAAC,gBAAgB,GAAG,UAAU,CAAC;QAEtC,OAAO,MAAM,CAAC;IAClB,CAAC,EACD;QACI,OAAO,EAAE,gBAAgB;KAC5B,CACJ,CAAC;AACN,CAAC","sourcesContent":["import { setModelIndentation } from '../../modelApi/block/setModelIndentation';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Indent or outdent to selected paragraphs\n * @param editor The editor to operate on\n * @param indentation Whether indent or outdent\n * @param length The length of pixel to indent/outdent @default 40\n */\nexport function setIndentation(\n editor: IEditor,\n indentation: 'indent' | 'outdent',\n length?: number\n) {\n editor.focus();\n\n editor.formatContentModel(\n (model, context) => {\n const result = setModelIndentation(model, indentation, length);\n\n context.newPendingFormat = 'preserve';\n\n return result;\n },\n {\n apiName: 'setIndentation',\n }\n );\n}\n"]}
@@ -30,7 +30,7 @@ export function formatSegmentWithContentModel(editor, apiName, toggleStyleCallba
30
30
  var modelsFromEntities = [];
31
31
  segmentAndParagraphs.forEach(function (item) {
32
32
  if (item[0].segmentType == 'Entity') {
33
- expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities);
33
+ expandEntitySelections(editor, item[0], formatsAndSegments, modelsFromEntities, item[1]);
34
34
  }
35
35
  else {
36
36
  formatsAndSegments.push([item[0].format, item[0], item[1]]);
@@ -74,7 +74,7 @@ export function formatSegmentWithContentModel(editor, apiName, toggleStyleCallba
74
74
  apiName: apiName,
75
75
  });
76
76
  }
77
- function expandEntitySelections(editor, entity, formatsAndSegments, modelsFromEntities) {
77
+ function expandEntitySelections(editor, entity, formatsAndSegments, modelsFromEntities, paragraph) {
78
78
  var _a = entity.entityFormat, id = _a.id, type = _a.entityType, isReadonly = _a.isReadonly;
79
79
  if (id && type) {
80
80
  var formattableRoots = [];
@@ -98,6 +98,9 @@ function expandEntitySelections(editor, entity, formatsAndSegments, modelsFromEn
98
98
  modelsFromEntities.push([entity, root, model]);
99
99
  }
100
100
  });
101
+ if (formattableRoots.length > 0) {
102
+ formatsAndSegments.push([entity.format, entity, paragraph]);
103
+ }
101
104
  }
102
105
  }
103
106
  function writeBackEntities(editor, modelsFromEntities) {
@@ -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,4BAA4B,EAAE,MAAM,gCAAgC,CAAC;AAC9E,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,mBAIS;IAET,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,CAAC,CAAC,CAAC,CAAC,CAAC;YAExC,oBAAoB,GAAG,mBAAmB,CACtC,KAAK,EACL,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAC7B,CAAC,GAAG,CAAC,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,EAAE,MAAI,EAAE,MAAI,CAAC,EAAf,CAAe,CAAC,CAAC;YAE5B,IAAI,oBAAoB,C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,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC;IACpF,CAAC,CAAC,CAAC;AACP,CAAC","sourcesContent":["import { adjustWordSelection } from '../../modelApi/selection/adjustWordSelection';\nimport { createEditorContextForEntity } from './createEditorContextForEntity';\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 FormatContentModelContext,\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?: (\n model: ReadonlyContentModelDocument,\n isTurningOff: boolean,\n context: FormatContentModelContext\n ) => 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, isTurningOff, context);\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 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
+ {"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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AC;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 { createEditorContextForEntity } from './createEditorContextForEntity';\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 FormatContentModelContext,\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?: (\n model: ReadonlyContentModelDocument,\n isTurningOff: boolean,\n context: FormatContentModelContext\n ) => 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(\n editor,\n item[0],\n formatsAndSegments,\n modelsFromEntities,\n item[1]\n );\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, isTurningOff, context);\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 expandEntitySelections(\n editor: IEditor,\n entity: ContentModelEntity,\n formatsAndSegments: [\n ContentModelSegmentFormat,\n ShallowMutableContentModelSegment | null,\n ShallowMutableContentModelParagraph | null\n ][],\n modelsFromEntities: [ContentModelEntity, FormattableRoot, ContentModelDocument][],\n paragraph: ShallowMutableContentModelParagraph | null\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 if (formattableRoots.length > 0) {\n formatsAndSegments.push([entity.format, entity, paragraph]);\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.42.0",
7
- "roosterjs-content-model-types": "^9.42.0"
6
+ "roosterjs-content-model-dom": "^9.44.0",
7
+ "roosterjs-content-model-types": "^9.44.0"
8
8
  },
9
- "version": "9.42.0",
9
+ "version": "9.44.0",
10
10
  "main": "./lib/index.js",
11
11
  "typings": "./lib/index.d.ts",
12
12
  "module": "./lib-mjs/index.js",