roosterjs-content-model-api 9.31.0 → 9.33.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 (52) hide show
  1. package/lib/modelApi/block/setModelDirection.d.ts +1 -1
  2. package/lib/modelApi/block/setModelDirection.js +40 -3
  3. package/lib/modelApi/block/setModelDirection.js.map +1 -1
  4. package/lib/modelApi/block/setModelIndentation.js +3 -3
  5. package/lib/modelApi/block/setModelIndentation.js.map +1 -1
  6. package/lib/modelApi/common/adjustIndentation.d.ts +13 -0
  7. package/lib/modelApi/common/adjustIndentation.js +115 -0
  8. package/lib/modelApi/common/adjustIndentation.js.map +1 -0
  9. package/lib/modelApi/list/setListType.js +7 -0
  10. package/lib/modelApi/list/setListType.js.map +1 -1
  11. package/lib/publicApi/block/setDirection.d.ts +2 -2
  12. package/lib/publicApi/block/setDirection.js +1 -1
  13. package/lib/publicApi/block/setDirection.js.map +1 -1
  14. package/lib/publicApi/format/getFormatState.js +1 -1
  15. package/lib/publicApi/format/getFormatState.js.map +1 -1
  16. package/lib/publicApi/table/insertTable.js +2 -0
  17. package/lib/publicApi/table/insertTable.js.map +1 -1
  18. package/lib-amd/modelApi/block/setModelDirection.d.ts +1 -1
  19. package/lib-amd/modelApi/block/setModelDirection.js +40 -3
  20. package/lib-amd/modelApi/block/setModelDirection.js.map +1 -1
  21. package/lib-amd/modelApi/block/setModelIndentation.js +3 -4
  22. package/lib-amd/modelApi/block/setModelIndentation.js.map +1 -1
  23. package/lib-amd/modelApi/common/adjustIndentation.d.ts +13 -0
  24. package/lib-amd/modelApi/common/adjustIndentation.js +115 -0
  25. package/lib-amd/modelApi/common/adjustIndentation.js.map +1 -0
  26. package/lib-amd/modelApi/list/setListType.js +7 -1
  27. package/lib-amd/modelApi/list/setListType.js.map +1 -1
  28. package/lib-amd/publicApi/block/setDirection.d.ts +2 -2
  29. package/lib-amd/publicApi/block/setDirection.js +1 -1
  30. package/lib-amd/publicApi/block/setDirection.js.map +1 -1
  31. package/lib-amd/publicApi/format/getFormatState.js +1 -1
  32. package/lib-amd/publicApi/format/getFormatState.js.map +1 -1
  33. package/lib-amd/publicApi/table/insertTable.js +2 -1
  34. package/lib-amd/publicApi/table/insertTable.js.map +1 -1
  35. package/lib-mjs/modelApi/block/setModelDirection.d.ts +1 -1
  36. package/lib-mjs/modelApi/block/setModelDirection.js +40 -3
  37. package/lib-mjs/modelApi/block/setModelDirection.js.map +1 -1
  38. package/lib-mjs/modelApi/block/setModelIndentation.js +1 -1
  39. package/lib-mjs/modelApi/block/setModelIndentation.js.map +1 -1
  40. package/lib-mjs/modelApi/common/adjustIndentation.d.ts +13 -0
  41. package/lib-mjs/modelApi/common/adjustIndentation.js +110 -0
  42. package/lib-mjs/modelApi/common/adjustIndentation.js.map +1 -0
  43. package/lib-mjs/modelApi/list/setListType.js +7 -0
  44. package/lib-mjs/modelApi/list/setListType.js.map +1 -1
  45. package/lib-mjs/publicApi/block/setDirection.d.ts +2 -2
  46. package/lib-mjs/publicApi/block/setDirection.js +1 -1
  47. package/lib-mjs/publicApi/block/setDirection.js.map +1 -1
  48. package/lib-mjs/publicApi/format/getFormatState.js +1 -1
  49. package/lib-mjs/publicApi/format/getFormatState.js.map +1 -1
  50. package/lib-mjs/publicApi/table/insertTable.js +2 -0
  51. package/lib-mjs/publicApi/table/insertTable.js.map +1 -1
  52. package/package.json +3 -3
@@ -2,4 +2,4 @@ import type { ReadonlyContentModelDocument } from 'roosterjs-content-model-types
2
2
  /**
3
3
  * @internal
4
4
  */
5
- export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl'): boolean;
5
+ export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl' | 'auto'): boolean;
@@ -4,6 +4,11 @@ exports.setModelDirection = void 0;
4
4
  var findListItemsInSameThread_1 = require("../list/findListItemsInSameThread");
5
5
  var splitSelectedParagraphByBr_1 = require("./splitSelectedParagraphByBr");
6
6
  var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
7
+ // Regexes for character direction detection
8
+ // Strongly typed RTL character ranges. Referenced unicode's DerivedBidiClass.txt, excluding things in the 2 bit range.
9
+ var RTL_CHAR_REGEX = /[\u0590-\u05FF\u0600-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/g;
10
+ var URL_CHAR_REGEX = /http\S+|www\S+|https\S+|<a\s+(?:[^>]*?\s+)?href=(["']).*?\1.*?>.*?<\/a>/g;
11
+ var WHITESPACE_REGEX = /\s/g;
7
12
  /**
8
13
  * @internal
9
14
  */
@@ -12,18 +17,25 @@ function setModelDirection(model, direction) {
12
17
  var paragraphOrListItemOrTable = (0, roosterjs_content_model_dom_1.getOperationalBlocks)(model, ['ListItem'], ['TableCell']);
13
18
  paragraphOrListItemOrTable.forEach(function (_a) {
14
19
  var block = _a.block;
20
+ var calcDirection;
21
+ if (direction === 'auto') {
22
+ calcDirection = determineTextDirection(block);
23
+ }
24
+ else {
25
+ calcDirection = direction;
26
+ }
15
27
  if ((0, roosterjs_content_model_dom_1.isBlockGroupOfType)(block, 'ListItem')) {
16
28
  var items = (0, findListItemsInSameThread_1.findListItemsInSameThread)(model, block);
17
29
  items.forEach(function (readonlyItem) {
18
30
  var item = (0, roosterjs_content_model_dom_1.mutateBlock)(readonlyItem);
19
31
  item.levels.forEach(function (level) {
20
- level.format.direction = direction;
32
+ level.format.direction = calcDirection;
21
33
  });
22
- item.blocks.forEach(function (block) { return internalSetDirection(block, direction); });
34
+ item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
23
35
  });
24
36
  }
25
37
  else if (block) {
26
- internalSetDirection(block, direction);
38
+ internalSetDirection(block, calcDirection);
27
39
  }
28
40
  });
29
41
  return paragraphOrListItemOrTable.length > 0;
@@ -70,4 +82,29 @@ function setProperty(format, key, value) {
70
82
  delete format[key];
71
83
  }
72
84
  }
85
+ // Designed to match browser's 'auto' detection, by scanning over the inner text until it hits a strong LTR/RTL character
86
+ function determineTextDirection(block) {
87
+ if (block.blockType === 'Paragraph') {
88
+ var findTextSegements = block.segments.filter(function (seg) { return seg.segmentType === 'Text'; });
89
+ var innerText = findTextSegements.length > 0
90
+ ? findTextSegements.reduce(function (prev, seg) { return prev + seg.text; }, '')
91
+ : undefined;
92
+ if (!!innerText) {
93
+ // Remove links
94
+ innerText = innerText.replace(URL_CHAR_REGEX, '');
95
+ // Remove whitespace
96
+ innerText = innerText.replace(WHITESPACE_REGEX, '');
97
+ var rtlMatches = innerText.match(RTL_CHAR_REGEX);
98
+ var rtlCount = rtlMatches ? rtlMatches.length : 0;
99
+ var ltrCount = innerText.length - rtlCount;
100
+ return rtlCount > ltrCount ? 'rtl' : 'ltr';
101
+ }
102
+ else {
103
+ return 'ltr'; // Default to LTR if no text is found
104
+ }
105
+ }
106
+ else {
107
+ return 'ltr';
108
+ }
109
+ }
73
110
  //# sourceMappingURL=setModelDirection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelDirection.ts"],"names":[],"mappings":";;;AAAA,+EAA8E;AAC9E,2EAA0E;AAC1E,2EAMqC;AAUrC;;GAEG;AACH,SAAgB,iBAAiB,CAAC,KAAmC,EAAE,SAAwB;IAC3F,IAAA,uDAA0B,EAAC,KAAK,CAAC,CAAC;IAElC,IAAM,0BAA0B,GAAG,IAAA,kDAAoB,EACnD,KAAK,EACL,CAAC,UAAU,CAAC,EACZ,CAAC,WAAW,CAAC,CAChB,CAAC;IAEF,0BAA0B,CAAC,OAAO,CAAC,UAAC,EAAS;YAAP,KAAK,WAAA;QACvC,IAAI,IAAA,gDAAkB,EAAuB,KAAK,EAAE,UAAU,CAAC,EAAE;YAC7D,IAAM,KAAK,GAAG,IAAA,qDAAyB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YAEtD,KAAK,CAAC,OAAO,CAAC,UAAA,YAAY;gBACtB,IAAM,IAAI,GAAG,IAAA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AI,KAAK,IAAI,KAAK,CAAC,SAAS,IAAI,OAAO,EAAE;YACrC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAA,GAAG;gBAClB,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,UAAA,IAAI;oBAClB,oDAAoD;oBACpD,IAAA,qDAAuB,EAAC,IAAA,yCAAW,EAAC,IAAI,CAAC,EAAE,UAAA,QAAQ;wBAC/C,IAAI,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,cAAc,EAAE;4BAC1B,IAAM,eAAe,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC;4BAC/C,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;4BAChE,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,aAAa,EAAE,eAAe,CAAC,CAAC;yBAC5D;wBACD,OAAO,QAAQ,CAAC;oBACpB,CAAC,CAAC,CAAC;gBACP,CAAC,CAAC,CAAC;YACP,CAAC,CAAC,CAAC;YAEH,wBAAwB;YACxB,IAAA,8CAAgB,EAAC,KAAK,EAAE,SAAS,CAAC,eAAe,EAAE,IAAI,CAAC,kBAAkB,CAAC,CAAC;SAC/E;KACJ;AACL,CAAC;AAED,SAAS,WAAW,CAChB,MAAmD,EACnD,GAAwD,EACxD,KAAyB;IAEzB,IAAI,KAAK,EAAE;QACP,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;KACvB;SAAM;QACH,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;KACtB;AACL,CAAC","sourcesContent":["import { findListItemsInSameThread } from '../list/findListItemsInSameThread';\nimport { splitSelectedParagraphByBr } from './splitSelectedParagraphByBr';\nimport {\n applyTableFormat,\n getOperationalBlocks,\n isBlockGroupOfType,\n mutateBlock,\n updateTableCellMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n BorderFormat,\n ContentModelListItem,\n MarginFormat,\n PaddingFormat,\n ReadonlyContentModelBlock,\n ReadonlyContentModelDocument,\n} from 'roosterjs-content-model-types';\n\n/**\n * @internal\n */\nexport function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl') {\n splitSelectedParagraphByBr(model);\n\n const paragraphOrListItemOrTable = getOperationalBlocks<ContentModelListItem>(\n model,\n ['ListItem'],\n ['TableCell']\n );\n\n paragraphOrListItemOrTable.forEach(({ block }) => {\n if (isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')) {\n const items = findListItemsInSameThread(model, block);\n\n items.forEach(readonlyItem => {\n const item = mutateBlock(readonlyItem);\n\n item.levels.forEach(level => {\n level.format.direction = direction;\n });\n\n item.blocks.forEach(block => internalSetDirection(block, direction));\n });\n } else if (block) {\n internalSetDirection(block, direction);\n }\n });\n\n return paragraphOrListItemOrTable.length > 0;\n}\n\nfunction internalSetDirection(block: ReadonlyContentModelBlock, direction: 'ltr' | 'rtl') {\n const wasRtl = block.format.direction == 'rtl';\n const isRtl = direction == 'rtl';\n\n if (wasRtl != isRtl) {\n const { format } = mutateBlock(block);\n format.direction = direction;\n\n // Adjust margin when change direction\n const marginLeft = format.marginLeft;\n const paddingLeft = format.paddingLeft;\n\n setProperty(format, 'marginLeft', format.marginRight);\n setProperty(format, 'marginRight', marginLeft);\n setProperty(format, 'paddingLeft', format.paddingRight);\n setProperty(format, 'paddingRight', paddingLeft);\n\n // If whole Table direction changed, flip cell side borders\n if (block && block.blockType == 'Table') {\n block.rows.forEach(row => {\n row.cells.forEach(cell => {\n // Optimise by skipping cells with unchanged borders\n updateTableCellMetadata(mutateBlock(cell), metadata => {\n if (metadata?.borderOverride) {\n const storeBorderLeft = cell.format.borderLeft;\n setProperty(cell.format, 'borderLeft', cell.format.borderRight);\n setProperty(cell.format, 'borderRight', storeBorderLeft);\n }\n return metadata;\n });\n });\n });\n\n // Apply changed borders\n applyTableFormat(block, undefined /* newFormat */, true /* keepCellShade*/);\n }\n }\n}\n\nfunction setProperty(\n format: MarginFormat & PaddingFormat & BorderFormat,\n key: keyof (MarginFormat & PaddingFormat & BorderFormat),\n value: string | undefined\n) {\n if (value) {\n format[key] = value;\n } else {\n delete format[key];\n }\n}\n"]}
1
+ {"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelDirection.ts"],"names":[],"mappings":";;;AAAA,+EAA8E;AAC9E,2EAA0E;AAC1E,2EAMqC;AAWrC,4CAA4C;AAC5C,uHAAuH;AACvH,IAAM,cAAc,GAAG,yDAAyD,CAAC;AACjF,IAAM,cAAc,GAAG,0EAA0E,CAAC;AAClG,IAAM,gBAAgB,GAAG,KAAK,CAAC;AAE/B;;GAEG;AACH,SAAgB,iBAAiB,CAC7B,KAAmC,EACnC,SAAiC;IAEjC,IAAA,uDAA0B,EAAC,KAAK,CAAC,CAAC;IAElC,IAAM,0BAA0B,GAAG,IAAA,kDAAoB,EACnD,KAAK,EACL,CAAC,UAAU,CAAC,EACZ,CAAC,WAAW,CAAC,CAChB,CAAC;IAEF,0BAA0B,CAAC,OAAO,CAAC,UAAC,EAAS;YAAP,KAAK,WAAA;QACvC,IAAI,aAA4B,CAAC;QACjC,IAAI,SAAS,KAAK,MAAM,EAAE;YACtB,aAAa,GAAG,sBAAsB,CAAC,KAAK,CAAC,CAAC;SACjD;aAAM;YACH,aAAa,GAAG,SAAS,CAAC;SAC7B;QACD,IAAI,IAAA,gDAAkB,EAAuB,KAAK,EAAE,UAAU,CAAC,EAAE;YAC7D,IAAM,KAAK,GAAG,IAAA,qDAAyB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YAEtD,KAAK,CAAC,OAAO,CAAC,UAAA,YAAY;gBACtB,IAAM,IAAI,GAAG,IAAA,yCAAW,EAAC,YAAY,CAAC,CAAC;gBAEvC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAA,KAAK;oBACrB,KAAK,CAAC,MAAM,CAAC,SAAS,GAAG,aAAa,CAAC;gBAC3C,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,GAAG,CAAC,IAAI,EAAf,CAAe,EAAE,EAAE,CAAC;YAC9D,CAAC,CAAC,SAAS,CAAC;QACpB,IAAI,CAAC,CAAC,SAAS,EAAE;YACb,eAAe;YACf,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;YAElD,oBAAoB;YACpB,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,gBAAgB,EAAE,EAAE,CAAC,CAAC;YAEpD,IAAM,UAAU,GAAG,SAAS,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;YACnD,IAAM,QAAQ,GAAG,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;YAEpD,IAAM,QAAQ,GAAG,SAAS,CAAC,MAAM,GAAG,QAAQ,CAAC;YAE7C,OAAO,QAAQ,GAAG,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC;SAC9C;aAAM;YACH,OAAO,KAAK,CAAC,CAAC,qCAAqC;SACtD;KACJ;SAAM;QACH,OAAO,KAAK,CAAC;KAChB;AACL,CAAC","sourcesContent":["import { findListItemsInSameThread } from '../list/findListItemsInSameThread';\nimport { splitSelectedParagraphByBr } from './splitSelectedParagraphByBr';\nimport {\n applyTableFormat,\n getOperationalBlocks,\n isBlockGroupOfType,\n mutateBlock,\n updateTableCellMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n BorderFormat,\n ContentModelListItem,\n MarginFormat,\n PaddingFormat,\n ReadonlyContentModelBlock,\n ReadonlyContentModelDocument,\n ReadonlyContentModelText,\n} from 'roosterjs-content-model-types';\n\n// Regexes for character direction detection\n// Strongly typed RTL character ranges. Referenced unicode's DerivedBidiClass.txt, excluding things in the 2 bit range.\nconst RTL_CHAR_REGEX = /[\\u0590-\\u05FF\\u0600-\\u08FF\\uFB1D-\\uFDFF\\uFE70-\\uFEFF]/g;\nconst URL_CHAR_REGEX = /http\\S+|www\\S+|https\\S+|<a\\s+(?:[^>]*?\\s+)?href=([\"']).*?\\1.*?>.*?<\\/a>/g;\nconst WHITESPACE_REGEX = /\\s/g;\n\n/**\n * @internal\n */\nexport function setModelDirection(\n model: ReadonlyContentModelDocument,\n direction: 'ltr' | 'rtl' | 'auto'\n) {\n splitSelectedParagraphByBr(model);\n\n const paragraphOrListItemOrTable = getOperationalBlocks<ContentModelListItem>(\n model,\n ['ListItem'],\n ['TableCell']\n );\n\n paragraphOrListItemOrTable.forEach(({ block }) => {\n let calcDirection: 'ltr' | 'rtl';\n if (direction === 'auto') {\n calcDirection = determineTextDirection(block);\n } else {\n calcDirection = direction;\n }\n if (isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')) {\n const items = findListItemsInSameThread(model, block);\n\n items.forEach(readonlyItem => {\n const item = mutateBlock(readonlyItem);\n\n item.levels.forEach(level => {\n level.format.direction = calcDirection;\n });\n\n item.blocks.forEach(block => internalSetDirection(block, calcDirection));\n });\n } else if (block) {\n internalSetDirection(block, calcDirection);\n }\n });\n\n return paragraphOrListItemOrTable.length > 0;\n}\n\nfunction internalSetDirection(block: ReadonlyContentModelBlock, direction: 'ltr' | 'rtl') {\n const wasRtl = block.format.direction == 'rtl';\n const isRtl = direction == 'rtl';\n\n if (wasRtl != isRtl) {\n const { format } = mutateBlock(block);\n format.direction = direction;\n\n // Adjust margin when change direction\n const marginLeft = format.marginLeft;\n const paddingLeft = format.paddingLeft;\n\n setProperty(format, 'marginLeft', format.marginRight);\n setProperty(format, 'marginRight', marginLeft);\n setProperty(format, 'paddingLeft', format.paddingRight);\n setProperty(format, 'paddingRight', paddingLeft);\n\n // If whole Table direction changed, flip cell side borders\n if (block && block.blockType == 'Table') {\n block.rows.forEach(row => {\n row.cells.forEach(cell => {\n // Optimise by skipping cells with unchanged borders\n updateTableCellMetadata(mutateBlock(cell), metadata => {\n if (metadata?.borderOverride) {\n const storeBorderLeft = cell.format.borderLeft;\n setProperty(cell.format, 'borderLeft', cell.format.borderRight);\n setProperty(cell.format, 'borderRight', storeBorderLeft);\n }\n return metadata;\n });\n });\n });\n\n // Apply changed borders\n applyTableFormat(block, undefined /* newFormat */, true /* keepCellShade*/);\n }\n }\n}\n\nfunction setProperty(\n format: MarginFormat & PaddingFormat & BorderFormat,\n key: keyof (MarginFormat & PaddingFormat & BorderFormat),\n value: string | undefined\n) {\n if (value) {\n format[key] = value;\n } else {\n delete format[key];\n }\n}\n\n// Designed to match browser's 'auto' detection, by scanning over the inner text until it hits a strong LTR/RTL character\nfunction determineTextDirection(block: ReadonlyContentModelBlock): 'ltr' | 'rtl' {\n if (block.blockType === 'Paragraph') {\n const findTextSegements: ReadonlyContentModelText[] = block.segments.filter(\n (seg): seg is ReadonlyContentModelText => seg.segmentType === 'Text'\n );\n let innerText =\n findTextSegements.length > 0\n ? findTextSegements.reduce((prev, seg) => prev + seg.text, '')\n : undefined;\n if (!!innerText) {\n // Remove links\n innerText = innerText.replace(URL_CHAR_REGEX, '');\n\n // Remove whitespace\n innerText = innerText.replace(WHITESPACE_REGEX, '');\n\n const rtlMatches = innerText.match(RTL_CHAR_REGEX);\n const rtlCount = rtlMatches ? rtlMatches.length : 0;\n\n const ltrCount = innerText.length - rtlCount;\n\n return rtlCount > ltrCount ? 'rtl' : 'ltr';\n } else {\n return 'ltr'; // Default to LTR if no text is found\n }\n } else {\n return 'ltr';\n }\n}\n"]}
@@ -4,9 +4,9 @@ exports.setModelIndentation = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var findListItemsInSameThread_1 = require("../list/findListItemsInSameThread");
6
6
  var getListAnnounceData_1 = require("../list/getListAnnounceData");
7
+ var adjustIndentation_1 = require("../common/adjustIndentation");
7
8
  var splitSelectedParagraphByBr_1 = require("./splitSelectedParagraphByBr");
8
9
  var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
9
- var IndentStepInPixel = 40;
10
10
  /**
11
11
  * @param model The content model to set indentation
12
12
  * @param indentation The indentation type, 'indent' to indent, 'outdent' to outdent
@@ -14,7 +14,7 @@ var IndentStepInPixel = 40;
14
14
  * Set indentation for selected list items or paragraphs
15
15
  */
16
16
  function setModelIndentation(model, indentation, length, context) {
17
- if (length === void 0) { length = IndentStepInPixel; }
17
+ if (length === void 0) { length = adjustIndentation_1.IndentStepInPixel; }
18
18
  (0, splitSelectedParagraphByBr_1.splitSelectedParagraphByBr)(model);
19
19
  var paragraphOrListItem = (0, roosterjs_content_model_dom_1.getOperationalBlocks)(model, ['ListItem'], ['TableCell']);
20
20
  var isIndent = indentation == 'indent';
@@ -134,7 +134,7 @@ function isMultilevelSelection(model, listItem, parent) {
134
134
  return false;
135
135
  }
136
136
  function calculateMarginValue(format, isIndent, length) {
137
- if (length === void 0) { length = IndentStepInPixel; }
137
+ if (length === void 0) { length = adjustIndentation_1.IndentStepInPixel; }
138
138
  var marginLeft = format.marginLeft, marginRight = format.marginRight, direction = format.direction;
139
139
  var isRtl = direction == 'rtl';
140
140
  var originalValue = (0, roosterjs_content_model_dom_1.parseValueWithUnit)(isRtl ? marginRight : marginLeft);
@@ -1 +1 @@
1
- {"version":3,"file":"setModelIndentation.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelIndentation.ts"],"names":[],"mappings":";;;;AAAA,+EAA8E;AAC9E,mEAAkE;AAClE,2EAA0E;AAC1E,2EAOqC;AAYrC,IAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B;;;;;GAKG;AACH,SAAgB,mBAAmB,CAC/B,KAAmC,EACnC,WAAiC,EACjC,MAAkC,EAClC,OAAmC;IADnC,uBAAA,EAAA,0BAAkC;IAGlC,IAAA,uDAA0B,EAAC,KAAK,CAAC,CAAC;IAElC,IAAM,mBAAmB,GAAG,IAAA,kDAAoB,EAC5C,KAAK,EACL,CAAC,UAAU,CAAC,EACZ,CAAC,WAAW,CAAC,CAChB,CAAC;IACF,IAAM,QAAQ,GAAG,WAAW,IAAI,QAAQ,CAAC;IACzC,IAAM,cAAc,GAAgC,E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AyC,EACzC,QAAiB,EACjB,MAAkC;IAAlC,uBAAA,EAAA,0BAAkC;IAE1B,IAAA,UAAU,GAA6B,MAAM,WAAnC,EAAE,WAAW,GAAgB,MAAM,YAAtB,EAAE,SAAS,GAAK,MAAM,UAAX,CAAY;IACtD,IAAM,KAAK,GAAG,SAAS,IAAI,KAAK,CAAC;IACjC,IAAM,aAAa,GAAG,IAAA,gDAAkB,EAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;IAC3E,IAAI,QAAQ,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,aAAa,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC;IAEpF,IAAI,QAAQ,IAAI,aAAa,EAAE;QAC3B,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;KACnE;IAED,IAAI,QAAQ,IAAI,aAAa,EAAE;QAC3B,oDAAoD;QACpD,OAAO,IAAI,CAAC;KACf;SAAM;QACH,OAAO,QAAQ,CAAC;KACnB;AACL,CAAC","sourcesContent":["import { findListItemsInSameThread } from '../list/findListItemsInSameThread';\nimport { getListAnnounceData } from '../list/getListAnnounceData';\nimport { splitSelectedParagraphByBr } from './splitSelectedParagraphByBr';\nimport {\n createListLevel,\n getOperationalBlocks,\n isBlockGroupOfType,\n mutateBlock,\n parseValueWithUnit,\n updateListMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelBlockFormat,\n ContentModelListItem,\n ContentModelListLevel,\n FormatContentModelContext,\n ReadonlyContentModelBlock,\n ReadonlyContentModelBlockGroup,\n ReadonlyContentModelDocument,\n ReadonlyContentModelListItem,\n} from 'roosterjs-content-model-types';\n\nconst IndentStepInPixel = 40;\n\n/**\n * @param model The content model to set indentation\n * @param indentation The indentation type, 'indent' to indent, 'outdent' to outdent\n * @param length The length of indentation in pixel, default value is 40\n * Set indentation for selected list items or paragraphs\n */\nexport function setModelIndentation(\n model: ReadonlyContentModelDocument,\n indentation: 'indent' | 'outdent',\n length: number = IndentStepInPixel,\n context?: FormatContentModelContext\n) {\n splitSelectedParagraphByBr(model);\n\n const paragraphOrListItem = getOperationalBlocks<ContentModelListItem>(\n model,\n ['ListItem'],\n ['TableCell']\n );\n const isIndent = indentation == 'indent';\n const modifiedBlocks: ReadonlyContentModelBlock[] = [];\n\n paragraphOrListItem.forEach(({ block, parent, path }) => {\n if (isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')) {\n const thread = findListItemsInSameThread(model, block);\n const firstItem = thread[0];\n //if the first item is selected and has only one level, we should add margin to the whole list\n if (isSelected(firstItem) && firstItem.levels.length == 1) {\n const level = block.levels[0];\n const { format } = level;\n const { marginLeft, marginRight } = format;\n const newValue = calculateMarginValue(format, isIndent, length);\n const isRtl = format.direction == 'rtl';\n const originalValue = parseValueWithUnit(isRtl ? marginRight : marginLeft);\n\n if (!isIndent && originalValue == 0) {\n block.levels.pop();\n } else if (newValue !== null) {\n if (isRtl) {\n level.format.marginRight = newValue + 'px';\n } else {\n level.format.marginLeft = newValue + 'px';\n }\n }\n //if block has only one level, there is not need to check if it is multilevel selection\n } else if (block.levels.length == 1 || !isMultilevelSelection(model, block, parent)) {\n if (isIndent) {\n const threadIdx = thread.indexOf(block);\n const previousItem = thread[threadIdx - 1];\n const nextItem = thread[threadIdx + 1];\n const levelLength = block.levels.length;\n const lastLevel = block.levels[levelLength - 1];\n const newLevel: ContentModelListLevel = createListLevel(\n lastLevel?.listType || 'UL',\n lastLevel?.format,\n previousItem && previousItem.levels.length > levelLength\n ? previousItem.levels[levelLength].dataset\n : nextItem && nextItem.levels.length > levelLength\n ? nextItem.levels[levelLength].dataset\n : undefined\n );\n\n updateListMetadata(newLevel, metadata => {\n metadata = metadata || {};\n metadata.applyListStyleFromLevel = true;\n return metadata;\n });\n\n // New level is totally new, no need to have these attributes for now\n delete newLevel.format.startNumberOverride;\n\n block.levels.push(newLevel);\n } else {\n block.levels.pop();\n }\n\n if (block.levels.length > 0 && context) {\n context.announceData = getListAnnounceData([block, ...path]);\n }\n }\n } else if (block) {\n let currentBlock: ReadonlyContentModelBlock = block;\n let currentParent: ReadonlyContentModelBlockGroup = parent;\n\n while (currentParent && modifiedBlocks.indexOf(currentBlock) < 0) {\n const index = path.indexOf(currentParent);\n const { format } = mutateBlock(currentBlock);\n const newValue = calculateMarginValue(format, isIndent, length);\n\n if (newValue !== null) {\n const isRtl = format.direction == 'rtl';\n\n if (isRtl) {\n format.marginRight = newValue + 'px';\n } else {\n format.marginLeft = newValue + 'px';\n }\n\n modifiedBlocks.push(currentBlock);\n\n break;\n } else if (currentParent.blockGroupType == 'FormatContainer' && index >= 0) {\n mutateBlock(currentParent);\n\n currentBlock = currentParent;\n currentParent = path[index + 1];\n } else {\n break;\n }\n }\n }\n });\n\n return paragraphOrListItem.length > 0;\n}\n\nfunction isSelected(listItem: ReadonlyContentModelListItem) {\n return listItem.blocks.some(block => {\n if (block.blockType == 'Paragraph') {\n return block.segments.some(segment => segment.isSelected);\n }\n });\n}\n\n/*\n * Check if the selection has list items with different levels and the first item of the list is selected, do not create a sub list.\n * Otherwise, the margin of the first item will be changed, and the sub list will be created, creating a unintentional margin difference between the list items.\n */\nfunction isMultilevelSelection(\n model: ReadonlyContentModelDocument,\n listItem: ReadonlyContentModelListItem,\n parent: ReadonlyContentModelBlockGroup\n) {\n const listIndex = parent.blocks.indexOf(listItem);\n for (let i = listIndex - 1; i >= 0; i--) {\n const block = parent.blocks[i];\n if (\n isBlockGroupOfType<ContentModelListItem>(block, 'ListItem') &&\n block.levels.length == 1 &&\n isSelected(block)\n ) {\n const firstItem = findListItemsInSameThread(model, block)[0];\n return isSelected(firstItem);\n }\n\n if (!isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')) {\n return false;\n }\n }\n return false;\n}\n\nfunction calculateMarginValue(\n format: Readonly<ContentModelBlockFormat>,\n isIndent: boolean,\n length: number = IndentStepInPixel\n): number | null {\n const { marginLeft, marginRight, direction } = format;\n const isRtl = direction == 'rtl';\n const originalValue = parseValueWithUnit(isRtl ? marginRight : marginLeft);\n let newValue = (isIndent ? Math.ceil : Math.floor)(originalValue / length) * length;\n\n if (newValue == originalValue) {\n newValue = Math.max(newValue + length * (isIndent ? 1 : -1), 0);\n }\n\n if (newValue == originalValue) {\n // Return null to let caller know nothing is changed\n return null;\n } else {\n return newValue;\n }\n}\n"]}
1
+ {"version":3,"file":"setModelIndentation.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelIndentation.ts"],"names":[],"mappings":";;;;AAAA,+EAA8E;AAC9E,mEAAkE;AAClE,iEAAgE;AAChE,2EAA0E;AAC1E,2EAOqC;AAYrC;;;;;GAKG;AACH,SAAgB,mBAAmB,CAC/B,KAAmC,EACnC,WAAiC,EACjC,MAAkC,EAClC,OAAmC;IADnC,uBAAA,EAAA,SAAiB,qCAAiB;IAGlC,IAAA,uDAA0B,EAAC,KAAK,CAAC,CAAC;IAElC,IAAM,mBAAmB,GAAG,IAAA,kDAAoB,EAC5C,KAAK,EACL,CAAC,UAAU,CAAC,EACZ,CAAC,WAAW,CAAC,CAChB,CAAC;IACF,IAAM,QAAQ,GAAG,WAAW,IAAI,QAAQ,CAAC;IACzC,IAAM,cAAc,GAAgC,EAAE,CAAC;IAEvD,mBAAmB,CAAC,OAAO,CAAC,UAAC,EAAuB;YAArB,KAAK,WAAA,EAAE,MAAM,YAAA,EAAE,IAAI,UAAA;QAC9C,IAAI,IAAA,gDAAkB,EAAuB,KAAK,EAAE,UAAU,CAAC,EAAE;YAC7D,IAAM,MAAM,GAAG,IAAA,qDAAyB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YACvD,IAAM,SAAS,GAAG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AAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;IAC3E,IAAI,QAAQ,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,aAAa,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC;IAEpF,IAAI,QAAQ,IAAI,aAAa,EAAE;QAC3B,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;KACnE;IAED,IAAI,QAAQ,IAAI,aAAa,EAAE;QAC3B,oDAAoD;QACpD,OAAO,IAAI,CAAC;KACf;SAAM;QACH,OAAO,QAAQ,CAAC;KACnB;AACL,CAAC","sourcesContent":["import { findListItemsInSameThread } from '../list/findListItemsInSameThread';\nimport { getListAnnounceData } from '../list/getListAnnounceData';\nimport { IndentStepInPixel } from '../common/adjustIndentation';\nimport { splitSelectedParagraphByBr } from './splitSelectedParagraphByBr';\nimport {\n createListLevel,\n getOperationalBlocks,\n isBlockGroupOfType,\n mutateBlock,\n parseValueWithUnit,\n updateListMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelBlockFormat,\n ContentModelListItem,\n ContentModelListLevel,\n FormatContentModelContext,\n ReadonlyContentModelBlock,\n ReadonlyContentModelBlockGroup,\n ReadonlyContentModelDocument,\n ReadonlyContentModelListItem,\n} from 'roosterjs-content-model-types';\n\n/**\n * @param model The content model to set indentation\n * @param indentation The indentation type, 'indent' to indent, 'outdent' to outdent\n * @param length The length of indentation in pixel, default value is 40\n * Set indentation for selected list items or paragraphs\n */\nexport function setModelIndentation(\n model: ReadonlyContentModelDocument,\n indentation: 'indent' | 'outdent',\n length: number = IndentStepInPixel,\n context?: FormatContentModelContext\n) {\n splitSelectedParagraphByBr(model);\n\n const paragraphOrListItem = getOperationalBlocks<ContentModelListItem>(\n model,\n ['ListItem'],\n ['TableCell']\n );\n const isIndent = indentation == 'indent';\n const modifiedBlocks: ReadonlyContentModelBlock[] = [];\n\n paragraphOrListItem.forEach(({ block, parent, path }) => {\n if (isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')) {\n const thread = findListItemsInSameThread(model, block);\n const firstItem = thread[0];\n //if the first item is selected and has only one level, we should add margin to the whole list\n if (isSelected(firstItem) && firstItem.levels.length == 1) {\n const level = block.levels[0];\n const { format } = level;\n const { marginLeft, marginRight } = format;\n const newValue = calculateMarginValue(format, isIndent, length);\n const isRtl = format.direction == 'rtl';\n const originalValue = parseValueWithUnit(isRtl ? marginRight : marginLeft);\n\n if (!isIndent && originalValue == 0) {\n block.levels.pop();\n } else if (newValue !== null) {\n if (isRtl) {\n level.format.marginRight = newValue + 'px';\n } else {\n level.format.marginLeft = newValue + 'px';\n }\n }\n //if block has only one level, there is not need to check if it is multilevel selection\n } else if (block.levels.length == 1 || !isMultilevelSelection(model, block, parent)) {\n if (isIndent) {\n const threadIdx = thread.indexOf(block);\n const previousItem = thread[threadIdx - 1];\n const nextItem = thread[threadIdx + 1];\n const levelLength = block.levels.length;\n const lastLevel = block.levels[levelLength - 1];\n const newLevel: ContentModelListLevel = createListLevel(\n lastLevel?.listType || 'UL',\n lastLevel?.format,\n previousItem && previousItem.levels.length > levelLength\n ? previousItem.levels[levelLength].dataset\n : nextItem && nextItem.levels.length > levelLength\n ? nextItem.levels[levelLength].dataset\n : undefined\n );\n\n updateListMetadata(newLevel, metadata => {\n metadata = metadata || {};\n metadata.applyListStyleFromLevel = true;\n return metadata;\n });\n\n // New level is totally new, no need to have these attributes for now\n delete newLevel.format.startNumberOverride;\n\n block.levels.push(newLevel);\n } else {\n block.levels.pop();\n }\n\n if (block.levels.length > 0 && context) {\n context.announceData = getListAnnounceData([block, ...path]);\n }\n }\n } else if (block) {\n let currentBlock: ReadonlyContentModelBlock = block;\n let currentParent: ReadonlyContentModelBlockGroup = parent;\n\n while (currentParent && modifiedBlocks.indexOf(currentBlock) < 0) {\n const index = path.indexOf(currentParent);\n const { format } = mutateBlock(currentBlock);\n const newValue = calculateMarginValue(format, isIndent, length);\n\n if (newValue !== null) {\n const isRtl = format.direction == 'rtl';\n\n if (isRtl) {\n format.marginRight = newValue + 'px';\n } else {\n format.marginLeft = newValue + 'px';\n }\n\n modifiedBlocks.push(currentBlock);\n\n break;\n } else if (currentParent.blockGroupType == 'FormatContainer' && index >= 0) {\n mutateBlock(currentParent);\n\n currentBlock = currentParent;\n currentParent = path[index + 1];\n } else {\n break;\n }\n }\n }\n });\n\n return paragraphOrListItem.length > 0;\n}\n\nfunction isSelected(listItem: ReadonlyContentModelListItem) {\n return listItem.blocks.some(block => {\n if (block.blockType == 'Paragraph') {\n return block.segments.some(segment => segment.isSelected);\n }\n });\n}\n\n/*\n * Check if the selection has list items with different levels and the first item of the list is selected, do not create a sub list.\n * Otherwise, the margin of the first item will be changed, and the sub list will be created, creating a unintentional margin difference between the list items.\n */\nfunction isMultilevelSelection(\n model: ReadonlyContentModelDocument,\n listItem: ReadonlyContentModelListItem,\n parent: ReadonlyContentModelBlockGroup\n) {\n const listIndex = parent.blocks.indexOf(listItem);\n for (let i = listIndex - 1; i >= 0; i--) {\n const block = parent.blocks[i];\n if (\n isBlockGroupOfType<ContentModelListItem>(block, 'ListItem') &&\n block.levels.length == 1 &&\n isSelected(block)\n ) {\n const firstItem = findListItemsInSameThread(model, block)[0];\n return isSelected(firstItem);\n }\n\n if (!isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')) {\n return false;\n }\n }\n return false;\n}\n\nfunction calculateMarginValue(\n format: Readonly<ContentModelBlockFormat>,\n isIndent: boolean,\n length: number = IndentStepInPixel\n): number | null {\n const { marginLeft, marginRight, direction } = format;\n const isRtl = direction == 'rtl';\n const originalValue = parseValueWithUnit(isRtl ? marginRight : marginLeft);\n let newValue = (isIndent ? Math.ceil : Math.floor)(originalValue / length) * length;\n\n if (newValue == originalValue) {\n newValue = Math.max(newValue + length * (isIndent ? 1 : -1), 0);\n }\n\n if (newValue == originalValue) {\n // Return null to let caller know nothing is changed\n return null;\n } else {\n return newValue;\n }\n}\n"]}
@@ -0,0 +1,13 @@
1
+ import type { ContentModelTable, InsertPoint, ShallowMutableContentModelListItem } from 'roosterjs-content-model-types';
2
+ /**
3
+ * @internal
4
+ */
5
+ export declare const IndentStepInPixel = 40;
6
+ /**
7
+ * @internal
8
+ */
9
+ export declare function adjustListIndentation(listItem: ShallowMutableContentModelListItem): void;
10
+ /**
11
+ * @internal
12
+ */
13
+ export declare function adjustTableIndentation(insertPoint: InsertPoint, table: ContentModelTable): void;
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.adjustTableIndentation = exports.adjustListIndentation = exports.IndentStepInPixel = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
6
+ var EN_SPACE = '\u2002';
7
+ var REGULAR_SPACE = '\u0020';
8
+ var NON_BREAK_SPACES = '\u00A0';
9
+ /**
10
+ * @internal
11
+ */
12
+ exports.IndentStepInPixel = 40;
13
+ function countTabsSpaces(text) {
14
+ var spaces = countSpacesBeforeText(text);
15
+ var tabSpaces = Math.floor(spaces / 4);
16
+ return tabSpaces;
17
+ }
18
+ function countSpacesBeforeText(str) {
19
+ var e_1, _a;
20
+ var count = 0;
21
+ try {
22
+ for (var str_1 = (0, tslib_1.__values)(str), str_1_1 = str_1.next(); !str_1_1.done; str_1_1 = str_1.next()) {
23
+ var char = str_1_1.value;
24
+ if (char === EN_SPACE || char === NON_BREAK_SPACES || char == REGULAR_SPACE) {
25
+ count++;
26
+ }
27
+ else {
28
+ break;
29
+ }
30
+ }
31
+ }
32
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
33
+ finally {
34
+ try {
35
+ if (str_1_1 && !str_1_1.done && (_a = str_1.return)) _a.call(str_1);
36
+ }
37
+ finally { if (e_1) throw e_1.error; }
38
+ }
39
+ return count;
40
+ }
41
+ /**
42
+ * @internal
43
+ */
44
+ function adjustListIndentation(listItem) {
45
+ var block = listItem.blocks[0];
46
+ if (block.blockType == 'Paragraph' &&
47
+ block.segments.length > 0 &&
48
+ block.segments[0].segmentType == 'Text') {
49
+ var tabSpaces_1 = countTabsSpaces(block.segments[0].text);
50
+ if (tabSpaces_1 > 0) {
51
+ (0, roosterjs_content_model_dom_1.mutateSegment)(block, block.segments[0], function (textSegment) {
52
+ textSegment.text = textSegment.text.substring(tabSpaces_1 * 4);
53
+ });
54
+ listItem.levels[0].format.marginLeft = tabSpaces_1 * exports.IndentStepInPixel + 'px';
55
+ }
56
+ }
57
+ }
58
+ exports.adjustListIndentation = adjustListIndentation;
59
+ /**
60
+ * @internal
61
+ */
62
+ function adjustTableIndentation(insertPoint, table) {
63
+ var paragraph = insertPoint.paragraph, marker = insertPoint.marker;
64
+ var indentationMargin = getTableIndentation(paragraph);
65
+ if (indentationMargin) {
66
+ insertPoint.paragraph.segments = [marker];
67
+ if (insertPoint.paragraph.format.direction == 'rtl') {
68
+ table.format.marginRight = indentationMargin * exports.IndentStepInPixel + 'px';
69
+ }
70
+ else {
71
+ table.format.marginLeft = indentationMargin * exports.IndentStepInPixel + 'px';
72
+ }
73
+ }
74
+ }
75
+ exports.adjustTableIndentation = adjustTableIndentation;
76
+ var getTableIndentation = function (paragraph) {
77
+ var e_2, _a;
78
+ var tabsNumber = 0;
79
+ var segments = paragraph.segments;
80
+ var isEmptyLine = paragraph.segments.every(function (s) {
81
+ return (s.segmentType == 'Text' && s.text.trim().length == 0) ||
82
+ s.segmentType == 'SelectionMarker' ||
83
+ s.segmentType == 'Br';
84
+ });
85
+ if (!isEmptyLine) {
86
+ return;
87
+ }
88
+ var numberOfSegments = 0;
89
+ try {
90
+ for (var segments_1 = (0, tslib_1.__values)(segments), segments_1_1 = segments_1.next(); !segments_1_1.done; segments_1_1 = segments_1.next()) {
91
+ var seg = segments_1_1.value;
92
+ if (seg.segmentType === 'Text') {
93
+ tabsNumber = tabsNumber + countTabsSpaces(seg.text);
94
+ numberOfSegments++;
95
+ }
96
+ else {
97
+ break;
98
+ }
99
+ }
100
+ }
101
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
102
+ finally {
103
+ try {
104
+ if (segments_1_1 && !segments_1_1.done && (_a = segments_1.return)) _a.call(segments_1);
105
+ }
106
+ finally { if (e_2) throw e_2.error; }
107
+ }
108
+ // Text segments must be >= (total segments - 2) to apply margin.
109
+ // If not, the selection marker is likely between texts segment, so skip margin adjustment.
110
+ if (segments.length - 2 <= numberOfSegments) {
111
+ return tabsNumber;
112
+ }
113
+ return;
114
+ };
115
+ //# sourceMappingURL=adjustIndentation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adjustIndentation.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/common/adjustIndentation.ts"],"names":[],"mappings":";;;;AAAA,2EAA4D;AAQ5D,IAAM,QAAQ,GAAG,QAAQ,CAAC;AAC1B,IAAM,aAAa,GAAG,QAAQ,CAAC;AAC/B,IAAM,gBAAgB,GAAG,QAAQ,CAAC;AAElC;;GAEG;AACU,QAAA,iBAAiB,GAAG,EAAE,CAAC;AAEpC,SAAS,eAAe,CAAC,IAAY;IACjC,IAAM,MAAM,GAAG,qBAAqB,CAAC,IAAI,CAAC,CAAC;IAC3C,IAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACzC,OAAO,SAAS,CAAC;AACrB,CAAC;AAED,SAAS,qBAAqB,CAAC,GAAW;;IACtC,IAAI,KAAK,GAAG,CAAC,CAAC;;QACd,KAAmB,IAAA,QAAA,sBAAA,GAAG,CAAA,wBAAA,yCAAE;YAAnB,IAAM,IAAI,gBAAA;YACX,IAAI,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,gBAAgB,IAAI,IAA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port type {\n ContentModelTable,\n InsertPoint,\n ShallowMutableContentModelListItem,\n ShallowMutableContentModelParagraph,\n} from 'roosterjs-content-model-types';\n\nconst EN_SPACE = '\\u2002';\nconst REGULAR_SPACE = '\\u0020';\nconst NON_BREAK_SPACES = '\\u00A0';\n\n/**\n * @internal\n */\nexport const IndentStepInPixel = 40;\n\nfunction countTabsSpaces(text: string) {\n const spaces = countSpacesBeforeText(text);\n const tabSpaces = Math.floor(spaces / 4);\n return tabSpaces;\n}\n\nfunction countSpacesBeforeText(str: string) {\n let count = 0;\n for (const char of str) {\n if (char === EN_SPACE || char === NON_BREAK_SPACES || char == REGULAR_SPACE) {\n count++;\n } else {\n break;\n }\n }\n return count;\n}\n\n/**\n * @internal\n */\nexport function adjustListIndentation(listItem: ShallowMutableContentModelListItem) {\n const block = listItem.blocks[0];\n if (\n block.blockType == 'Paragraph' &&\n block.segments.length > 0 &&\n block.segments[0].segmentType == 'Text'\n ) {\n const tabSpaces = countTabsSpaces(block.segments[0].text);\n\n if (tabSpaces > 0) {\n mutateSegment(block, block.segments[0], textSegment => {\n textSegment.text = textSegment.text.substring(tabSpaces * 4);\n });\n listItem.levels[0].format.marginLeft = tabSpaces * IndentStepInPixel + 'px';\n }\n }\n}\n\n/**\n * @internal\n */\nexport function adjustTableIndentation(insertPoint: InsertPoint, table: ContentModelTable) {\n const { paragraph, marker } = insertPoint;\n const indentationMargin = getTableIndentation(paragraph);\n\n if (indentationMargin) {\n insertPoint.paragraph.segments = [marker];\n if (insertPoint.paragraph.format.direction == 'rtl') {\n table.format.marginRight = indentationMargin * IndentStepInPixel + 'px';\n } else {\n table.format.marginLeft = indentationMargin * IndentStepInPixel + 'px';\n }\n }\n}\n\nconst getTableIndentation = (paragraph: ShallowMutableContentModelParagraph) => {\n let tabsNumber = 0;\n const segments = paragraph.segments;\n\n const isEmptyLine = paragraph.segments.every(\n s =>\n (s.segmentType == 'Text' && s.text.trim().length == 0) ||\n s.segmentType == 'SelectionMarker' ||\n s.segmentType == 'Br'\n );\n\n if (!isEmptyLine) {\n return;\n }\n\n let numberOfSegments = 0;\n for (const seg of segments) {\n if (seg.segmentType === 'Text') {\n tabsNumber = tabsNumber + countTabsSpaces(seg.text);\n numberOfSegments++;\n } else {\n break;\n }\n }\n\n // Text segments must be >= (total segments - 2) to apply margin.\n // If not, the selection marker is likely between texts segment, so skip margin adjustment.\n if (segments.length - 2 <= numberOfSegments) {\n return tabsNumber;\n }\n\n return;\n};\n"]}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setListType = void 0;
4
+ var adjustIndentation_1 = require("../common/adjustIndentation");
4
5
  var splitSelectedParagraphByBr_1 = require("../block/splitSelectedParagraphByBr");
5
6
  var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
6
7
  /**
@@ -29,6 +30,11 @@ function setListType(model, listType, removeMargins) {
29
30
  var level = block.levels.pop();
30
31
  if (!alreadyInExpectedType && level) {
31
32
  level.listType = listType;
33
+ (0, roosterjs_content_model_dom_1.updateListMetadata)(level, function (metadata) {
34
+ return Object.assign({}, metadata, {
35
+ applyListStyleFromLevel: true,
36
+ });
37
+ });
32
38
  block.levels.push(level);
33
39
  }
34
40
  else if (block.blocks.length == 1) {
@@ -72,6 +78,7 @@ function setListType(model, listType, removeMargins) {
72
78
  }
73
79
  var mutableBlock = (0, roosterjs_content_model_dom_1.mutateBlock)(block);
74
80
  newListItem.blocks.push(mutableBlock);
81
+ (0, adjustIndentation_1.adjustListIndentation)(newListItem);
75
82
  (0, roosterjs_content_model_dom_1.copyFormat)(newListItem.format, mutableBlock.format, roosterjs_content_model_dom_1.ListFormatsToMove, true /*deleteOriginalFormat*/);
76
83
  (0, roosterjs_content_model_dom_1.copyFormat)(newListItem.format, mutableBlock.format, roosterjs_content_model_dom_1.ListFormatsToKeep);
77
84
  (0, roosterjs_content_model_dom_1.mutateBlock)(parent).blocks.splice(index, 1, newListItem);
@@ -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,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,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,KAAK,CAAC,CAAC;aAC5B;iBAAM,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,EAAE;gBACj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AAuB,EAAE,IAAI;6BAChC,CAAC;wBAFF,CAEE,CACL,CAAC;qBACL;iBACJ;qBAAM;oBACH,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;oBACF,iBAAiB,GAAG,EAAE,CAAC;iBAC1B;aACJ;SACJ;IACL,CAAC,CAAC,CAAC;IAEH,IAAA,mDAAqB,EAAC,KAAK,CAAC,CAAC;IAE7B,OAAO,oBAAoB,CAAC,MAAM,GAAG,CAAC,CAAC;AAC3C,CAAC;AAlHD,kCAkHC;AAED,SAAS,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 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
+ {"version":3,"file":"setListType.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/list/setListType.ts"],"names":[],"mappings":";;;AAAA,iEAAoE;AACpE,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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AAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,GAAG,KAAK,CAAC,EAAtD,CAAsD,CAC9D,CAAC;oBACF,iBAAiB,GAAG,EAAE,CAAC;iBAC1B;aACJ;SACJ;IACL,CAAC,CAAC,CAAC;IAEH,IAAA,mDAAqB,EAAC,KAAK,CAAC,CAAC;IAE7B,OAAO,oBAAoB,CAAC,MAAM,GAAG,CAAC,CAAC;AAC3C,CAAC;AA3HD,kCA2HC;AAED,SAAS,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 { 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"]}
@@ -2,6 +2,6 @@ import type { IEditor } from 'roosterjs-content-model-types';
2
2
  /**
3
3
  * Set text direction of selected paragraphs (Left to right or Right to left)
4
4
  * @param editor The editor to set alignment
5
- * @param direction Direction value: ltr (Left to right) or rtl (Right to left)
5
+ * @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)
6
6
  */
7
- export declare function setDirection(editor: IEditor, direction: 'ltr' | 'rtl'): void;
7
+ export declare function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto'): void;
@@ -5,7 +5,7 @@ var setModelDirection_1 = require("../../modelApi/block/setModelDirection");
5
5
  /**
6
6
  * Set text direction of selected paragraphs (Left to right or Right to left)
7
7
  * @param editor The editor to set alignment
8
- * @param direction Direction value: ltr (Left to right) or rtl (Right to left)
8
+ * @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)
9
9
  */
10
10
  function setDirection(editor, direction) {
11
11
  editor.focus();
@@ -1 +1 @@
1
- {"version":3,"file":"setDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setDirection.ts"],"names":[],"mappings":";;;AAAA,4EAA2E;AAG3E;;;;GAIG;AACH,SAAgB,YAAY,CAAC,MAAe,EAAE,SAAwB;IAClE,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CAAC,UAAA,KAAK,IAAI,OAAA,IAAA,qCAAiB,EAAC,KAAK,EAAE,SAAS,CAAC,EAAnC,CAAmC,EAAE;QACpE,OAAO,EAAE,cAAc;KAC1B,CAAC,CAAC;AACP,CAAC;AAND,oCAMC","sourcesContent":["import { setModelDirection } from '../../modelApi/block/setModelDirection';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Set text direction of selected paragraphs (Left to right or Right to left)\n * @param editor The editor to set alignment\n * @param direction Direction value: ltr (Left to right) or rtl (Right to left)\n */\nexport function setDirection(editor: IEditor, direction: 'ltr' | 'rtl') {\n editor.focus();\n\n editor.formatContentModel(model => setModelDirection(model, direction), {\n apiName: 'setDirection',\n });\n}\n"]}
1
+ {"version":3,"file":"setDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setDirection.ts"],"names":[],"mappings":";;;AAAA,4EAA2E;AAG3E;;;;GAIG;AACH,SAAgB,YAAY,CAAC,MAAe,EAAE,SAAiC;IAC3E,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CAAC,UAAA,KAAK,IAAI,OAAA,IAAA,qCAAiB,EAAC,KAAK,EAAE,SAAS,CAAC,EAAnC,CAAmC,EAAE;QACpE,OAAO,EAAE,cAAc;KAC1B,CAAC,CAAC;AACP,CAAC;AAND,oCAMC","sourcesContent":["import { setModelDirection } from '../../modelApi/block/setModelDirection';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Set text direction of selected paragraphs (Left to right or Right to left)\n * @param editor The editor to set alignment\n * @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)\n */\nexport function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto') {\n editor.focus();\n\n editor.formatContentModel(model => setModelDirection(model, direction), {\n apiName: 'setDirection',\n });\n}\n"]}
@@ -18,7 +18,7 @@ function getFormatState(editor, conflictSolution) {
18
18
  isDarkMode: editor.isDarkMode(),
19
19
  };
20
20
  editor.formatContentModel(function (model) {
21
- (0, roosterjs_content_model_dom_1.retrieveModelFormatState)(model, pendingFormat, result, conflictSolution, editor.getDOMHelper());
21
+ (0, roosterjs_content_model_dom_1.retrieveModelFormatState)(model, pendingFormat, result, conflictSolution, editor.getDOMHelper(), editor.isDarkMode(), editor.getColorManager());
22
22
  return false;
23
23
  }, undefined /*options*/, {
24
24
  processorOverride: {
@@ -1 +1 @@
1
- {"version":3,"file":"getFormatState.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/format/getFormatState.ts"],"names":[],"mappings":";;;AAAA,+FAA8F;AAC9F,2EAAuE;AAOvE;;;;GAIG;AACH,SAAgB,cAAc,CAC1B,MAAe,EACf,gBAAmD;IAAnD,iCAAA,EAAA,2BAAmD;IAEnD,IAAM,aAAa,GAAG,MAAM,CAAC,gBAAgB,EAAE,CAAC;IAChD,IAAM,OAAO,GAAG,MAAM,CAAC,mBAAmB,EAAE,CAAC;IAC7C,IAAM,MAAM,GAA4B;QACpC,OAAO,EAAE,OAAO,CAAC,aAAa,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACrD,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;QAC3B,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE;KAClC,CAAC;IAEF,MAAM,CAAC,kBAAkB,CACrB,UAAA,KAAK;QACD,IAAA,sDAAwB,EACpB,KAAK,EACL,aAAa,EACb,MAAM,EACN,gBAAgB,EAChB,MAAM,CAAC,YAAY,EAAE,CACxB,CAAC;QAEF,OAAO,KAAK,CAAC;IACjB,CAAC,EACD,SAAS,CAAC,WAAW,EACrB;QACI,iBAAiB,EAAE;YACf,KAAK,EAAE,uDAA0B;SACpC;QACD,eAAe,EAAE,IAAI;KACxB,CACJ,CAAC;IAEF,OAAO,MAAM,CAAC;AAClB,CAAC;AAlCD,wCAkCC","sourcesContent":["import { reducedModelChildProcessor } from '../../modelApi/common/reducedModelChildProcessor';\nimport { retrieveModelFormatState } from 'roosterjs-content-model-dom';\nimport type {\n IEditor,\n ContentModelFormatState,\n ConflictFormatSolution,\n} from 'roosterjs-content-model-types';\n\n/**\n * Get current format state\n * @param editor The editor to get format from\n * @param conflictSolution The strategy for handling format conflicts\n */\nexport function getFormatState(\n editor: IEditor,\n conflictSolution: ConflictFormatSolution = 'remove'\n): ContentModelFormatState {\n const pendingFormat = editor.getPendingFormat();\n const manager = editor.getSnapshotsManager();\n const result: ContentModelFormatState = {\n canUndo: manager.hasNewContent || manager.canMove(-1),\n canRedo: manager.canMove(1),\n isDarkMode: editor.isDarkMode(),\n };\n\n editor.formatContentModel(\n model => {\n retrieveModelFormatState(\n model,\n pendingFormat,\n result,\n conflictSolution,\n editor.getDOMHelper()\n );\n\n return false;\n },\n undefined /*options*/,\n {\n processorOverride: {\n child: reducedModelChildProcessor,\n },\n tryGetFromCache: true,\n }\n );\n\n return result;\n}\n"]}
1
+ {"version":3,"file":"getFormatState.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/format/getFormatState.ts"],"names":[],"mappings":";;;AAAA,+FAA8F;AAC9F,2EAAuE;AAOvE;;;;GAIG;AACH,SAAgB,cAAc,CAC1B,MAAe,EACf,gBAAmD;IAAnD,iCAAA,EAAA,2BAAmD;IAEnD,IAAM,aAAa,GAAG,MAAM,CAAC,gBAAgB,EAAE,CAAC;IAChD,IAAM,OAAO,GAAG,MAAM,CAAC,mBAAmB,EAAE,CAAC;IAC7C,IAAM,MAAM,GAA4B;QACpC,OAAO,EAAE,OAAO,CAAC,aAAa,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACrD,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;QAC3B,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE;KAClC,CAAC;IAEF,MAAM,CAAC,kBAAkB,CACrB,UAAA,KAAK;QACD,IAAA,sDAAwB,EACpB,KAAK,EACL,aAAa,EACb,MAAM,EACN,gBAAgB,EAChB,MAAM,CAAC,YAAY,EAAE,EACrB,MAAM,CAAC,UAAU,EAAE,EACnB,MAAM,CAAC,eAAe,EAAE,CAC3B,CAAC;QAEF,OAAO,KAAK,CAAC;IACjB,CAAC,EACD,SAAS,CAAC,WAAW,EACrB;QACI,iBAAiB,EAAE;YACf,KAAK,EAAE,uDAA0B;SACpC;QACD,eAAe,EAAE,IAAI;KACxB,CACJ,CAAC;IAEF,OAAO,MAAM,CAAC;AAClB,CAAC;AApCD,wCAoCC","sourcesContent":["import { reducedModelChildProcessor } from '../../modelApi/common/reducedModelChildProcessor';\nimport { retrieveModelFormatState } from 'roosterjs-content-model-dom';\nimport type {\n IEditor,\n ContentModelFormatState,\n ConflictFormatSolution,\n} from 'roosterjs-content-model-types';\n\n/**\n * Get current format state\n * @param editor The editor to get format from\n * @param conflictSolution The strategy for handling format conflicts\n */\nexport function getFormatState(\n editor: IEditor,\n conflictSolution: ConflictFormatSolution = 'remove'\n): ContentModelFormatState {\n const pendingFormat = editor.getPendingFormat();\n const manager = editor.getSnapshotsManager();\n const result: ContentModelFormatState = {\n canUndo: manager.hasNewContent || manager.canMove(-1),\n canRedo: manager.canMove(1),\n isDarkMode: editor.isDarkMode(),\n };\n\n editor.formatContentModel(\n model => {\n retrieveModelFormatState(\n model,\n pendingFormat,\n result,\n conflictSolution,\n editor.getDOMHelper(),\n editor.isDarkMode(),\n editor.getColorManager()\n );\n\n return false;\n },\n undefined /*options*/,\n {\n processorOverride: {\n child: reducedModelChildProcessor,\n },\n tryGetFromCache: true,\n }\n );\n\n return result;\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.insertTable = void 0;
4
+ var adjustIndentation_1 = require("../../modelApi/common/adjustIndentation");
4
5
  var createTableStructure_1 = require("../../modelApi/table/createTableStructure");
5
6
  var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
6
7
  /**
@@ -21,6 +22,7 @@ function insertTable(editor, columns, rows, format) {
21
22
  var doc = (0, roosterjs_content_model_dom_1.createContentModelDocument)();
22
23
  var table = (0, createTableStructure_1.createTableStructure)(doc, columns, rows);
23
24
  (0, roosterjs_content_model_dom_1.normalizeTable)(table, editor.getPendingFormat() || insertPosition.marker.format);
25
+ (0, adjustIndentation_1.adjustTableIndentation)(insertPosition, table);
24
26
  // Assign default vertical align
25
27
  format = format || { verticalAlign: 'top' };
26
28
  (0, roosterjs_content_model_dom_1.applyTableFormat)(table, format);
@@ -1 +1 @@
1
- {"version":3,"file":"insertTable.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/table/insertTable.ts"],"names":[],"mappings":";;;AAAA,kFAAiF;AACjF,2EAQqC;AAGrC;;;;;;;;GAQG;AACH,SAAgB,WAAW,CACvB,MAAe,EACf,OAAe,EACf,IAAY,EACZ,MAAqC;IAErC,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;;QACX,IAAM,cAAc,GAAG,IAAA,6CAAe,EAAC,KAAK,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC,WAAW,CAAC;QAEvE,IAAI,cAAc,EAAE;YAChB,IAAM,GAAG,GAAG,IAAA,wDAA0B,GAAE,CAAC;YACzC,IAAM,KAAK,GAAG,IAAA,2CAAoB,EAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;YAEvD,IAAA,4CAAc,EAAC,KAAK,EAAE,MAAM,CAAC,gBAAgB,EAAE,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YACjF,gCAAgC;YAChC,MAAM,GAAG,MAAM,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC;YAC5C,IAAA,8CAAgB,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAChC,IAAA,wCAAU,EAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE;gBAC5B,cAAc,gBAAA;gBACd,WAAW,EAAE,UAAU;aAC1B,CAAC,CAAC;YAEH,IAAM,UAAU,GAAG,MAAA,MAAA,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,0CAAE,KAAK,CAAC,CAAC,CAAC,0CAAE,MAAM,CAAC,CAAC,CAAC,CAAC;YAEtD,IAAI,CAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,SAAS,KAAI,WAAW,EAAE;gBACtC,IAAM,MAAM,GAAG,IAAA,mDAAqB,EAAC,MAAA,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,0CAAE,MAAM,CAAC,CAAC;gBACrE,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBACpC,IAAA,0CAAY,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC;aAC/B;YAED,OAAO,IAAI,CAAC;SACf;aAAM;YACH,OAAO,KAAK,CAAC;SAChB;IACL,CAAC,EACD;QACI,OAAO,EAAE,aAAa;KACzB,CACJ,CAAC;AACN,CAAC;AA1CD,kCA0CC","sourcesContent":["import { createTableStructure } from '../../modelApi/table/createTableStructure';\nimport {\n createContentModelDocument,\n createSelectionMarker,\n applyTableFormat,\n deleteSelection,\n mergeModel,\n normalizeTable,\n setSelection,\n} from 'roosterjs-content-model-dom';\nimport type { IEditor, TableMetadataFormat } from 'roosterjs-content-model-types';\n\n/**\n * Insert table into editor at current selection\n * @param editor The editor instance\n * @param columns Number of columns in table, it also controls the default table cell width:\n * if columns &lt;= 4, width = 120px; if columns &lt;= 6, width = 100px; else width = 70px\n * @param rows Number of rows in table\n * @param format (Optional) The table format. If not passed, the default format will be applied:\n * background color: #FFF; border color: #ABABAB\n */\nexport function insertTable(\n editor: IEditor,\n columns: number,\n rows: number,\n format?: Partial<TableMetadataFormat>\n) {\n editor.focus();\n\n editor.formatContentModel(\n (model, context) => {\n const insertPosition = deleteSelection(model, [], context).insertPoint;\n\n if (insertPosition) {\n const doc = createContentModelDocument();\n const table = createTableStructure(doc, columns, rows);\n\n normalizeTable(table, editor.getPendingFormat() || insertPosition.marker.format);\n // Assign default vertical align\n format = format || { verticalAlign: 'top' };\n applyTableFormat(table, format);\n mergeModel(model, doc, context, {\n insertPosition,\n mergeFormat: 'mergeAll',\n });\n\n const firstBlock = table.rows[0]?.cells[0]?.blocks[0];\n\n if (firstBlock?.blockType == 'Paragraph') {\n const marker = createSelectionMarker(firstBlock.segments[0]?.format);\n firstBlock.segments.unshift(marker);\n setSelection(model, marker);\n }\n\n return true;\n } else {\n return false;\n }\n },\n {\n apiName: 'insertTable',\n }\n );\n}\n"]}
1
+ {"version":3,"file":"insertTable.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/table/insertTable.ts"],"names":[],"mappings":";;;AAAA,6EAAiF;AACjF,kFAAiF;AACjF,2EAQqC;AAGrC;;;;;;;;GAQG;AACH,SAAgB,WAAW,CACvB,MAAe,EACf,OAAe,EACf,IAAY,EACZ,MAAqC;IAErC,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;;QACX,IAAM,cAAc,GAAG,IAAA,6CAAe,EAAC,KAAK,EAAE,EAAE,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D,kCA6CC","sourcesContent":["import { adjustTableIndentation } from '../../modelApi/common/adjustIndentation';\nimport { createTableStructure } from '../../modelApi/table/createTableStructure';\nimport {\n createContentModelDocument,\n createSelectionMarker,\n applyTableFormat,\n deleteSelection,\n mergeModel,\n normalizeTable,\n setSelection,\n} from 'roosterjs-content-model-dom';\nimport type { IEditor, TableMetadataFormat } from 'roosterjs-content-model-types';\n\n/**\n * Insert table into editor at current selection\n * @param editor The editor instance\n * @param columns Number of columns in table, it also controls the default table cell width:\n * if columns &lt;= 4, width = 120px; if columns &lt;= 6, width = 100px; else width = 70px\n * @param rows Number of rows in table\n * @param format (Optional) The table format. If not passed, the default format will be applied:\n * background color: #FFF; border color: #ABABAB\n */\nexport function insertTable(\n editor: IEditor,\n columns: number,\n rows: number,\n format?: Partial<TableMetadataFormat>\n) {\n editor.focus();\n\n editor.formatContentModel(\n (model, context) => {\n const insertPosition = deleteSelection(model, [], context).insertPoint;\n\n if (insertPosition) {\n const doc = createContentModelDocument();\n const table = createTableStructure(doc, columns, rows);\n\n normalizeTable(table, editor.getPendingFormat() || insertPosition.marker.format);\n\n adjustTableIndentation(insertPosition, table);\n\n // Assign default vertical align\n format = format || { verticalAlign: 'top' };\n applyTableFormat(table, format);\n mergeModel(model, doc, context, {\n insertPosition,\n mergeFormat: 'mergeAll',\n });\n\n const firstBlock = table.rows[0]?.cells[0]?.blocks[0];\n\n if (firstBlock?.blockType == 'Paragraph') {\n const marker = createSelectionMarker(firstBlock.segments[0]?.format);\n firstBlock.segments.unshift(marker);\n setSelection(model, marker);\n }\n\n return true;\n } else {\n return false;\n }\n },\n {\n apiName: 'insertTable',\n }\n );\n}\n"]}
@@ -2,4 +2,4 @@ import type { ReadonlyContentModelDocument } from 'roosterjs-content-model-types
2
2
  /**
3
3
  * @internal
4
4
  */
5
- export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl'): boolean;
5
+ export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl' | 'auto'): boolean;
@@ -2,6 +2,11 @@ define(["require", "exports", "../list/findListItemsInSameThread", "./splitSelec
2
2
  "use strict";
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.setModelDirection = void 0;
5
+ // Regexes for character direction detection
6
+ // Strongly typed RTL character ranges. Referenced unicode's DerivedBidiClass.txt, excluding things in the 2 bit range.
7
+ var RTL_CHAR_REGEX = /[\u0590-\u05FF\u0600-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/g;
8
+ var URL_CHAR_REGEX = /http\S+|www\S+|https\S+|<a\s+(?:[^>]*?\s+)?href=(["']).*?\1.*?>.*?<\/a>/g;
9
+ var WHITESPACE_REGEX = /\s/g;
5
10
  /**
6
11
  * @internal
7
12
  */
@@ -10,18 +15,25 @@ define(["require", "exports", "../list/findListItemsInSameThread", "./splitSelec
10
15
  var paragraphOrListItemOrTable = (0, roosterjs_content_model_dom_1.getOperationalBlocks)(model, ['ListItem'], ['TableCell']);
11
16
  paragraphOrListItemOrTable.forEach(function (_a) {
12
17
  var block = _a.block;
18
+ var calcDirection;
19
+ if (direction === 'auto') {
20
+ calcDirection = determineTextDirection(block);
21
+ }
22
+ else {
23
+ calcDirection = direction;
24
+ }
13
25
  if ((0, roosterjs_content_model_dom_1.isBlockGroupOfType)(block, 'ListItem')) {
14
26
  var items = (0, findListItemsInSameThread_1.findListItemsInSameThread)(model, block);
15
27
  items.forEach(function (readonlyItem) {
16
28
  var item = (0, roosterjs_content_model_dom_1.mutateBlock)(readonlyItem);
17
29
  item.levels.forEach(function (level) {
18
- level.format.direction = direction;
30
+ level.format.direction = calcDirection;
19
31
  });
20
- item.blocks.forEach(function (block) { return internalSetDirection(block, direction); });
32
+ item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
21
33
  });
22
34
  }
23
35
  else if (block) {
24
- internalSetDirection(block, direction);
36
+ internalSetDirection(block, calcDirection);
25
37
  }
26
38
  });
27
39
  return paragraphOrListItemOrTable.length > 0;
@@ -68,5 +80,30 @@ define(["require", "exports", "../list/findListItemsInSameThread", "./splitSelec
68
80
  delete format[key];
69
81
  }
70
82
  }
83
+ // Designed to match browser's 'auto' detection, by scanning over the inner text until it hits a strong LTR/RTL character
84
+ function determineTextDirection(block) {
85
+ if (block.blockType === 'Paragraph') {
86
+ var findTextSegements = block.segments.filter(function (seg) { return seg.segmentType === 'Text'; });
87
+ var innerText = findTextSegements.length > 0
88
+ ? findTextSegements.reduce(function (prev, seg) { return prev + seg.text; }, '')
89
+ : undefined;
90
+ if (!!innerText) {
91
+ // Remove links
92
+ innerText = innerText.replace(URL_CHAR_REGEX, '');
93
+ // Remove whitespace
94
+ innerText = innerText.replace(WHITESPACE_REGEX, '');
95
+ var rtlMatches = innerText.match(RTL_CHAR_REGEX);
96
+ var rtlCount = rtlMatches ? rtlMatches.length : 0;
97
+ var ltrCount = innerText.length - rtlCount;
98
+ return rtlCount > ltrCount ? 'rtl' : 'ltr';
99
+ }
100
+ else {
101
+ return 'ltr'; // Default to LTR if no text is found
102
+ }
103
+ }
104
+ else {
105
+ return 'ltr';
106
+ }
107
+ }
71
108
  });
72
109
  //# sourceMappingURL=setModelDirection.js.map