roosterjs-content-model-api 9.44.0 → 9.45.1

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 (31) hide show
  1. package/lib/modelApi/block/setModelDirection.js +4 -0
  2. package/lib/modelApi/block/setModelDirection.js.map +1 -1
  3. package/lib/modelApi/table/alignTableCell.js +14 -4
  4. package/lib/modelApi/table/alignTableCell.js.map +1 -1
  5. package/lib/modelApi/table/createTableStructure.d.ts +2 -2
  6. package/lib/modelApi/table/createTableStructure.js +2 -2
  7. package/lib/modelApi/table/createTableStructure.js.map +1 -1
  8. package/lib/publicApi/table/insertTable.d.ts +3 -2
  9. package/lib/publicApi/table/insertTable.js +26 -2
  10. package/lib/publicApi/table/insertTable.js.map +1 -1
  11. package/lib-amd/modelApi/block/setModelDirection.js +4 -0
  12. package/lib-amd/modelApi/block/setModelDirection.js.map +1 -1
  13. package/lib-amd/modelApi/table/alignTableCell.js +14 -4
  14. package/lib-amd/modelApi/table/alignTableCell.js.map +1 -1
  15. package/lib-amd/modelApi/table/createTableStructure.d.ts +2 -2
  16. package/lib-amd/modelApi/table/createTableStructure.js +2 -2
  17. package/lib-amd/modelApi/table/createTableStructure.js.map +1 -1
  18. package/lib-amd/publicApi/table/insertTable.d.ts +3 -2
  19. package/lib-amd/publicApi/table/insertTable.js +26 -2
  20. package/lib-amd/publicApi/table/insertTable.js.map +1 -1
  21. package/lib-mjs/modelApi/block/setModelDirection.js +4 -0
  22. package/lib-mjs/modelApi/block/setModelDirection.js.map +1 -1
  23. package/lib-mjs/modelApi/table/alignTableCell.js +14 -4
  24. package/lib-mjs/modelApi/table/alignTableCell.js.map +1 -1
  25. package/lib-mjs/modelApi/table/createTableStructure.d.ts +2 -2
  26. package/lib-mjs/modelApi/table/createTableStructure.js +2 -2
  27. package/lib-mjs/modelApi/table/createTableStructure.js.map +1 -1
  28. package/lib-mjs/publicApi/table/insertTable.d.ts +3 -2
  29. package/lib-mjs/publicApi/table/insertTable.js +28 -4
  30. package/lib-mjs/publicApi/table/insertTable.js.map +1 -1
  31. package/package.json +4 -4
@@ -31,6 +31,10 @@ function setModelDirection(model, direction) {
31
31
  item.levels.forEach(function (level) {
32
32
  level.format.direction = calcDirection;
33
33
  });
34
+ // We already set direction on levels, no need to keep it on list item level
35
+ delete item.format.direction;
36
+ // Remove textAlign to let it be calculated based on direction change
37
+ delete item.format.textAlign;
34
38
  item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
35
39
  });
36
40
  }
@@ -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;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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AAsC,OAAA,GAAG,CAAC,WAAW,KAAK,MAAM,EAA1B,CAA0B,CACvE,CAAC;QACF,IAAI,SAAS,GACT,iBAAiB,CAAC,MAAM,GAAG,CAAC;YACxB,CAAC,CAAC,iBAAiB,CAAC,MAAM,CAAC,UAAC,IAAI,EAAE,GAAG,IAAK,OAAA,IAAI,GAAG,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"]}
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;AAWr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S,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 // We already set direction on levels, no need to keep it on list item level\n delete item.format.direction;\n\n // Remove textAlign to let it be calculated based on direction change\n delete item.format.textAlign;\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"]}
@@ -3,9 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.alignTableCellVertically = exports.alignTableCellHorizontally = void 0;
4
4
  var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
5
5
  var TextAlignValueMap = {
6
- alignCellLeft: 'start',
7
- alignCellCenter: 'center',
8
- alignCellRight: 'end',
6
+ alignCellLeft: {
7
+ ltr: 'start',
8
+ rtl: 'end',
9
+ },
10
+ alignCellCenter: {
11
+ ltr: 'center',
12
+ rtl: 'center',
13
+ },
14
+ alignCellRight: {
15
+ ltr: 'end',
16
+ rtl: 'start',
17
+ },
9
18
  };
10
19
  var VerticalAlignValueMap = {
11
20
  alignCellTop: 'top',
@@ -17,7 +26,8 @@ var VerticalAlignValueMap = {
17
26
  */
18
27
  function alignTableCellHorizontally(table, operation) {
19
28
  alignTableCellInternal(table, function (cell) {
20
- cell.format.textAlign = TextAlignValueMap[operation];
29
+ cell.format.textAlign =
30
+ TextAlignValueMap[operation][cell.format.direction == 'rtl' ? 'rtl' : 'ltr'];
21
31
  });
22
32
  }
23
33
  exports.alignTableCellHorizontally = alignTableCellHorizontally;
@@ -1 +1 @@
1
- {"version":3,"file":"alignTableCell.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/alignTableCell.ts"],"names":[],"mappings":";;;AAAA,2EAIqC;AAQrC,IAAM,iBAAiB,GAGlB;IACD,aAAa,EAAE,OAAO;IACtB,eAAe,EAAE,QAAQ;IACzB,cAAc,EAAE,KAAK;CACxB,CAAC;AAEF,IAAM,qBAAqB,GAGtB;IACD,YAAY,EAAE,KAAK;IACnB,eAAe,EAAE,QAAQ;IACzB,eAAe,EAAE,QAAQ;CAC5B,CAAC;AAEF;;GAEG;AACH,SAAgB,0BAA0B,CACtC,KAAsC,EACtC,SAA4C;IAE5C,sBAAsB,CAAC,KAAK,EAAE,UAAA,IAAI;QAC9B,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,iBAAiB,CAAC,SAAS,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;AACP,CAAC;AAPD,gEAOC;AAED;;GAEG;AACH,SAAgB,wBAAwB,CACpC,KAAsC,EACtC,SAA0C;IAE1C,sBAAsB,CAAC,KAAK,EAAE,UAAA,IAAI;QAC9B,IAAI,CAAC,MAAM,CAAC,aAAa,GAAG,qBAAqB,CAAC,SAAS,CAAC,CAAC;QAE7D,IAAA,qDAAuB,EAAC,IAAI,EAAE,UAAA,QAAQ;YAClC,QAAQ,GAAG,QAAQ,IAAI,EAAE,CAAC;YAC1B,QAAQ,CAAC,cAAc,GAAG,IAAI,CAAC;YAC/B,OAAO,QAAQ,CAAC;QACpB,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC;AAbD,4DAaC;AAED,SAAS,sBAAsB,CAC3B,KAAsC,EACtC,QAA6D;;IAE7D,IAAM,GAAG,GAAG,IAAA,8CAAgB,EAAC,KAAK,CAAC,CAAC;IAEpC,IAAI,GAAG,EAAE;QACL,KAAK,IAAI,QAAQ,GAAG,GAAG,CAAC,QAAQ,EAAE,QAAQ,IAAI,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE;YACnE,KAAK,IAAI,QAAQ,GAAG,GAAG,CAAC,WAAW,EAAE,QAAQ,IAAI,GAAG,CAAC,UAAU,EAAE,QAAQ,EAAE,EAAE;gBACzE,IAAM,IAAI,GAAG,MAAA,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,0CAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;gBACnD,IAAM,MAAM,GAAG,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,MAAM,CAAC;gBAE5B,IAAI,MAAM,EAAE;oBACR,QAAQ,CAAC,IAAA,yCAAW,EAAC,IAAI,CAAC,CAAC,CAAC;oBAE5B,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAA,KAAK;wBACrB,IAAI,KAAK,CAAC,SAAS,KAAK,WAAW,IAAI,KAAK,CAAC,MAAM,CAAC,SAAS,EAAE;4BAC3D,OAAO,IAAA,yCAAW,EAAC,KAAK,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;yBAC9C;oBACL,CAAC,CAAC,CAAC;iBACN;aACJ;SACJ;KACJ;AACL,CAAC","sourcesContent":["import {\n getSelectedCells,\n mutateBlock,\n updateTableCellMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ShallowMutableContentModelTable,\n ShallowMutableContentModelTableCell,\n TableCellHorizontalAlignOperation,\n TableCellVerticalAlignOperation,\n} from 'roosterjs-content-model-types';\n\nconst TextAlignValueMap: Partial<Record<\n TableCellHorizontalAlignOperation,\n 'start' | 'center' | 'end'\n>> = {\n alignCellLeft: 'start',\n alignCellCenter: 'center',\n alignCellRight: 'end',\n};\n\nconst VerticalAlignValueMap: Partial<Record<\n TableCellVerticalAlignOperation,\n 'top' | 'middle' | 'bottom'\n>> = {\n alignCellTop: 'top',\n alignCellMiddle: 'middle',\n alignCellBottom: 'bottom',\n};\n\n/**\n * @internal\n */\nexport function alignTableCellHorizontally(\n table: ShallowMutableContentModelTable,\n operation: TableCellHorizontalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.textAlign = TextAlignValueMap[operation];\n });\n}\n\n/**\n * @internal\n */\nexport function alignTableCellVertically(\n table: ShallowMutableContentModelTable,\n operation: TableCellVerticalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.verticalAlign = VerticalAlignValueMap[operation];\n\n updateTableCellMetadata(cell, metadata => {\n metadata = metadata || {};\n metadata.vAlignOverride = true;\n return metadata;\n });\n });\n}\n\nfunction alignTableCellInternal(\n table: ShallowMutableContentModelTable,\n callback: (cell: ShallowMutableContentModelTableCell) => void\n) {\n const sel = getSelectedCells(table);\n\n if (sel) {\n for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {\n for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {\n const cell = table.rows[rowIndex]?.cells[colIndex];\n const format = cell?.format;\n\n if (format) {\n callback(mutateBlock(cell));\n\n cell.blocks.forEach(block => {\n if (block.blockType === 'Paragraph' && block.format.textAlign) {\n delete mutateBlock(block).format.textAlign;\n }\n });\n }\n }\n }\n }\n}\n"]}
1
+ {"version":3,"file":"alignTableCell.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/alignTableCell.ts"],"names":[],"mappings":";;;AAAA,2EAIqC;AAQrC,IAAM,iBAAiB,GAGnB;IACA,aAAa,EAAE;QACX,GAAG,EAAE,OAAO;QACZ,GAAG,EAAE,KAAK;KACb;IACD,eAAe,EAAE;QACb,GAAG,EAAE,QAAQ;QACb,GAAG,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QAAQ,GAAG,GAAG,CAAC,WAAW,EAAE,QAAQ,IAAI,GAAG,CAAC,UAAU,EAAE,QAAQ,EAAE,EAAE;gBACzE,IAAM,IAAI,GAAG,MAAA,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,0CAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;gBACnD,IAAM,MAAM,GAAG,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,MAAM,CAAC;gBAE5B,IAAI,MAAM,EAAE;oBACR,QAAQ,CAAC,IAAA,yCAAW,EAAC,IAAI,CAAC,CAAC,CAAC;oBAE5B,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAA,KAAK;wBACrB,IAAI,KAAK,CAAC,SAAS,KAAK,WAAW,IAAI,KAAK,CAAC,MAAM,CAAC,SAAS,EAAE;4BAC3D,OAAO,IAAA,yCAAW,EAAC,KAAK,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;yBAC9C;oBACL,CAAC,CAAC,CAAC;iBACN;aACJ;SACJ;KACJ;AACL,CAAC","sourcesContent":["import {\n getSelectedCells,\n mutateBlock,\n updateTableCellMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ShallowMutableContentModelTable,\n ShallowMutableContentModelTableCell,\n TableCellHorizontalAlignOperation,\n TableCellVerticalAlignOperation,\n} from 'roosterjs-content-model-types';\n\nconst TextAlignValueMap: Record<\n TableCellHorizontalAlignOperation,\n Partial<Record<'ltr' | 'rtl', 'start' | 'center' | 'end'>>\n> = {\n alignCellLeft: {\n ltr: 'start',\n rtl: 'end',\n },\n alignCellCenter: {\n ltr: 'center',\n rtl: 'center',\n },\n alignCellRight: {\n ltr: 'end',\n rtl: 'start',\n },\n};\n\nconst VerticalAlignValueMap: Partial<Record<\n TableCellVerticalAlignOperation,\n 'top' | 'middle' | 'bottom'\n>> = {\n alignCellTop: 'top',\n alignCellMiddle: 'middle',\n alignCellBottom: 'bottom',\n};\n\n/**\n * @internal\n */\nexport function alignTableCellHorizontally(\n table: ShallowMutableContentModelTable,\n operation: TableCellHorizontalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.textAlign =\n TextAlignValueMap[operation][cell.format.direction == 'rtl' ? 'rtl' : 'ltr'];\n });\n}\n\n/**\n * @internal\n */\nexport function alignTableCellVertically(\n table: ShallowMutableContentModelTable,\n operation: TableCellVerticalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.verticalAlign = VerticalAlignValueMap[operation];\n\n updateTableCellMetadata(cell, metadata => {\n metadata = metadata || {};\n metadata.vAlignOverride = true;\n return metadata;\n });\n });\n}\n\nfunction alignTableCellInternal(\n table: ShallowMutableContentModelTable,\n callback: (cell: ShallowMutableContentModelTableCell) => void\n) {\n const sel = getSelectedCells(table);\n\n if (sel) {\n for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {\n for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {\n const cell = table.rows[rowIndex]?.cells[colIndex];\n const format = cell?.format;\n\n if (format) {\n callback(mutateBlock(cell));\n\n cell.blocks.forEach(block => {\n if (block.blockType === 'Paragraph' && block.format.textAlign) {\n delete mutateBlock(block).format.textAlign;\n }\n });\n }\n }\n }\n }\n}\n"]}
@@ -1,5 +1,5 @@
1
- import type { ContentModelBlockGroup, ContentModelTable } from 'roosterjs-content-model-types';
1
+ import type { ContentModelBlockGroup, ContentModelTable, ContentModelTableCellFormat } from 'roosterjs-content-model-types';
2
2
  /**
3
3
  * @internal
4
4
  */
5
- export declare function createTableStructure(parent: ContentModelBlockGroup, columns: number, rows: number): ContentModelTable;
5
+ export declare function createTableStructure(parent: ContentModelBlockGroup, columns: number, rows: number, cellFormat?: ContentModelTableCellFormat): ContentModelTable;
@@ -5,12 +5,12 @@ var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
5
5
  /**
6
6
  * @internal
7
7
  */
8
- function createTableStructure(parent, columns, rows) {
8
+ function createTableStructure(parent, columns, rows, cellFormat) {
9
9
  var table = (0, roosterjs_content_model_dom_1.createTable)(rows);
10
10
  (0, roosterjs_content_model_dom_1.addBlock)(parent, table);
11
11
  table.rows.forEach(function (row) {
12
12
  for (var i = 0; i < columns; i++) {
13
- var cell = (0, roosterjs_content_model_dom_1.createTableCell)();
13
+ var cell = (0, roosterjs_content_model_dom_1.createTableCell)(undefined /*spanLeftOrColSpan */, undefined /*spanAboveOrRowSpan */, undefined /* isHeader */, cellFormat);
14
14
  row.cells.push(cell);
15
15
  }
16
16
  });
@@ -1 +1 @@
1
- {"version":3,"file":"createTableStructure.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/createTableStructure.ts"],"names":[],"mappings":";;;AAAA,2EAAqF;AAGrF;;GAEG;AACH,SAAgB,oBAAoB,CAChC,MAA8B,EAC9B,OAAe,EACf,IAAY;IAEZ,IAAM,KAAK,GAAG,IAAA,yCAAW,EAAC,IAAI,CAAC,CAAC;IAEhC,IAAA,sCAAQ,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAExB,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAA,GAAG;QAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;YAC9B,IAAM,IAAI,GAAG,IAAA,6CAAe,GAAE,CAAC;YAE/B,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACxB;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACjB,CAAC;AAlBD,oDAkBC","sourcesContent":["import { addBlock, createTable, createTableCell } from 'roosterjs-content-model-dom';\nimport type { ContentModelBlockGroup, ContentModelTable } from 'roosterjs-content-model-types';\n\n/**\n * @internal\n */\nexport function createTableStructure(\n parent: ContentModelBlockGroup,\n columns: number,\n rows: number\n): ContentModelTable {\n const table = createTable(rows);\n\n addBlock(parent, table);\n\n table.rows.forEach(row => {\n for (let i = 0; i < columns; i++) {\n const cell = createTableCell();\n\n row.cells.push(cell);\n }\n });\n\n return table;\n}\n"]}
1
+ {"version":3,"file":"createTableStructure.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/createTableStructure.ts"],"names":[],"mappings":";;;AAAA,2EAAqF;AAOrF;;GAEG;AACH,SAAgB,oBAAoB,CAChC,MAA8B,EAC9B,OAAe,EACf,IAAY,EACZ,UAAwC;IAExC,IAAM,KAAK,GAAG,IAAA,yCAAW,EAAC,IAAI,CAAC,CAAC;IAEhC,IAAA,sCAAQ,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAExB,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAA,GAAG;QAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;YAC9B,IAAM,IAAI,GAAG,IAAA,6CAAe,EACxB,SAAS,CAAC,sBAAsB,EAChC,SAAS,CAAC,uBAAuB,EACjC,SAAS,CAAC,cAAc,EACxB,UAAU,CACb,CAAC;YAEF,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACxB;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACjB,CAAC;AAxBD,oDAwBC","sourcesContent":["import { addBlock, createTable, createTableCell } from 'roosterjs-content-model-dom';\nimport type {\n ContentModelBlockGroup,\n ContentModelTable,\n ContentModelTableCellFormat,\n} from 'roosterjs-content-model-types';\n\n/**\n * @internal\n */\nexport function createTableStructure(\n parent: ContentModelBlockGroup,\n columns: number,\n rows: number,\n cellFormat?: ContentModelTableCellFormat\n): ContentModelTable {\n const table = createTable(rows);\n\n addBlock(parent, table);\n\n table.rows.forEach(row => {\n for (let i = 0; i < columns; i++) {\n const cell = createTableCell(\n undefined /*spanLeftOrColSpan */,\n undefined /*spanAboveOrRowSpan */,\n undefined /* isHeader */,\n cellFormat\n );\n\n row.cells.push(cell);\n }\n });\n\n return table;\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { ContentModelTableFormat, IEditor, TableMetadataFormat } from 'roosterjs-content-model-types';
1
+ import type { ContentModelTableFormat, IEditor, TableMetadataFormat, ContentModelTableCellFormat } from 'roosterjs-content-model-types';
2
2
  /**
3
3
  * Insert table into editor at current selection
4
4
  * @param editor The editor instance
@@ -7,5 +7,6 @@ import type { ContentModelTableFormat, IEditor, TableMetadataFormat } from 'roos
7
7
  * @param rows Number of rows in table
8
8
  * @param tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB
9
9
  * @param format (Optional) The table format used for style attributes
10
+ * @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat
10
11
  */
11
- export declare function insertTable(editor: IEditor, columns: number, rows: number, tableMetadataFormat?: Partial<TableMetadataFormat>, format?: ContentModelTableFormat): void;
12
+ export declare function insertTable(editor: IEditor, columns: number, rows: number, tableMetadataFormat?: Partial<TableMetadataFormat>, format?: ContentModelTableFormat, customCellFormat?: ContentModelTableCellFormat): void;
@@ -13,19 +13,21 @@ var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
13
13
  * @param rows Number of rows in table
14
14
  * @param tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB
15
15
  * @param format (Optional) The table format used for style attributes
16
+ * @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat
16
17
  */
17
- function insertTable(editor, columns, rows, tableMetadataFormat, format) {
18
+ function insertTable(editor, columns, rows, tableMetadataFormat, format, customCellFormat) {
18
19
  editor.focus();
19
20
  editor.formatContentModel(function (model, context) {
20
21
  var _a, _b, _c;
21
22
  var insertPosition = (0, roosterjs_content_model_dom_1.deleteSelection)(model, [], context).insertPoint;
22
23
  if (insertPosition) {
23
24
  var doc = (0, roosterjs_content_model_dom_1.createContentModelDocument)();
24
- var table = (0, createTableStructure_1.createTableStructure)(doc, columns, rows);
25
+ var table = (0, createTableStructure_1.createTableStructure)(doc, columns, rows, customCellFormat);
25
26
  if (format) {
26
27
  table.format = (0, tslib_1.__assign)({}, format);
27
28
  }
28
29
  (0, roosterjs_content_model_dom_1.normalizeTable)(table, editor.getPendingFormat() || insertPosition.marker.format);
30
+ initCellWidth(table);
29
31
  (0, adjustIndentation_1.adjustTableIndentation)(insertPosition, table);
30
32
  // Assign default vertical align
31
33
  tableMetadataFormat = tableMetadataFormat || { verticalAlign: 'top' };
@@ -50,4 +52,26 @@ function insertTable(editor, columns, rows, tableMetadataFormat, format) {
50
52
  });
51
53
  }
52
54
  exports.insertTable = insertTable;
55
+ function initCellWidth(table) {
56
+ var columns = Math.max.apply(Math, (0, tslib_1.__spreadArray)([], (0, tslib_1.__read)(table.rows.map(function (row) { return row.cells.length; })), false));
57
+ for (var i = 0; i < columns; i++) {
58
+ if (table.widths[i] === undefined) {
59
+ table.widths[i] = getTableCellWidth(columns);
60
+ }
61
+ else if (table.widths[i] < roosterjs_content_model_dom_1.MIN_ALLOWED_TABLE_CELL_WIDTH) {
62
+ table.widths[i] = roosterjs_content_model_dom_1.MIN_ALLOWED_TABLE_CELL_WIDTH;
63
+ }
64
+ }
65
+ }
66
+ function getTableCellWidth(columns) {
67
+ if (columns <= 4) {
68
+ return 120;
69
+ }
70
+ else if (columns <= 6) {
71
+ return 100;
72
+ }
73
+ else {
74
+ return 70;
75
+ }
76
+ }
53
77
  //# sourceMappingURL=insertTable.js.map
@@ -1 +1 @@
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;AAOrC;;;;;;;;GAQG;AACH,SAAgB,WAAW,CACvB,MAAe,EACf,OAAe,EACf,IAAY,EACZ,mBAAkD,EAClD,MAAgC;IAEhC,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,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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;AAjDD,kCAiDC","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 {\n ContentModelTableFormat,\n IEditor,\n TableMetadataFormat,\n} 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 tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB\n * @param format (Optional) The table format used for style attributes\n */\nexport function insertTable(\n editor: IEditor,\n columns: number,\n rows: number,\n tableMetadataFormat?: Partial<TableMetadataFormat>,\n format?: ContentModelTableFormat\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 if (format) {\n table.format = { ...format };\n }\n\n normalizeTable(table, editor.getPendingFormat() || insertPosition.marker.format);\n\n adjustTableIndentation(insertPosition, table);\n\n // Assign default vertical align\n tableMetadataFormat = tableMetadataFormat || { verticalAlign: 'top' };\n applyTableFormat(table, tableMetadataFormat);\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,2EASqC;AASrC;;;;;;;;;GASG;AACH,SAAgB,WAAW,CACvB,MAAe,EACf,OAAe,EACf,IAAY,EACZ,mBAAkD,EAClD,MAAgC,EAChC,gBAA8C;IAE9C,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,EAAE,gBAAgB,CAAC,CAAC;YACzE,IAAI,MAAM,EAAE;gBACR,KAAK,CAAC,MAAM,6BAAQ,MAAM,CAAE,CAAC;aAChC;YAED,IAAA,4CAAc,EAAC,KAAK,EAAE,MAAM,CAAC,gBAAgB,EAAE,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YACjF,aAAa,CAAC,KAAK,CAAC,CAAC;YAErB,IAAA,0CAAsB,EAAC,cAAc,EAAE,KAAK,CAAC,CAAC;YAE9C,gCAAgC;YAChC,mBAAmB,GAAG,mBAAmB,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC;YACtE,IAAA,8CAAgB,EAAC,KAAK,EAAE,mBAAmB,CAAC,CAAC;YAC7C,IAAA,wCAAU,EAAC,KAAK,EAAE,GA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AAC,MAAM,CAAC,CAAC,CAAC,GAAG,0DAA4B,CAAC;SAClD;KACJ;AACL,CAAC;AAED,SAAS,iBAAiB,CAAC,OAAe;IACtC,IAAI,OAAO,IAAI,CAAC,EAAE;QACd,OAAO,GAAG,CAAC;KACd;SAAM,IAAI,OAAO,IAAI,CAAC,EAAE;QACrB,OAAO,GAAG,CAAC;KACd;SAAM;QACH,OAAO,EAAE,CAAC;KACb;AACL,CAAC","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 MIN_ALLOWED_TABLE_CELL_WIDTH,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelTable,\n ContentModelTableFormat,\n IEditor,\n TableMetadataFormat,\n ContentModelTableCellFormat,\n} 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 tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB\n * @param format (Optional) The table format used for style attributes\n * @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat\n */\nexport function insertTable(\n editor: IEditor,\n columns: number,\n rows: number,\n tableMetadataFormat?: Partial<TableMetadataFormat>,\n format?: ContentModelTableFormat,\n customCellFormat?: ContentModelTableCellFormat\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, customCellFormat);\n if (format) {\n table.format = { ...format };\n }\n\n normalizeTable(table, editor.getPendingFormat() || insertPosition.marker.format);\n initCellWidth(table);\n\n adjustTableIndentation(insertPosition, table);\n\n // Assign default vertical align\n tableMetadataFormat = tableMetadataFormat || { verticalAlign: 'top' };\n applyTableFormat(table, tableMetadataFormat);\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\nfunction initCellWidth(table: ContentModelTable) {\n const columns = Math.max(...table.rows.map(row => row.cells.length));\n\n for (let i = 0; i < columns; i++) {\n if (table.widths[i] === undefined) {\n table.widths[i] = getTableCellWidth(columns);\n } else if (table.widths[i] < MIN_ALLOWED_TABLE_CELL_WIDTH) {\n table.widths[i] = MIN_ALLOWED_TABLE_CELL_WIDTH;\n }\n }\n}\n\nfunction getTableCellWidth(columns: number): number {\n if (columns <= 4) {\n return 120;\n } else if (columns <= 6) {\n return 100;\n } else {\n return 70;\n }\n}\n"]}
@@ -29,6 +29,10 @@ define(["require", "exports", "../list/findListItemsInSameThread", "./splitSelec
29
29
  item.levels.forEach(function (level) {
30
30
  level.format.direction = calcDirection;
31
31
  });
32
+ // We already set direction on levels, no need to keep it on list item level
33
+ delete item.format.direction;
34
+ // Remove textAlign to let it be calculated based on direction change
35
+ delete item.format.textAlign;
32
36
  item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
33
37
  });
34
38
  }
@@ -1 +1 @@
1
- {"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelDirection.ts"],"names":[],"mappings":";;;;IAmBA,4CAA4C;IAC5C,uHAAuH;IACvH,IAAM,cAAc,GAAG,yDAAyD,CAAC;IACjF,IAAM,cAAc,GAAG,0EAA0E,CAAC;IAClG,IAAM,gBAA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G,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;gBAEpD,IAAM,QAAQ,GAAG,SAAS,CAAC,MAAM,GAAG,QAAQ,CAAC;gBAE7C,OAAO,QAAQ,GAAG,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC;aAC9C;iBAAM;gBACH,OAAO,KAAK,CAAC,CAAC,qCAAqC;aACtD;SACJ;aAAM;YACH,OAAO,KAAK,CAAC;SAChB;IACL,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"]}
1
+ {"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelDirection.ts"],"names":[],"mappings":";;;;IAmBA,4CAA4C;IAC5C,uHAAuH;IACvH,IAAM,cAAc,GAAG,yDAAyD,CAAC;IACjF,IAAM,cAAc,GAAG,0EAA0E,CAAC;IAClG,IAAM,gBAAgB,GAAG,KAAK,CAAC;IAE/B;;OAEG;IACH,SAAgB,iBAAiB,CAC7B,KAAmC,EACnC,SAAiC;QAEjC,IAAA,uDAA0B,EAAC,KAAK,CAAC,CAAC;QAElC,IAAM,0BAA0B,GAAG,IAAA,kDAAoB,EACnD,KAAK,EACL,CAAC,UAAU,CAAC,EACZ,CAAC,WAAW,CAAC,CAChB,CAAC;QAEF,0BAA0B,CAAC,OAAO,CAAC,UAAC,EAAS;gBAAP,KAAK,WAAA;YACvC,IAAI,aAA4B,CAAC;YACjC,IAAI,SAAS,KAAK,MAAM,EAAE;gBACtB,aAAa,GAAG,sBAAsB,CAAC,KAAK,CAAC,CAAC;aACjD;iBAAM;gBACH,aAAa,GAAG,SAAS,CAAC;aAC7B;YACD,IAAI,IAAA,gDAAkB,EAAuB,KAAK,EAA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InSameThread';\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 // We already set direction on levels, no need to keep it on list item level\n delete item.format.direction;\n\n // Remove textAlign to let it be calculated based on direction change\n delete item.format.textAlign;\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"]}
@@ -3,9 +3,18 @@ define(["require", "exports", "roosterjs-content-model-dom"], function (require,
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.alignTableCellVertically = exports.alignTableCellHorizontally = void 0;
5
5
  var TextAlignValueMap = {
6
- alignCellLeft: 'start',
7
- alignCellCenter: 'center',
8
- alignCellRight: 'end',
6
+ alignCellLeft: {
7
+ ltr: 'start',
8
+ rtl: 'end',
9
+ },
10
+ alignCellCenter: {
11
+ ltr: 'center',
12
+ rtl: 'center',
13
+ },
14
+ alignCellRight: {
15
+ ltr: 'end',
16
+ rtl: 'start',
17
+ },
9
18
  };
10
19
  var VerticalAlignValueMap = {
11
20
  alignCellTop: 'top',
@@ -17,7 +26,8 @@ define(["require", "exports", "roosterjs-content-model-dom"], function (require,
17
26
  */
18
27
  function alignTableCellHorizontally(table, operation) {
19
28
  alignTableCellInternal(table, function (cell) {
20
- cell.format.textAlign = TextAlignValueMap[operation];
29
+ cell.format.textAlign =
30
+ TextAlignValueMap[operation][cell.format.direction == 'rtl' ? 'rtl' : 'ltr'];
21
31
  });
22
32
  }
23
33
  exports.alignTableCellHorizontally = alignTableCellHorizontally;
@@ -1 +1 @@
1
- {"version":3,"file":"alignTableCell.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/alignTableCell.ts"],"names":[],"mappings":";;;;IAYA,IAAM,iBAAiB,GAGlB;QACD,aAAa,EAAE,OAAO;QACtB,eAAe,EAAE,QAAQ;QACzB,cAAc,EAAE,KAAK;KACxB,CAAC;IAEF,IAAM,qBAAqB,GAGtB;QACD,YAAY,EAAE,KAAK;QACnB,eAAe,EAAE,QAAQ;QACzB,eA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AE5B,IAAI,MAAM,EAAE;wBACR,QAAQ,CAAC,IAAA,yCAAW,EAAC,IAAI,CAAC,CAAC,CAAC;wBAE5B,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAA,KAAK;4BACrB,IAAI,KAAK,CAAC,SAAS,KAAK,WAAW,IAAI,KAAK,CAAC,MAAM,CAAC,SAAS,EAAE;gCAC3D,OAAO,IAAA,yCAAW,EAAC,KAAK,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;6BAC9C;wBACL,CAAC,CAAC,CAAC;qBACN;iBACJ;aACJ;SACJ;IACL,CAAC","sourcesContent":["import {\n getSelectedCells,\n mutateBlock,\n updateTableCellMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ShallowMutableContentModelTable,\n ShallowMutableContentModelTableCell,\n TableCellHorizontalAlignOperation,\n TableCellVerticalAlignOperation,\n} from 'roosterjs-content-model-types';\n\nconst TextAlignValueMap: Partial<Record<\n TableCellHorizontalAlignOperation,\n 'start' | 'center' | 'end'\n>> = {\n alignCellLeft: 'start',\n alignCellCenter: 'center',\n alignCellRight: 'end',\n};\n\nconst VerticalAlignValueMap: Partial<Record<\n TableCellVerticalAlignOperation,\n 'top' | 'middle' | 'bottom'\n>> = {\n alignCellTop: 'top',\n alignCellMiddle: 'middle',\n alignCellBottom: 'bottom',\n};\n\n/**\n * @internal\n */\nexport function alignTableCellHorizontally(\n table: ShallowMutableContentModelTable,\n operation: TableCellHorizontalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.textAlign = TextAlignValueMap[operation];\n });\n}\n\n/**\n * @internal\n */\nexport function alignTableCellVertically(\n table: ShallowMutableContentModelTable,\n operation: TableCellVerticalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.verticalAlign = VerticalAlignValueMap[operation];\n\n updateTableCellMetadata(cell, metadata => {\n metadata = metadata || {};\n metadata.vAlignOverride = true;\n return metadata;\n });\n });\n}\n\nfunction alignTableCellInternal(\n table: ShallowMutableContentModelTable,\n callback: (cell: ShallowMutableContentModelTableCell) => void\n) {\n const sel = getSelectedCells(table);\n\n if (sel) {\n for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {\n for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {\n const cell = table.rows[rowIndex]?.cells[colIndex];\n const format = cell?.format;\n\n if (format) {\n callback(mutateBlock(cell));\n\n cell.blocks.forEach(block => {\n if (block.blockType === 'Paragraph' && block.format.textAlign) {\n delete mutateBlock(block).format.textAlign;\n }\n });\n }\n }\n }\n }\n}\n"]}
1
+ {"version":3,"file":"alignTableCell.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/alignTableCell.ts"],"names":[],"mappings":";;;;IAYA,IAAM,iBAAiB,GAGnB;QACA,aAAa,EAAE;YACX,GAAG,EAAE,OAAO;YACZ,GAAG,EAAE,KAAK;SACb;QACD,eAAe,EAAE;YACb,GAAG,EAAE,QAAQ;YACb,GAAG,EAAE,QAAQ;SAChB;QACD,cAAc,EAAE;YACZ,GAAG,EAAE,KAAK;YACV,GAAG,EAAE,OAAO;SACf;KACJ,CAAC;IAEF,IAAM,qBAAqB,GAGtB;QACD,YAAY,EAAE,KAAK;QACnB,eAAe,EAAE,QAAQ;QACzB,eAAe,EAAE,QAAQ;KAC5B,CAAC;IAEF;;OAEG;IACH,SAAgB,0BAA0B,CACtC,KAAsC,EACtC,SAA4C;QAE5C,sBAAsB,CAAC,KAAK,EAAE,UAAA,IAAI;YAC9B,IAAI,CAAC,MAAM,CAAC,SAAS;gBACjB,iBAAiB,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACrF,CAAC,CAAC,CAAC;IACP,CAAC;IARD,gEAQC;IAED;;OAEG;IACH,SAAgB,wBAAwB,CACpC,KAAsC,EACtC,SAA0C;QAE1C,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AAC,CAAC;qBACN;iBACJ;aACJ;SACJ;IACL,CAAC","sourcesContent":["import {\n getSelectedCells,\n mutateBlock,\n updateTableCellMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ShallowMutableContentModelTable,\n ShallowMutableContentModelTableCell,\n TableCellHorizontalAlignOperation,\n TableCellVerticalAlignOperation,\n} from 'roosterjs-content-model-types';\n\nconst TextAlignValueMap: Record<\n TableCellHorizontalAlignOperation,\n Partial<Record<'ltr' | 'rtl', 'start' | 'center' | 'end'>>\n> = {\n alignCellLeft: {\n ltr: 'start',\n rtl: 'end',\n },\n alignCellCenter: {\n ltr: 'center',\n rtl: 'center',\n },\n alignCellRight: {\n ltr: 'end',\n rtl: 'start',\n },\n};\n\nconst VerticalAlignValueMap: Partial<Record<\n TableCellVerticalAlignOperation,\n 'top' | 'middle' | 'bottom'\n>> = {\n alignCellTop: 'top',\n alignCellMiddle: 'middle',\n alignCellBottom: 'bottom',\n};\n\n/**\n * @internal\n */\nexport function alignTableCellHorizontally(\n table: ShallowMutableContentModelTable,\n operation: TableCellHorizontalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.textAlign =\n TextAlignValueMap[operation][cell.format.direction == 'rtl' ? 'rtl' : 'ltr'];\n });\n}\n\n/**\n * @internal\n */\nexport function alignTableCellVertically(\n table: ShallowMutableContentModelTable,\n operation: TableCellVerticalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.verticalAlign = VerticalAlignValueMap[operation];\n\n updateTableCellMetadata(cell, metadata => {\n metadata = metadata || {};\n metadata.vAlignOverride = true;\n return metadata;\n });\n });\n}\n\nfunction alignTableCellInternal(\n table: ShallowMutableContentModelTable,\n callback: (cell: ShallowMutableContentModelTableCell) => void\n) {\n const sel = getSelectedCells(table);\n\n if (sel) {\n for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {\n for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {\n const cell = table.rows[rowIndex]?.cells[colIndex];\n const format = cell?.format;\n\n if (format) {\n callback(mutateBlock(cell));\n\n cell.blocks.forEach(block => {\n if (block.blockType === 'Paragraph' && block.format.textAlign) {\n delete mutateBlock(block).format.textAlign;\n }\n });\n }\n }\n }\n }\n}\n"]}
@@ -1,5 +1,5 @@
1
- import type { ContentModelBlockGroup, ContentModelTable } from 'roosterjs-content-model-types';
1
+ import type { ContentModelBlockGroup, ContentModelTable, ContentModelTableCellFormat } from 'roosterjs-content-model-types';
2
2
  /**
3
3
  * @internal
4
4
  */
5
- export declare function createTableStructure(parent: ContentModelBlockGroup, columns: number, rows: number): ContentModelTable;
5
+ export declare function createTableStructure(parent: ContentModelBlockGroup, columns: number, rows: number, cellFormat?: ContentModelTableCellFormat): ContentModelTable;
@@ -5,12 +5,12 @@ define(["require", "exports", "roosterjs-content-model-dom"], function (require,
5
5
  /**
6
6
  * @internal
7
7
  */
8
- function createTableStructure(parent, columns, rows) {
8
+ function createTableStructure(parent, columns, rows, cellFormat) {
9
9
  var table = (0, roosterjs_content_model_dom_1.createTable)(rows);
10
10
  (0, roosterjs_content_model_dom_1.addBlock)(parent, table);
11
11
  table.rows.forEach(function (row) {
12
12
  for (var i = 0; i < columns; i++) {
13
- var cell = (0, roosterjs_content_model_dom_1.createTableCell)();
13
+ var cell = (0, roosterjs_content_model_dom_1.createTableCell)(undefined /*spanLeftOrColSpan */, undefined /*spanAboveOrRowSpan */, undefined /* isHeader */, cellFormat);
14
14
  row.cells.push(cell);
15
15
  }
16
16
  });
@@ -1 +1 @@
1
- {"version":3,"file":"createTableStructure.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/createTableStructure.ts"],"names":[],"mappings":";;;;IAGA;;OAEG;IACH,SAAgB,oBAAoB,CAChC,MAA8B,EAC9B,OAAe,EACf,IAAY;QAEZ,IAAM,KAAK,GAAG,IAAA,yCAAW,EAAC,IAAI,CAAC,CAAC;QAEhC,IAAA,sCAAQ,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAExB,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAA,GAAG;YAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;gBAC9B,IAAM,IAAI,GAAG,IAAA,6CAAe,GAAE,CAAC;gBAE/B,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACxB;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACjB,CAAC;IAlBD,oDAkBC","sourcesContent":["import { addBlock, createTable, createTableCell } from 'roosterjs-content-model-dom';\nimport type { ContentModelBlockGroup, ContentModelTable } from 'roosterjs-content-model-types';\n\n/**\n * @internal\n */\nexport function createTableStructure(\n parent: ContentModelBlockGroup,\n columns: number,\n rows: number\n): ContentModelTable {\n const table = createTable(rows);\n\n addBlock(parent, table);\n\n table.rows.forEach(row => {\n for (let i = 0; i < columns; i++) {\n const cell = createTableCell();\n\n row.cells.push(cell);\n }\n });\n\n return table;\n}\n"]}
1
+ {"version":3,"file":"createTableStructure.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/createTableStructure.ts"],"names":[],"mappings":";;;;IAOA;;OAEG;IACH,SAAgB,oBAAoB,CAChC,MAA8B,EAC9B,OAAe,EACf,IAAY,EACZ,UAAwC;QAExC,IAAM,KAAK,GAAG,IAAA,yCAAW,EAAC,IAAI,CAAC,CAAC;QAEhC,IAAA,sCAAQ,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAExB,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAA,GAAG;YAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;gBAC9B,IAAM,IAAI,GAAG,IAAA,6CAAe,EACxB,SAAS,CAAC,sBAAsB,EAChC,SAAS,CAAC,uBAAuB,EACjC,SAAS,CAAC,cAAc,EACxB,UAAU,CACb,CAAC;gBAEF,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACxB;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACjB,CAAC;IAxBD,oDAwBC","sourcesContent":["import { addBlock, createTable, createTableCell } from 'roosterjs-content-model-dom';\nimport type {\n ContentModelBlockGroup,\n ContentModelTable,\n ContentModelTableCellFormat,\n} from 'roosterjs-content-model-types';\n\n/**\n * @internal\n */\nexport function createTableStructure(\n parent: ContentModelBlockGroup,\n columns: number,\n rows: number,\n cellFormat?: ContentModelTableCellFormat\n): ContentModelTable {\n const table = createTable(rows);\n\n addBlock(parent, table);\n\n table.rows.forEach(row => {\n for (let i = 0; i < columns; i++) {\n const cell = createTableCell(\n undefined /*spanLeftOrColSpan */,\n undefined /*spanAboveOrRowSpan */,\n undefined /* isHeader */,\n cellFormat\n );\n\n row.cells.push(cell);\n }\n });\n\n return table;\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { ContentModelTableFormat, IEditor, TableMetadataFormat } from 'roosterjs-content-model-types';
1
+ import type { ContentModelTableFormat, IEditor, TableMetadataFormat, ContentModelTableCellFormat } from 'roosterjs-content-model-types';
2
2
  /**
3
3
  * Insert table into editor at current selection
4
4
  * @param editor The editor instance
@@ -7,5 +7,6 @@ import type { ContentModelTableFormat, IEditor, TableMetadataFormat } from 'roos
7
7
  * @param rows Number of rows in table
8
8
  * @param tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB
9
9
  * @param format (Optional) The table format used for style attributes
10
+ * @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat
10
11
  */
11
- export declare function insertTable(editor: IEditor, columns: number, rows: number, tableMetadataFormat?: Partial<TableMetadataFormat>, format?: ContentModelTableFormat): void;
12
+ export declare function insertTable(editor: IEditor, columns: number, rows: number, tableMetadataFormat?: Partial<TableMetadataFormat>, format?: ContentModelTableFormat, customCellFormat?: ContentModelTableCellFormat): void;
@@ -10,19 +10,21 @@ define(["require", "exports", "tslib", "../../modelApi/common/adjustIndentation"
10
10
  * @param rows Number of rows in table
11
11
  * @param tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB
12
12
  * @param format (Optional) The table format used for style attributes
13
+ * @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat
13
14
  */
14
- function insertTable(editor, columns, rows, tableMetadataFormat, format) {
15
+ function insertTable(editor, columns, rows, tableMetadataFormat, format, customCellFormat) {
15
16
  editor.focus();
16
17
  editor.formatContentModel(function (model, context) {
17
18
  var _a, _b, _c;
18
19
  var insertPosition = (0, roosterjs_content_model_dom_1.deleteSelection)(model, [], context).insertPoint;
19
20
  if (insertPosition) {
20
21
  var doc = (0, roosterjs_content_model_dom_1.createContentModelDocument)();
21
- var table = (0, createTableStructure_1.createTableStructure)(doc, columns, rows);
22
+ var table = (0, createTableStructure_1.createTableStructure)(doc, columns, rows, customCellFormat);
22
23
  if (format) {
23
24
  table.format = (0, tslib_1.__assign)({}, format);
24
25
  }
25
26
  (0, roosterjs_content_model_dom_1.normalizeTable)(table, editor.getPendingFormat() || insertPosition.marker.format);
27
+ initCellWidth(table);
26
28
  (0, adjustIndentation_1.adjustTableIndentation)(insertPosition, table);
27
29
  // Assign default vertical align
28
30
  tableMetadataFormat = tableMetadataFormat || { verticalAlign: 'top' };
@@ -47,5 +49,27 @@ define(["require", "exports", "tslib", "../../modelApi/common/adjustIndentation"
47
49
  });
48
50
  }
49
51
  exports.insertTable = insertTable;
52
+ function initCellWidth(table) {
53
+ var columns = Math.max.apply(Math, (0, tslib_1.__spreadArray)([], (0, tslib_1.__read)(table.rows.map(function (row) { return row.cells.length; })), false));
54
+ for (var i = 0; i < columns; i++) {
55
+ if (table.widths[i] === undefined) {
56
+ table.widths[i] = getTableCellWidth(columns);
57
+ }
58
+ else if (table.widths[i] < roosterjs_content_model_dom_1.MIN_ALLOWED_TABLE_CELL_WIDTH) {
59
+ table.widths[i] = roosterjs_content_model_dom_1.MIN_ALLOWED_TABLE_CELL_WIDTH;
60
+ }
61
+ }
62
+ }
63
+ function getTableCellWidth(columns) {
64
+ if (columns <= 4) {
65
+ return 120;
66
+ }
67
+ else if (columns <= 6) {
68
+ return 100;
69
+ }
70
+ else {
71
+ return 70;
72
+ }
73
+ }
50
74
  });
51
75
  //# sourceMappingURL=insertTable.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"insertTable.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/table/insertTable.ts"],"names":[],"mappings":";;;;IAiBA;;;;;;;;OAQG;IACH,SAAgB,WAAW,CACvB,MAAe,EACf,OAAe,EACf,IAAY,EACZ,mBAAkD,EAClD,MAAgC;QAEhC,MAAM,CAAC,KAAK,EAAE,CAAC;QAEf,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;;YACX,IAAM,cAAc,GAAG,IAAA,6CAAe,EAAC,KAAK,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC,WAAW,CAAC;YAEvE,IAAI,cAAc,EAAE;gBAChB,IAAM,GAAG,GAAG,IAAA,wDAA0B,GAAE,CAAC;gBACzC,IAAM,KAAK,GAAG,IAAA,2CAAoB,EAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;gBACvD,IAAI,MAAM,EAAE;oBACR,KAAK,CAAC,MAAM,6BAAQ,MAAM,CAAE,CAAC;iBAChC;gBAED,IAAA,4CAAc,EAAC,KAAK,EAAE,MAAM,CAAC,gBAAgB,EAAE,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAEjF,IAAA,0CAAsB,EAAC,cAAc,EAAE,KAAK,CAAC,CAAC;gBAE9C,gCAAgC;gBAChC,mBAAmB,GAAG,mBAAmB,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC;gBACtE,IAAA,8CAAgB,EAAC,KAAK,EAAE,mBAAmB,CAAC,CAAC;gBAC7C,IAAA,wCAAU,EAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE;oBAC5B,cAAc,gBAAA;oBACd,WAAW,EAAE,UAAU;iBAC1B,CAAC,CAAC;gBAEH,IAAM,UAAU,GAAG,MAAA,MAAA,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,0CAAE,KAAK,CAAC,CAAC,CAAC,0CAAE,MAAM,CAAC,CAAC,CAAC,CAAC;gBAEtD,IAAI,CAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,SAAS,KAAI,WAAW,EAAE;oBACtC,IAAM,MAAM,GAAG,IAAA,mDAAqB,EAAC,MAAA,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,0CAAE,MAAM,CAAC,CAAC;oBACrE,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;oBACpC,IAAA,0CAAY,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC;iBAC/B;gBAED,OAAO,IAAI,CAAC;aACf;iBAAM;gBACH,OAAO,KAAK,CAAC;aAChB;QACL,CAAC,EACD;YACI,OAAO,EAAE,aAAa;SACzB,CACJ,CAAC;IACN,CAAC;IAjDD,kCAiDC","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 {\n ContentModelTableFormat,\n IEditor,\n TableMetadataFormat,\n} 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 tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB\n * @param format (Optional) The table format used for style attributes\n */\nexport function insertTable(\n editor: IEditor,\n columns: number,\n rows: number,\n tableMetadataFormat?: Partial<TableMetadataFormat>,\n format?: ContentModelTableFormat\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 if (format) {\n table.format = { ...format };\n }\n\n normalizeTable(table, editor.getPendingFormat() || insertPosition.marker.format);\n\n adjustTableIndentation(insertPosition, table);\n\n // Assign default vertical align\n tableMetadataFormat = tableMetadataFormat || { verticalAlign: 'top' };\n applyTableFormat(table, tableMetadataFormat);\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":";;;;IAoBA;;;;;;;;;OASG;IACH,SAAgB,WAAW,CACvB,MAAe,EACf,OAAe,EACf,IAAY,EACZ,mBAAkD,EAClD,MAAgC,EAChC,gBAA8C;QAE9C,MAAM,CAAC,KAAK,EAAE,CAAC;QAEf,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;;YACX,IAAM,cAAc,GAAG,IAAA,6CAAe,EAAC,KAAK,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC,WAAW,CAAC;YAEvE,IAAI,cAAc,EAAE;gBAChB,IAAM,GAAG,GAAG,IAAA,wDAA0B,GAAE,CAAC;gBACzC,IAAM,KAAK,GAA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AAC;IACN,CAAC;IAnDD,kCAmDC;IAED,SAAS,aAAa,CAAC,KAAwB;QAC3C,IAAM,OAAO,GAAG,IAAI,CAAC,GAAG,OAAR,IAAI,qDAAQ,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,UAAA,GAAG,IAAI,OAAA,GAAG,CAAC,KAAK,CAAC,MAAM,EAAhB,CAAgB,CAAC,UAAC,CAAC;QAErE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;YAC9B,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE;gBAC/B,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAC,OAAO,CAAC,CAAC;aAChD;iBAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,0DAA4B,EAAE;gBACvD,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,0DAA4B,CAAC;aAClD;SACJ;IACL,CAAC;IAED,SAAS,iBAAiB,CAAC,OAAe;QACtC,IAAI,OAAO,IAAI,CAAC,EAAE;YACd,OAAO,GAAG,CAAC;SACd;aAAM,IAAI,OAAO,IAAI,CAAC,EAAE;YACrB,OAAO,GAAG,CAAC;SACd;aAAM;YACH,OAAO,EAAE,CAAC;SACb;IACL,CAAC","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 MIN_ALLOWED_TABLE_CELL_WIDTH,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelTable,\n ContentModelTableFormat,\n IEditor,\n TableMetadataFormat,\n ContentModelTableCellFormat,\n} 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 tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB\n * @param format (Optional) The table format used for style attributes\n * @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat\n */\nexport function insertTable(\n editor: IEditor,\n columns: number,\n rows: number,\n tableMetadataFormat?: Partial<TableMetadataFormat>,\n format?: ContentModelTableFormat,\n customCellFormat?: ContentModelTableCellFormat\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, customCellFormat);\n if (format) {\n table.format = { ...format };\n }\n\n normalizeTable(table, editor.getPendingFormat() || insertPosition.marker.format);\n initCellWidth(table);\n\n adjustTableIndentation(insertPosition, table);\n\n // Assign default vertical align\n tableMetadataFormat = tableMetadataFormat || { verticalAlign: 'top' };\n applyTableFormat(table, tableMetadataFormat);\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\nfunction initCellWidth(table: ContentModelTable) {\n const columns = Math.max(...table.rows.map(row => row.cells.length));\n\n for (let i = 0; i < columns; i++) {\n if (table.widths[i] === undefined) {\n table.widths[i] = getTableCellWidth(columns);\n } else if (table.widths[i] < MIN_ALLOWED_TABLE_CELL_WIDTH) {\n table.widths[i] = MIN_ALLOWED_TABLE_CELL_WIDTH;\n }\n }\n}\n\nfunction getTableCellWidth(columns: number): number {\n if (columns <= 4) {\n return 120;\n } else if (columns <= 6) {\n return 100;\n } else {\n return 70;\n }\n}\n"]}
@@ -28,6 +28,10 @@ export function setModelDirection(model, direction) {
28
28
  item.levels.forEach(function (level) {
29
29
  level.format.direction = calcDirection;
30
30
  });
31
+ // We already set direction on levels, no need to keep it on list item level
32
+ delete item.format.direction;
33
+ // Remove textAlign to let it be calculated based on direction change
34
+ delete item.format.textAlign;
31
35
  item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
32
36
  });
33
37
  }
@@ -1 +1 @@
1
- {"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelDirection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,mCAAmC,CAAC;AAC9E,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EACH,gBAAgB,EAChB,oBAAoB,EACpB,kBAAkB,EAClB,WAAW,EACX,uBAAuB,GAC1B,MAAM,6BAA6B,CAAC;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,MAAM,UAAU,iBAAiB,CAC7B,KAAmC,EACnC,SAAiC;IAEjC,0BAA0B,CAAC,KAAK,CAAC,CAAC;IAElC,IAAM,0BAA0B,GAAG,oBAAoB,CACnD,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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E;QACjC,IAAM,iBAAiB,GAA+B,KAAK,CAAC,QAAQ,CAAC,MAAM,CACvE,UAAC,GAAG,IAAsC,OAAA,GAAG,CAAC,WAAW,KAAK,MAAM,EAA1B,CAA0B,CACvE,CAAC;QACF,IAAI,SAAS,GACT,iBAAiB,CAAC,MAAM,GAAG,CAAC;YACxB,CAAC,CAAC,iBAAiB,CAAC,MAAM,CAAC,UAAC,IAAI,EAAE,GAAG,IAAK,OAAA,IAAI,GAAG,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"]}
1
+ {"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block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E,GAAG,IAAK,OAAA,IAAI,GAAG,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 // We already set direction on levels, no need to keep it on list item level\n delete item.format.direction;\n\n // Remove textAlign to let it be calculated based on direction change\n delete item.format.textAlign;\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"]}
@@ -1,8 +1,17 @@
1
1
  import { getSelectedCells, mutateBlock, updateTableCellMetadata, } from 'roosterjs-content-model-dom';
2
2
  var TextAlignValueMap = {
3
- alignCellLeft: 'start',
4
- alignCellCenter: 'center',
5
- alignCellRight: 'end',
3
+ alignCellLeft: {
4
+ ltr: 'start',
5
+ rtl: 'end',
6
+ },
7
+ alignCellCenter: {
8
+ ltr: 'center',
9
+ rtl: 'center',
10
+ },
11
+ alignCellRight: {
12
+ ltr: 'end',
13
+ rtl: 'start',
14
+ },
6
15
  };
7
16
  var VerticalAlignValueMap = {
8
17
  alignCellTop: 'top',
@@ -14,7 +23,8 @@ var VerticalAlignValueMap = {
14
23
  */
15
24
  export function alignTableCellHorizontally(table, operation) {
16
25
  alignTableCellInternal(table, function (cell) {
17
- cell.format.textAlign = TextAlignValueMap[operation];
26
+ cell.format.textAlign =
27
+ TextAlignValueMap[operation][cell.format.direction == 'rtl' ? 'rtl' : 'ltr'];
18
28
  });
19
29
  }
20
30
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"alignTableCell.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/alignTableCell.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,gBAAgB,EAChB,WAAW,EACX,uBAAuB,GAC1B,MAAM,6BAA6B,CAAC;AAQrC,IAAM,iBAAiB,GAGlB;IACD,aAAa,EAAE,OAAO;IACtB,eAAe,EAAE,QAAQ;IACzB,cAAc,EAAE,KAAK;CACxB,CAAC;AAEF,IAAM,qBAAqB,GAGtB;IACD,YAAY,EAAE,KAAK;IACnB,eAAe,EAAE,QAAQ;IACzB,eAAe,EAAE,QAAQ;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,0BAA0B,CACtC,KAAsC,EACtC,SAA4C;IAE5C,sBAAsB,CAAC,KAAK,EAAE,UAAA,IAAI;QAC9B,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,iBAAiB,CAAC,SAAS,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;AACP,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACpC,KAAsC,EACtC,SAA0C;IAE1C,sBAAsB,CAAC,KAAK,EAAE,UAAA,IAAI;QAC9B,IAAI,CAAC,MAAM,CAAC,aAAa,GAAG,qBAAqB,CAAC,SAAS,CAAC,CAAC;QAE7D,uBAAuB,CAAC,IAAI,EAAE,UAAA,QAAQ;YAClC,QAAQ,GAAG,QAAQ,IAAI,EAAE,CAAC;YAC1B,QAAQ,CAAC,cAAc,GAAG,IAAI,CAAC;YAC/B,OAAO,QAAQ,CAAC;QACpB,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC;AAED,SAAS,sBAAsB,CAC3B,KAAsC,EACtC,QAA6D;;IAE7D,IAAM,GAAG,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAEpC,IAAI,GAAG,EAAE;QACL,KAAK,IAAI,QAAQ,GAAG,GAAG,CAAC,QAAQ,EAAE,QAAQ,IAAI,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE;YACnE,KAAK,IAAI,QAAQ,GAAG,GAAG,CAAC,WAAW,EAAE,QAAQ,IAAI,GAAG,CAAC,UAAU,EAAE,QAAQ,EAAE,EAAE;gBACzE,IAAM,IAAI,GAAG,MAAA,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,0CAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;gBACnD,IAAM,MAAM,GAAG,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,MAAM,CAAC;gBAE5B,IAAI,MAAM,EAAE;oBACR,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;oBAE5B,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAA,KAAK;wBACrB,IAAI,KAAK,CAAC,SAAS,KAAK,WAAW,IAAI,KAAK,CAAC,MAAM,CAAC,SAAS,EAAE;4BAC3D,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;yBAC9C;oBACL,CAAC,CAAC,CAAC;iBACN;aACJ;SACJ;KACJ;AACL,CAAC","sourcesContent":["import {\n getSelectedCells,\n mutateBlock,\n updateTableCellMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ShallowMutableContentModelTable,\n ShallowMutableContentModelTableCell,\n TableCellHorizontalAlignOperation,\n TableCellVerticalAlignOperation,\n} from 'roosterjs-content-model-types';\n\nconst TextAlignValueMap: Partial<Record<\n TableCellHorizontalAlignOperation,\n 'start' | 'center' | 'end'\n>> = {\n alignCellLeft: 'start',\n alignCellCenter: 'center',\n alignCellRight: 'end',\n};\n\nconst VerticalAlignValueMap: Partial<Record<\n TableCellVerticalAlignOperation,\n 'top' | 'middle' | 'bottom'\n>> = {\n alignCellTop: 'top',\n alignCellMiddle: 'middle',\n alignCellBottom: 'bottom',\n};\n\n/**\n * @internal\n */\nexport function alignTableCellHorizontally(\n table: ShallowMutableContentModelTable,\n operation: TableCellHorizontalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.textAlign = TextAlignValueMap[operation];\n });\n}\n\n/**\n * @internal\n */\nexport function alignTableCellVertically(\n table: ShallowMutableContentModelTable,\n operation: TableCellVerticalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.verticalAlign = VerticalAlignValueMap[operation];\n\n updateTableCellMetadata(cell, metadata => {\n metadata = metadata || {};\n metadata.vAlignOverride = true;\n return metadata;\n });\n });\n}\n\nfunction alignTableCellInternal(\n table: ShallowMutableContentModelTable,\n callback: (cell: ShallowMutableContentModelTableCell) => void\n) {\n const sel = getSelectedCells(table);\n\n if (sel) {\n for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {\n for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {\n const cell = table.rows[rowIndex]?.cells[colIndex];\n const format = cell?.format;\n\n if (format) {\n callback(mutateBlock(cell));\n\n cell.blocks.forEach(block => {\n if (block.blockType === 'Paragraph' && block.format.textAlign) {\n delete mutateBlock(block).format.textAlign;\n }\n });\n }\n }\n }\n }\n}\n"]}
1
+ {"version":3,"file":"alignTableCell.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/alignTableCell.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,gBAAgB,EAChB,WAAW,EACX,uBAAuB,GAC1B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,GAAG,EAAE;QACL,KAAK,IAAI,QAAQ,GAAG,GAAG,CAAC,QAAQ,EAAE,QAAQ,IAAI,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE;YACnE,KAAK,IAAI,QAAQ,GAAG,GAAG,CAAC,WAAW,EAAE,QAAQ,IAAI,GAAG,CAAC,UAAU,EAAE,QAAQ,EAAE,EAAE;gBACzE,IAAM,IAAI,GAAG,MAAA,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,0CAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;gBACnD,IAAM,MAAM,GAAG,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,MAAM,CAAC;gBAE5B,IAAI,MAAM,EAAE;oBACR,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;oBAE5B,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAA,KAAK;wBACrB,IAAI,KAAK,CAAC,SAAS,KAAK,WAAW,IAAI,KAAK,CAAC,MAAM,CAAC,SAAS,EAAE;4BAC3D,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;yBAC9C;oBACL,CAAC,CAAC,CAAC;iBACN;aACJ;SACJ;KACJ;AACL,CAAC","sourcesContent":["import {\n getSelectedCells,\n mutateBlock,\n updateTableCellMetadata,\n} from 'roosterjs-content-model-dom';\nimport type {\n ShallowMutableContentModelTable,\n ShallowMutableContentModelTableCell,\n TableCellHorizontalAlignOperation,\n TableCellVerticalAlignOperation,\n} from 'roosterjs-content-model-types';\n\nconst TextAlignValueMap: Record<\n TableCellHorizontalAlignOperation,\n Partial<Record<'ltr' | 'rtl', 'start' | 'center' | 'end'>>\n> = {\n alignCellLeft: {\n ltr: 'start',\n rtl: 'end',\n },\n alignCellCenter: {\n ltr: 'center',\n rtl: 'center',\n },\n alignCellRight: {\n ltr: 'end',\n rtl: 'start',\n },\n};\n\nconst VerticalAlignValueMap: Partial<Record<\n TableCellVerticalAlignOperation,\n 'top' | 'middle' | 'bottom'\n>> = {\n alignCellTop: 'top',\n alignCellMiddle: 'middle',\n alignCellBottom: 'bottom',\n};\n\n/**\n * @internal\n */\nexport function alignTableCellHorizontally(\n table: ShallowMutableContentModelTable,\n operation: TableCellHorizontalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.textAlign =\n TextAlignValueMap[operation][cell.format.direction == 'rtl' ? 'rtl' : 'ltr'];\n });\n}\n\n/**\n * @internal\n */\nexport function alignTableCellVertically(\n table: ShallowMutableContentModelTable,\n operation: TableCellVerticalAlignOperation\n) {\n alignTableCellInternal(table, cell => {\n cell.format.verticalAlign = VerticalAlignValueMap[operation];\n\n updateTableCellMetadata(cell, metadata => {\n metadata = metadata || {};\n metadata.vAlignOverride = true;\n return metadata;\n });\n });\n}\n\nfunction alignTableCellInternal(\n table: ShallowMutableContentModelTable,\n callback: (cell: ShallowMutableContentModelTableCell) => void\n) {\n const sel = getSelectedCells(table);\n\n if (sel) {\n for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {\n for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {\n const cell = table.rows[rowIndex]?.cells[colIndex];\n const format = cell?.format;\n\n if (format) {\n callback(mutateBlock(cell));\n\n cell.blocks.forEach(block => {\n if (block.blockType === 'Paragraph' && block.format.textAlign) {\n delete mutateBlock(block).format.textAlign;\n }\n });\n }\n }\n }\n }\n}\n"]}
@@ -1,5 +1,5 @@
1
- import type { ContentModelBlockGroup, ContentModelTable } from 'roosterjs-content-model-types';
1
+ import type { ContentModelBlockGroup, ContentModelTable, ContentModelTableCellFormat } from 'roosterjs-content-model-types';
2
2
  /**
3
3
  * @internal
4
4
  */
5
- export declare function createTableStructure(parent: ContentModelBlockGroup, columns: number, rows: number): ContentModelTable;
5
+ export declare function createTableStructure(parent: ContentModelBlockGroup, columns: number, rows: number, cellFormat?: ContentModelTableCellFormat): ContentModelTable;
@@ -2,12 +2,12 @@ import { addBlock, createTable, createTableCell } from 'roosterjs-content-model-
2
2
  /**
3
3
  * @internal
4
4
  */
5
- export function createTableStructure(parent, columns, rows) {
5
+ export function createTableStructure(parent, columns, rows, cellFormat) {
6
6
  var table = createTable(rows);
7
7
  addBlock(parent, table);
8
8
  table.rows.forEach(function (row) {
9
9
  for (var i = 0; i < columns; i++) {
10
- var cell = createTableCell();
10
+ var cell = createTableCell(undefined /*spanLeftOrColSpan */, undefined /*spanAboveOrRowSpan */, undefined /* isHeader */, cellFormat);
11
11
  row.cells.push(cell);
12
12
  }
13
13
  });
@@ -1 +1 @@
1
- {"version":3,"file":"createTableStructure.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/createTableStructure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAGrF;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAChC,MAA8B,EAC9B,OAAe,EACf,IAAY;IAEZ,IAAM,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAEhC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAExB,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAA,GAAG;QAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;YAC9B,IAAM,IAAI,GAAG,eAAe,EAAE,CAAC;YAE/B,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACxB;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import { addBlock, createTable, createTableCell } from 'roosterjs-content-model-dom';\nimport type { ContentModelBlockGroup, ContentModelTable } from 'roosterjs-content-model-types';\n\n/**\n * @internal\n */\nexport function createTableStructure(\n parent: ContentModelBlockGroup,\n columns: number,\n rows: number\n): ContentModelTable {\n const table = createTable(rows);\n\n addBlock(parent, table);\n\n table.rows.forEach(row => {\n for (let i = 0; i < columns; i++) {\n const cell = createTableCell();\n\n row.cells.push(cell);\n }\n });\n\n return table;\n}\n"]}
1
+ {"version":3,"file":"createTableStructure.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/table/createTableStructure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAOrF;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAChC,MAA8B,EAC9B,OAAe,EACf,IAAY,EACZ,UAAwC;IAExC,IAAM,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAEhC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAExB,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAA,GAAG;QAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;YAC9B,IAAM,IAAI,GAAG,eAAe,CACxB,SAAS,CAAC,sBAAsB,EAChC,SAAS,CAAC,uBAAuB,EACjC,SAAS,CAAC,cAAc,EACxB,UAAU,CACb,CAAC;YAEF,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACxB;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import { addBlock, createTable, createTableCell } from 'roosterjs-content-model-dom';\nimport type {\n ContentModelBlockGroup,\n ContentModelTable,\n ContentModelTableCellFormat,\n} from 'roosterjs-content-model-types';\n\n/**\n * @internal\n */\nexport function createTableStructure(\n parent: ContentModelBlockGroup,\n columns: number,\n rows: number,\n cellFormat?: ContentModelTableCellFormat\n): ContentModelTable {\n const table = createTable(rows);\n\n addBlock(parent, table);\n\n table.rows.forEach(row => {\n for (let i = 0; i < columns; i++) {\n const cell = createTableCell(\n undefined /*spanLeftOrColSpan */,\n undefined /*spanAboveOrRowSpan */,\n undefined /* isHeader */,\n cellFormat\n );\n\n row.cells.push(cell);\n }\n });\n\n return table;\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { ContentModelTableFormat, IEditor, TableMetadataFormat } from 'roosterjs-content-model-types';
1
+ import type { ContentModelTableFormat, IEditor, TableMetadataFormat, ContentModelTableCellFormat } from 'roosterjs-content-model-types';
2
2
  /**
3
3
  * Insert table into editor at current selection
4
4
  * @param editor The editor instance
@@ -7,5 +7,6 @@ import type { ContentModelTableFormat, IEditor, TableMetadataFormat } from 'roos
7
7
  * @param rows Number of rows in table
8
8
  * @param tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB
9
9
  * @param format (Optional) The table format used for style attributes
10
+ * @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat
10
11
  */
11
- export declare function insertTable(editor: IEditor, columns: number, rows: number, tableMetadataFormat?: Partial<TableMetadataFormat>, format?: ContentModelTableFormat): void;
12
+ export declare function insertTable(editor: IEditor, columns: number, rows: number, tableMetadataFormat?: Partial<TableMetadataFormat>, format?: ContentModelTableFormat, customCellFormat?: ContentModelTableCellFormat): void;
@@ -1,7 +1,7 @@
1
- import { __assign } from "tslib";
1
+ import { __assign, __read, __spreadArray } from "tslib";
2
2
  import { adjustTableIndentation } from '../../modelApi/common/adjustIndentation';
3
3
  import { createTableStructure } from '../../modelApi/table/createTableStructure';
4
- import { createContentModelDocument, createSelectionMarker, applyTableFormat, deleteSelection, mergeModel, normalizeTable, setSelection, } from 'roosterjs-content-model-dom';
4
+ import { createContentModelDocument, createSelectionMarker, applyTableFormat, deleteSelection, mergeModel, normalizeTable, setSelection, MIN_ALLOWED_TABLE_CELL_WIDTH, } from 'roosterjs-content-model-dom';
5
5
  /**
6
6
  * Insert table into editor at current selection
7
7
  * @param editor The editor instance
@@ -10,19 +10,21 @@ import { createContentModelDocument, createSelectionMarker, applyTableFormat, de
10
10
  * @param rows Number of rows in table
11
11
  * @param tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB
12
12
  * @param format (Optional) The table format used for style attributes
13
+ * @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat
13
14
  */
14
- export function insertTable(editor, columns, rows, tableMetadataFormat, format) {
15
+ export function insertTable(editor, columns, rows, tableMetadataFormat, format, customCellFormat) {
15
16
  editor.focus();
16
17
  editor.formatContentModel(function (model, context) {
17
18
  var _a, _b, _c;
18
19
  var insertPosition = deleteSelection(model, [], context).insertPoint;
19
20
  if (insertPosition) {
20
21
  var doc = createContentModelDocument();
21
- var table = createTableStructure(doc, columns, rows);
22
+ var table = createTableStructure(doc, columns, rows, customCellFormat);
22
23
  if (format) {
23
24
  table.format = __assign({}, format);
24
25
  }
25
26
  normalizeTable(table, editor.getPendingFormat() || insertPosition.marker.format);
27
+ initCellWidth(table);
26
28
  adjustTableIndentation(insertPosition, table);
27
29
  // Assign default vertical align
28
30
  tableMetadataFormat = tableMetadataFormat || { verticalAlign: 'top' };
@@ -46,4 +48,26 @@ export function insertTable(editor, columns, rows, tableMetadataFormat, format)
46
48
  apiName: 'insertTable',
47
49
  });
48
50
  }
51
+ function initCellWidth(table) {
52
+ var columns = Math.max.apply(Math, __spreadArray([], __read(table.rows.map(function (row) { return row.cells.length; })), false));
53
+ for (var i = 0; i < columns; i++) {
54
+ if (table.widths[i] === undefined) {
55
+ table.widths[i] = getTableCellWidth(columns);
56
+ }
57
+ else if (table.widths[i] < MIN_ALLOWED_TABLE_CELL_WIDTH) {
58
+ table.widths[i] = MIN_ALLOWED_TABLE_CELL_WIDTH;
59
+ }
60
+ }
61
+ }
62
+ function getTableCellWidth(columns) {
63
+ if (columns <= 4) {
64
+ return 120;
65
+ }
66
+ else if (columns <= 6) {
67
+ return 100;
68
+ }
69
+ else {
70
+ return 70;
71
+ }
72
+ }
49
73
  //# sourceMappingURL=insertTable.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"insertTable.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/table/insertTable.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,yCAAyC,CAAC;AACjF,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EACH,0BAA0B,EAC1B,qBAAqB,EACrB,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,cAAc,EACd,YAAY,GACf,MAAM,6BAA6B,CAAC;AAOrC;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CACvB,MAAe,EACf,OAAe,EACf,IAAY,EACZ,mBAAkD,EAClD,MAAgC;IAEhC,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;;QACX,IAAM,cAAc,GAAG,eAAe,CAAC,KAAK,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC,WAAW,CAAC;QAEvE,IAAI,cAAc,EAAE;YAChB,IAAM,GAAG,GAAG,0BAA0B,EAAE,CAAC;YACzC,IAAM,KAAK,GAAG,oBAAoB,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;YACvD,IAAI,MAAM,EAAE;gBACR,KAAK,CAAC,MAAM,gBAAQ,MAAM,CAAE,CAAC;aAChC;YAED,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,gBAAgB,EAAE,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAEjF,sBAAsB,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;YAE9C,gCAAgC;YAChC,mBAAmB,GAAG,mBAAmB,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC;YACtE,gBAAgB,CAAC,KAAK,EAAE,mBAAmB,CAAC,CAAC;YAC7C,UAAU,CAAC,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,qBAAqB,CAAC,MAAA,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,0CAAE,MAAM,CAAC,CAAC;gBACrE,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBACpC,YAAY,CAAC,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","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 {\n ContentModelTableFormat,\n IEditor,\n TableMetadataFormat,\n} 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 tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB\n * @param format (Optional) The table format used for style attributes\n */\nexport function insertTable(\n editor: IEditor,\n columns: number,\n rows: number,\n tableMetadataFormat?: Partial<TableMetadataFormat>,\n format?: ContentModelTableFormat\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 if (format) {\n table.format = { ...format };\n }\n\n normalizeTable(table, editor.getPendingFormat() || insertPosition.marker.format);\n\n adjustTableIndentation(insertPosition, table);\n\n // Assign default vertical align\n tableMetadataFormat = tableMetadataFormat || { verticalAlign: 'top' };\n applyTableFormat(table, tableMetadataFormat);\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,OAAO,EAAE,sBAAsB,EAAE,MAAM,yCAAyC,CAAC;AACjF,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EACH,0BAA0B,EAC1B,qBAAqB,EACrB,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,cAAc,EACd,YAAY,EACZ,4BAA4B,GAC/B,MAAM,6BAA6B,CAAC;AASrC;;;;;;;;;GASG;AACH,MAAM,UAAU,WAAW,CACvB,MAAe,EACf,OAAe,EACf,IAAY,EACZ,mBAAkD,EAClD,MAAgC,EAChC,gBAA8C;IAE9C,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CACrB,UAAC,KAAK,EAAE,OAAO;;QACX,IAAM,cAAc,GAAG,eAAe,CAAC,KAAK,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC,WAAW,CAAC;QAEvE,IAAI,cAAc,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CACJ,CAAC;AACN,CAAC;AAED,SAAS,aAAa,CAAC,KAAwB;IAC3C,IAAM,OAAO,GAAG,IAAI,CAAC,GAAG,OAAR,IAAI,2BAAQ,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,UAAA,GAAG,IAAI,OAAA,GAAG,CAAC,KAAK,CAAC,MAAM,EAAhB,CAAgB,CAAC,UAAC,CAAC;IAErE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;QAC9B,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE;YAC/B,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAC,OAAO,CAAC,CAAC;SAChD;aAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,4BAA4B,EAAE;YACvD,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,4BAA4B,CAAC;SAClD;KACJ;AACL,CAAC;AAED,SAAS,iBAAiB,CAAC,OAAe;IACtC,IAAI,OAAO,IAAI,CAAC,EAAE;QACd,OAAO,GAAG,CAAC;KACd;SAAM,IAAI,OAAO,IAAI,CAAC,EAAE;QACrB,OAAO,GAAG,CAAC;KACd;SAAM;QACH,OAAO,EAAE,CAAC;KACb;AACL,CAAC","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 MIN_ALLOWED_TABLE_CELL_WIDTH,\n} from 'roosterjs-content-model-dom';\nimport type {\n ContentModelTable,\n ContentModelTableFormat,\n IEditor,\n TableMetadataFormat,\n ContentModelTableCellFormat,\n} 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 tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB\n * @param format (Optional) The table format used for style attributes\n * @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat\n */\nexport function insertTable(\n editor: IEditor,\n columns: number,\n rows: number,\n tableMetadataFormat?: Partial<TableMetadataFormat>,\n format?: ContentModelTableFormat,\n customCellFormat?: ContentModelTableCellFormat\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, customCellFormat);\n if (format) {\n table.format = { ...format };\n }\n\n normalizeTable(table, editor.getPendingFormat() || insertPosition.marker.format);\n initCellWidth(table);\n\n adjustTableIndentation(insertPosition, table);\n\n // Assign default vertical align\n tableMetadataFormat = tableMetadataFormat || { verticalAlign: 'top' };\n applyTableFormat(table, tableMetadataFormat);\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\nfunction initCellWidth(table: ContentModelTable) {\n const columns = Math.max(...table.rows.map(row => row.cells.length));\n\n for (let i = 0; i < columns; i++) {\n if (table.widths[i] === undefined) {\n table.widths[i] = getTableCellWidth(columns);\n } else if (table.widths[i] < MIN_ALLOWED_TABLE_CELL_WIDTH) {\n table.widths[i] = MIN_ALLOWED_TABLE_CELL_WIDTH;\n }\n }\n}\n\nfunction getTableCellWidth(columns: number): number {\n if (columns <= 4) {\n return 120;\n } else if (columns <= 6) {\n return 100;\n } else {\n return 70;\n }\n}\n"]}
package/package.json CHANGED
@@ -3,16 +3,16 @@
3
3
  "description": "Format API for roosterjs",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.1",
6
- "roosterjs-content-model-dom": "^9.44.0",
7
- "roosterjs-content-model-types": "^9.44.0"
6
+ "roosterjs-content-model-dom": "^9.45.1",
7
+ "roosterjs-content-model-types": "^9.45.1"
8
8
  },
9
- "version": "9.44.0",
9
+ "version": "9.45.1",
10
10
  "main": "./lib/index.js",
11
11
  "typings": "./lib/index.d.ts",
12
12
  "module": "./lib-mjs/index.js",
13
13
  "license": "MIT",
14
14
  "repository": {
15
15
  "type": "git",
16
- "url": "https://github.com/Microsoft/roosterjs"
16
+ "url": "git+https://github.com/Microsoft/roosterjs.git"
17
17
  }
18
18
  }