roosterjs-content-model-api 9.28.0 → 9.29.3

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.
@@ -35,17 +35,9 @@ function setListType(model, listType, removeMargins) {
35
35
  (0, roosterjs_content_model_dom_1.setParagraphNotImplicit)(block.blocks[0]);
36
36
  }
37
37
  if (alreadyInExpectedType) {
38
- //if the list item has margins or textAlign, we need to apply them to the block to preserve the indention and alignment
38
+ // if the list item has margins or textAlign, we need to apply them to the block to preserve the indention and alignment
39
39
  block.blocks.forEach(function (x) {
40
- if (block.format.marginLeft) {
41
- x.format.marginLeft = block.format.marginLeft;
42
- }
43
- if (block.format.marginRight) {
44
- x.format.marginRight = block.format.marginRight;
45
- }
46
- if (block.format.textAlign) {
47
- x.format.textAlign = block.format.textAlign;
48
- }
40
+ (0, roosterjs_content_model_dom_1.copyFormat)(x.format, block.format, roosterjs_content_model_dom_1.ListFormats);
49
41
  });
50
42
  }
51
43
  }
@@ -80,17 +72,8 @@ function setListType(model, listType, removeMargins) {
80
72
  }
81
73
  var mutableBlock = (0, roosterjs_content_model_dom_1.mutateBlock)(block);
82
74
  newListItem.blocks.push(mutableBlock);
83
- if (mutableBlock.format.marginRight) {
84
- newListItem.format.marginRight = mutableBlock.format.marginRight;
85
- mutableBlock.format.marginRight = undefined;
86
- }
87
- if (mutableBlock.format.marginLeft) {
88
- newListItem.format.marginLeft = mutableBlock.format.marginLeft;
89
- mutableBlock.format.marginLeft = undefined;
90
- }
91
- if (mutableBlock.format.textAlign) {
92
- newListItem.format.textAlign = mutableBlock.format.textAlign;
93
- }
75
+ (0, roosterjs_content_model_dom_1.copyFormat)(newListItem.format, mutableBlock.format, roosterjs_content_model_dom_1.ListFormatsToMove, true /*deleteOriginalFormat*/);
76
+ (0, roosterjs_content_model_dom_1.copyFormat)(newListItem.format, mutableBlock.format, roosterjs_content_model_dom_1.ListFormatsToKeep);
94
77
  (0, roosterjs_content_model_dom_1.mutateBlock)(parent).blocks.splice(index, 1, newListItem);
95
78
  existingListItems.push(newListItem);
96
79
  var levelIndex = newListItem.levels.length - 1;
@@ -1 +1 @@
1
- {"version":3,"file":"setListType.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/list/setListType.ts"],"names":[],"mappings":";;;AAAA,kFAAiF;AACjF,2EASqC;AASrC;;;;;GAKG;AACH,SAAgB,WAAW,CACvB,KAAmC,EACnC,QAAqB,EACrB,aAA8B;IAA9B,8BAAA,EAAA,qBAA8B;IAE9B,IAAA,uDAA0B,EAAC,KAAK,CAAC,CAAC;IAElC,IAAM,oBAAoB,GAAG,IAAA,kDAAoB,EAC7C,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,IAAA,gDAAkB,EAAuB,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,IAAI,iBAAiB,GAAmC,EAAE,CAAC;IAE3D,oBAAoB,CAAC,OAAO,CAAC,UAAC,EAAiB,EAAE,SAAS;;YAA1B,KAAK,WAAA,EAAE,MAAM,YAAA;QACzC,IAAI,IAAA,gDAAkB,EAAuB,KAAK,EAAE,UAAU,CAAC,EAAE;YAC7D,IAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;YAEjC,IAAI,CAAC,qBAAqB,IAAI,KAAK,EAAE;gBACjC,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC;gBAC1B,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC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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 { splitSelectedParagraphByBr } from '../block/splitSelectedParagraphByBr';\nimport {\n createListItem,\n createListLevel,\n getOperationalBlocks,\n isBlockGroupOfType,\n mutateBlock,\n normalizeContentModel,\n setParagraphNotImplicit,\n updateListMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\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 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 if (block.format.marginLeft) {\n x.format.marginLeft = block.format.marginLeft;\n }\n\n if (block.format.marginRight) {\n x.format.marginRight = block.format.marginRight;\n }\n\n if (block.format.textAlign) {\n x.format.textAlign = block.format.textAlign;\n }\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 if (mutableBlock.format.marginRight) {\n newListItem.format.marginRight = mutableBlock.format.marginRight;\n mutableBlock.format.marginRight = undefined;\n }\n\n if (mutableBlock.format.marginLeft) {\n newListItem.format.marginLeft = mutableBlock.format.marginLeft;\n mutableBlock.format.marginLeft = undefined;\n }\n\n if (mutableBlock.format.textAlign) {\n newListItem.format.textAlign = mutableBlock.format.textAlign;\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,kFAAiF;AACjF,2EAaqC;AAUrC;;;;;GAKG;AACH,SAAgB,WAAW,CACvB,KAAmC,EACnC,QAAqB,EACrB,aAA8B;IAA9B,8BAAA,EAAA,qBAA8B;IAE9B,IAAA,uDAA0B,EAAC,KAAK,CAAC,CAAC;IAElC,IAAM,oBAAoB,GAAG,IAAA,kDAAoB,EAC7C,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,IAAA,gDAAkB,EAAuB,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,IAAI,iBAAiB,GAAmC,EAAE,CAAC;IAE3D,oBAAoB,CAAC,OAAO,CAAC,UAAC,EAA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":["import { 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 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 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"]}
@@ -34,17 +34,9 @@ define(["require", "exports", "../block/splitSelectedParagraphByBr", "roosterjs-
34
34
  (0, roosterjs_content_model_dom_1.setParagraphNotImplicit)(block.blocks[0]);
35
35
  }
36
36
  if (alreadyInExpectedType) {
37
- //if the list item has margins or textAlign, we need to apply them to the block to preserve the indention and alignment
37
+ // if the list item has margins or textAlign, we need to apply them to the block to preserve the indention and alignment
38
38
  block.blocks.forEach(function (x) {
39
- if (block.format.marginLeft) {
40
- x.format.marginLeft = block.format.marginLeft;
41
- }
42
- if (block.format.marginRight) {
43
- x.format.marginRight = block.format.marginRight;
44
- }
45
- if (block.format.textAlign) {
46
- x.format.textAlign = block.format.textAlign;
47
- }
39
+ (0, roosterjs_content_model_dom_1.copyFormat)(x.format, block.format, roosterjs_content_model_dom_1.ListFormats);
48
40
  });
49
41
  }
50
42
  }
@@ -79,17 +71,8 @@ define(["require", "exports", "../block/splitSelectedParagraphByBr", "roosterjs-
79
71
  }
80
72
  var mutableBlock = (0, roosterjs_content_model_dom_1.mutateBlock)(block);
81
73
  newListItem.blocks.push(mutableBlock);
82
- if (mutableBlock.format.marginRight) {
83
- newListItem.format.marginRight = mutableBlock.format.marginRight;
84
- mutableBlock.format.marginRight = undefined;
85
- }
86
- if (mutableBlock.format.marginLeft) {
87
- newListItem.format.marginLeft = mutableBlock.format.marginLeft;
88
- mutableBlock.format.marginLeft = undefined;
89
- }
90
- if (mutableBlock.format.textAlign) {
91
- newListItem.format.textAlign = mutableBlock.format.textAlign;
92
- }
74
+ (0, roosterjs_content_model_dom_1.copyFormat)(newListItem.format, mutableBlock.format, roosterjs_content_model_dom_1.ListFormatsToMove, true /*deleteOriginalFormat*/);
75
+ (0, roosterjs_content_model_dom_1.copyFormat)(newListItem.format, mutableBlock.format, roosterjs_content_model_dom_1.ListFormatsToKeep);
93
76
  (0, roosterjs_content_model_dom_1.mutateBlock)(parent).blocks.splice(index, 1, newListItem);
94
77
  existingListItems.push(newListItem);
95
78
  var levelIndex = newListItem.levels.length - 1;
@@ -1 +1 @@
1
- {"version":3,"file":"setListType.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/list/setListType.ts"],"names":[],"mappings":";;;;IAmBA;;;;;OAKG;IACH,SAAgB,WAAW,CACvB,KAAmC,EACnC,QAAqB,EACrB,aAA8B;QAA9B,8BAAA,EAAA,qBAA8B;QAE9B,IAAA,uDAA0B,EAAC,KAAK,CAAC,CAAC;QAElC,IAAM,oBAAoB,GAAG,IAAA,kDAAoB,EAC7C,KAAK,EACL,CAAC,UAAU,CAAC,EACZ,EAAE,CAAC,8IAA8I;SACpJ,CAAC;QACF,IAAM,qBAAqB,GAAG,oBAAoB,CAAC,KAAK,CAAC,UAAC,EAAS;;gBAAP,KAAK,WAAA;YAC7D,OAAA,IAAA,gDAAkB,EAAuB,KAAK,EAAE,UAAU,CAAC;gBACvD,CAAC,CAAC,CAAA,MAAA,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,0CAAE,QAAQ,KAAI,QAAQ;gBAC7D,CAAC,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAA;SAAA,CACjC,CAAC;QACF,IAAI,iBAAiB,GAAmC,EAAE,CAAC;QAE3D,oBAAoB,CAAC,OAAO,CAAC,UAAC,EAAiB,EAAE,SAAS;;gBAA1B,KAAK,WAAA,EAAE,MAAM,YAAA;YACzC,IAAI,IAAA,gDAAkB,EAAuB,KAAK,EAAE,UAAU,CAAC,EAAE;gBAC7D,IAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;gBAEjC,IAAI,CAAC,qBAAqB,IAAI,KAAK,EAAE;oBACjC,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC;oBAC1B,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;iBAC5B;qBAAM,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,EAAE;oBACj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,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;YACN;gBACI,OAAO,IAAI,CAAC;SACnB;IACL,CAAC","sourcesContent":["import { splitSelectedParagraphByBr } from '../block/splitSelectedParagraphByBr';\nimport {\n createListItem,\n createListLevel,\n getOperationalBlocks,\n isBlockGroupOfType,\n mutateBlock,\n normalizeContentModel,\n setParagraphNotImplicit,\n updateListMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\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 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 if (block.format.marginLeft) {\n x.format.marginLeft = block.format.marginLeft;\n }\n\n if (block.format.marginRight) {\n x.format.marginRight = block.format.marginRight;\n }\n\n if (block.format.textAlign) {\n x.format.textAlign = block.format.textAlign;\n }\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 if (mutableBlock.format.marginRight) {\n newListItem.format.marginRight = mutableBlock.format.marginRight;\n mutableBlock.format.marginRight = undefined;\n }\n\n if (mutableBlock.format.marginLeft) {\n newListItem.format.marginLeft = mutableBlock.format.marginLeft;\n mutableBlock.format.marginLeft = undefined;\n }\n\n if (mutableBlock.format.textAlign) {\n newListItem.format.textAlign = mutableBlock.format.textAlign;\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":";;;;IAwBA;;;;;OAKG;IACH,SAAgB,WAAW,CACvB,KAAmC,EACnC,QAAqB,EACrB,aAA8B;QAA9B,8BAAA,EAAA,qBAA8B;QAE9B,IAAA,uDAA0B,EAAC,KAAK,CAAC,CAAC;QAElC,IAAM,oBAAoB,GAAG,IAAA,kDAAoB,EAC7C,KAAK,EACL,CAAC,UAAU,CAAC,EACZ,EAAE,CAAC,8IAA8I;SACpJ,CAAC;QACF,IAAM,qBAAqB,GAAG,oBAAoB,CAAC,KAAK,CAAC,UAAC,EAAS;;gBAAP,KAAK,WAAA;YAC7D,OAAA,IAAA,gDAAkB,EAAuB,KAAK,EAAE,UAAU,CAAC;gBACvD,CAAC,CAAC,CAAA,MAAA,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,0CAAE,QAAQ,KAAI,QAAQ;gBAC7D,CAAC,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAA;SAAA,CACjC,CAAC;QACF,IAAI,iBAAiB,GAAmC,EAAE,CAAC;QAE3D,oBAAoB,CAAC,OAAO,CAAC,UAAC,EAAiB,EAAE,SAAS;;gBAA1B,KAAK,WAAA,EAAE,MAAM,YAAA;YACzC,IAAI,IAAA,gDAAkB,EAAuB,KAAK,EAAE,UAA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;wBAEpC,IAAM,UAAU,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;wBACjD,IAAM,KAAK,GAAG,IAAA,yCAAW,EAAC,WAAW,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;wBAE1D,IAAI,KAAK,EAAE;4BACP,IAAA,gDAAkB,EAAC,KAAK,EAAE,UAAA,QAAQ;gCAC9B,OAAA,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,QAAQ,EAAE;oCACxB,uBAAuB,EAAE,IAAI;iCAChC,CAAC;4BAFF,CAEE,CACL,CAAC;yBACL;qBACJ;yBAAM;wBACH,iBAAiB,CAAC,OAAO,CACrB,UAAA,CAAC,IAAI,OAAA,CAAC,IAAA,yCAAW,EAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,GAAG,KAAK,CAAC,EAAtD,CAAsD,CAC9D,CAAC;wBACF,iBAAiB,GAAG,EAAE,CAAC;qBAC1B;iBACJ;aACJ;QACL,CAAC,CAAC,CAAC;QAEH,IAAA,mDAAqB,EAAC,KAAK,CAAC,CAAC;QAE7B,OAAO,oBAAoB,CAAC,MAAM,GAAG,CAAC,CAAC;IAC3C,CAAC;IAlHD,kCAkHC;IAED,SAAS,iBAAiB,CAAC,KAAgC;QACvD,QAAQ,KAAK,CAAC,SAAS,EAAE;YACrB,KAAK,OAAO;gBACR,OAAO,KAAK,CAAC;YACjB,KAAK,WAAW;gBACZ,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;YACN;gBACI,OAAO,IAAI,CAAC;SACnB;IACL,CAAC","sourcesContent":["import { 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 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 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,5 +1,5 @@
1
1
  import { splitSelectedParagraphByBr } from '../block/splitSelectedParagraphByBr';
2
- import { createListItem, createListLevel, getOperationalBlocks, isBlockGroupOfType, mutateBlock, normalizeContentModel, setParagraphNotImplicit, updateListMetadata, } from 'roosterjs-content-model-dom';
2
+ import { copyFormat, createListItem, createListLevel, getOperationalBlocks, isBlockGroupOfType, ListFormats, ListFormatsToKeep, ListFormatsToMove, mutateBlock, normalizeContentModel, setParagraphNotImplicit, updateListMetadata, } from 'roosterjs-content-model-dom';
3
3
  /**
4
4
  * Set a list type to content model
5
5
  * @param model the model document
@@ -32,17 +32,9 @@ export function setListType(model, listType, removeMargins) {
32
32
  setParagraphNotImplicit(block.blocks[0]);
33
33
  }
34
34
  if (alreadyInExpectedType) {
35
- //if the list item has margins or textAlign, we need to apply them to the block to preserve the indention and alignment
35
+ // if the list item has margins or textAlign, we need to apply them to the block to preserve the indention and alignment
36
36
  block.blocks.forEach(function (x) {
37
- if (block.format.marginLeft) {
38
- x.format.marginLeft = block.format.marginLeft;
39
- }
40
- if (block.format.marginRight) {
41
- x.format.marginRight = block.format.marginRight;
42
- }
43
- if (block.format.textAlign) {
44
- x.format.textAlign = block.format.textAlign;
45
- }
37
+ copyFormat(x.format, block.format, ListFormats);
46
38
  });
47
39
  }
48
40
  }
@@ -77,17 +69,8 @@ export function setListType(model, listType, removeMargins) {
77
69
  }
78
70
  var mutableBlock = mutateBlock(block);
79
71
  newListItem.blocks.push(mutableBlock);
80
- if (mutableBlock.format.marginRight) {
81
- newListItem.format.marginRight = mutableBlock.format.marginRight;
82
- mutableBlock.format.marginRight = undefined;
83
- }
84
- if (mutableBlock.format.marginLeft) {
85
- newListItem.format.marginLeft = mutableBlock.format.marginLeft;
86
- mutableBlock.format.marginLeft = undefined;
87
- }
88
- if (mutableBlock.format.textAlign) {
89
- newListItem.format.textAlign = mutableBlock.format.textAlign;
90
- }
72
+ copyFormat(newListItem.format, mutableBlock.format, ListFormatsToMove, true /*deleteOriginalFormat*/);
73
+ copyFormat(newListItem.format, mutableBlock.format, ListFormatsToKeep);
91
74
  mutateBlock(parent).blocks.splice(index, 1, newListItem);
92
75
  existingListItems.push(newListItem);
93
76
  var levelIndex = newListItem.levels.length - 1;
@@ -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,0BAA0B,EAAE,MAAM,qCAAqC,CAAC;AACjF,OAAO,EACH,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,kBAAkB,EAClB,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,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,IAAI,iBAAiB,GAAmC,EAAE,CAAC;IAE3D,oBAAoB,CAAC,OAAO,CAAC,UAAC,EAAiB,EAAE,SAAS;;YAA1B,KAAK,WAAA,EAAE,MAAM,YAAA;QACzC,IAAI,kBAAkB,CAAuB,KAAK,EAAE,UAAU,CAAC,EAAE;YAC7D,IAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;YAEjC,IAA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iBAAiB,CAAC,KAAgC;IACvD,QAAQ,KAAK,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 { splitSelectedParagraphByBr } from '../block/splitSelectedParagraphByBr';\nimport {\n createListItem,\n createListLevel,\n getOperationalBlocks,\n isBlockGroupOfType,\n mutateBlock,\n normalizeContentModel,\n setParagraphNotImplicit,\n updateListMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\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 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 if (block.format.marginLeft) {\n x.format.marginLeft = block.format.marginLeft;\n }\n\n if (block.format.marginRight) {\n x.format.marginRight = block.format.marginRight;\n }\n\n if (block.format.textAlign) {\n x.format.textAlign = block.format.textAlign;\n }\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 if (mutableBlock.format.marginRight) {\n newListItem.format.marginRight = mutableBlock.format.marginRight;\n mutableBlock.format.marginRight = undefined;\n }\n\n if (mutableBlock.format.marginLeft) {\n newListItem.format.marginLeft = mutableBlock.format.marginLeft;\n mutableBlock.format.marginLeft = undefined;\n }\n\n if (mutableBlock.format.textAlign) {\n newListItem.format.textAlign = mutableBlock.format.textAlign;\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,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,CAA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,IAAI,IAAI,IAAI,CAAC,CAAC,WAAW,IAAI,iBAAiB,EAA3D,CAA2D,CACnE,CAAC;QACN;YACI,OAAO,IAAI,CAAC;KACnB;AACL,CAAC","sourcesContent":["import { 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 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 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"]}
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.27.0",
7
- "roosterjs-content-model-types": "^9.27.0"
6
+ "roosterjs-content-model-dom": "^9.29.3",
7
+ "roosterjs-content-model-types": "^9.29.3"
8
8
  },
9
- "version": "9.28.0",
9
+ "version": "9.29.3",
10
10
  "main": "./lib/index.js",
11
11
  "typings": "./lib/index.d.ts",
12
12
  "module": "./lib-mjs/index.js",