roosterjs-content-model-api 9.30.0 → 9.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/modelApi/block/setModelDirection.d.ts +1 -1
- package/lib/modelApi/block/setModelDirection.js +40 -3
- package/lib/modelApi/block/setModelDirection.js.map +1 -1
- package/lib/publicApi/block/setDirection.d.ts +2 -2
- package/lib/publicApi/block/setDirection.js +1 -1
- package/lib/publicApi/block/setDirection.js.map +1 -1
- package/lib-amd/modelApi/block/setModelDirection.d.ts +1 -1
- package/lib-amd/modelApi/block/setModelDirection.js +40 -3
- package/lib-amd/modelApi/block/setModelDirection.js.map +1 -1
- package/lib-amd/publicApi/block/setDirection.d.ts +2 -2
- package/lib-amd/publicApi/block/setDirection.js +1 -1
- package/lib-amd/publicApi/block/setDirection.js.map +1 -1
- package/lib-mjs/modelApi/block/setModelDirection.d.ts +1 -1
- package/lib-mjs/modelApi/block/setModelDirection.js +40 -3
- package/lib-mjs/modelApi/block/setModelDirection.js.map +1 -1
- package/lib-mjs/publicApi/block/setDirection.d.ts +2 -2
- package/lib-mjs/publicApi/block/setDirection.js +1 -1
- package/lib-mjs/publicApi/block/setDirection.js.map +1 -1
- package/package.json +3 -3
|
@@ -2,4 +2,4 @@ import type { ReadonlyContentModelDocument } from 'roosterjs-content-model-types
|
|
|
2
2
|
/**
|
|
3
3
|
* @internal
|
|
4
4
|
*/
|
|
5
|
-
export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl'): boolean;
|
|
5
|
+
export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl' | 'auto'): boolean;
|
|
@@ -4,6 +4,11 @@ exports.setModelDirection = void 0;
|
|
|
4
4
|
var findListItemsInSameThread_1 = require("../list/findListItemsInSameThread");
|
|
5
5
|
var splitSelectedParagraphByBr_1 = require("./splitSelectedParagraphByBr");
|
|
6
6
|
var roosterjs_content_model_dom_1 = require("roosterjs-content-model-dom");
|
|
7
|
+
// Regexes for character direction detection
|
|
8
|
+
// Strongly typed RTL character ranges. Referenced unicode's DerivedBidiClass.txt, excluding things in the 2 bit range.
|
|
9
|
+
var RTL_CHAR_REGEX = /[\u0590-\u05FF\u0600-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/g;
|
|
10
|
+
var URL_CHAR_REGEX = /http\S+|www\S+|https\S+|<a\s+(?:[^>]*?\s+)?href=(["']).*?\1.*?>.*?<\/a>/g;
|
|
11
|
+
var WHITESPACE_REGEX = /\s/g;
|
|
7
12
|
/**
|
|
8
13
|
* @internal
|
|
9
14
|
*/
|
|
@@ -12,18 +17,25 @@ function setModelDirection(model, direction) {
|
|
|
12
17
|
var paragraphOrListItemOrTable = (0, roosterjs_content_model_dom_1.getOperationalBlocks)(model, ['ListItem'], ['TableCell']);
|
|
13
18
|
paragraphOrListItemOrTable.forEach(function (_a) {
|
|
14
19
|
var block = _a.block;
|
|
20
|
+
var calcDirection;
|
|
21
|
+
if (direction === 'auto') {
|
|
22
|
+
calcDirection = determineTextDirection(block);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
calcDirection = direction;
|
|
26
|
+
}
|
|
15
27
|
if ((0, roosterjs_content_model_dom_1.isBlockGroupOfType)(block, 'ListItem')) {
|
|
16
28
|
var items = (0, findListItemsInSameThread_1.findListItemsInSameThread)(model, block);
|
|
17
29
|
items.forEach(function (readonlyItem) {
|
|
18
30
|
var item = (0, roosterjs_content_model_dom_1.mutateBlock)(readonlyItem);
|
|
19
31
|
item.levels.forEach(function (level) {
|
|
20
|
-
level.format.direction =
|
|
32
|
+
level.format.direction = calcDirection;
|
|
21
33
|
});
|
|
22
|
-
item.blocks.forEach(function (block) { return internalSetDirection(block,
|
|
34
|
+
item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
|
|
23
35
|
});
|
|
24
36
|
}
|
|
25
37
|
else if (block) {
|
|
26
|
-
internalSetDirection(block,
|
|
38
|
+
internalSetDirection(block, calcDirection);
|
|
27
39
|
}
|
|
28
40
|
});
|
|
29
41
|
return paragraphOrListItemOrTable.length > 0;
|
|
@@ -70,4 +82,29 @@ function setProperty(format, key, value) {
|
|
|
70
82
|
delete format[key];
|
|
71
83
|
}
|
|
72
84
|
}
|
|
85
|
+
// Designed to match browser's 'auto' detection, by scanning over the inner text until it hits a strong LTR/RTL character
|
|
86
|
+
function determineTextDirection(block) {
|
|
87
|
+
if (block.blockType === 'Paragraph') {
|
|
88
|
+
var findTextSegements = block.segments.filter(function (seg) { return seg.segmentType === 'Text'; });
|
|
89
|
+
var innerText = findTextSegements.length > 0
|
|
90
|
+
? findTextSegements.reduce(function (prev, seg) { return prev + seg.text; }, '')
|
|
91
|
+
: undefined;
|
|
92
|
+
if (!!innerText) {
|
|
93
|
+
// Remove links
|
|
94
|
+
innerText = innerText.replace(URL_CHAR_REGEX, '');
|
|
95
|
+
// Remove whitespace
|
|
96
|
+
innerText = innerText.replace(WHITESPACE_REGEX, '');
|
|
97
|
+
var rtlMatches = innerText.match(RTL_CHAR_REGEX);
|
|
98
|
+
var rtlCount = rtlMatches ? rtlMatches.length : 0;
|
|
99
|
+
var ltrCount = innerText.length - rtlCount;
|
|
100
|
+
return rtlCount > ltrCount ? 'rtl' : 'ltr';
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
return 'ltr'; // Default to LTR if no text is found
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
return 'ltr';
|
|
108
|
+
}
|
|
109
|
+
}
|
|
73
110
|
//# sourceMappingURL=setModelDirection.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelDirection.ts"],"names":[],"mappings":";;;AAAA,+EAA8E;AAC9E,2EAA0E;AAC1E,2EAMqC;
|
|
1
|
+
{"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../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,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"]}
|
|
@@ -2,6 +2,6 @@ import type { IEditor } from 'roosterjs-content-model-types';
|
|
|
2
2
|
/**
|
|
3
3
|
* Set text direction of selected paragraphs (Left to right or Right to left)
|
|
4
4
|
* @param editor The editor to set alignment
|
|
5
|
-
* @param direction Direction value: ltr (Left to right) or rtl (Right to left)
|
|
5
|
+
* @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)
|
|
6
6
|
*/
|
|
7
|
-
export declare function setDirection(editor: IEditor, direction: 'ltr' | 'rtl'): void;
|
|
7
|
+
export declare function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto'): void;
|
|
@@ -5,7 +5,7 @@ var setModelDirection_1 = require("../../modelApi/block/setModelDirection");
|
|
|
5
5
|
/**
|
|
6
6
|
* Set text direction of selected paragraphs (Left to right or Right to left)
|
|
7
7
|
* @param editor The editor to set alignment
|
|
8
|
-
* @param direction Direction value: ltr (Left to right) or rtl (Right to left)
|
|
8
|
+
* @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)
|
|
9
9
|
*/
|
|
10
10
|
function setDirection(editor, direction) {
|
|
11
11
|
editor.focus();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setDirection.ts"],"names":[],"mappings":";;;AAAA,4EAA2E;AAG3E;;;;GAIG;AACH,SAAgB,YAAY,CAAC,MAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"setDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setDirection.ts"],"names":[],"mappings":";;;AAAA,4EAA2E;AAG3E;;;;GAIG;AACH,SAAgB,YAAY,CAAC,MAAe,EAAE,SAAiC;IAC3E,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CAAC,UAAA,KAAK,IAAI,OAAA,IAAA,qCAAiB,EAAC,KAAK,EAAE,SAAS,CAAC,EAAnC,CAAmC,EAAE;QACpE,OAAO,EAAE,cAAc;KAC1B,CAAC,CAAC;AACP,CAAC;AAND,oCAMC","sourcesContent":["import { setModelDirection } from '../../modelApi/block/setModelDirection';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Set text direction of selected paragraphs (Left to right or Right to left)\n * @param editor The editor to set alignment\n * @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)\n */\nexport function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto') {\n editor.focus();\n\n editor.formatContentModel(model => setModelDirection(model, direction), {\n apiName: 'setDirection',\n });\n}\n"]}
|
|
@@ -2,4 +2,4 @@ import type { ReadonlyContentModelDocument } from 'roosterjs-content-model-types
|
|
|
2
2
|
/**
|
|
3
3
|
* @internal
|
|
4
4
|
*/
|
|
5
|
-
export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl'): boolean;
|
|
5
|
+
export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl' | 'auto'): boolean;
|
|
@@ -2,6 +2,11 @@ define(["require", "exports", "../list/findListItemsInSameThread", "./splitSelec
|
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.setModelDirection = void 0;
|
|
5
|
+
// Regexes for character direction detection
|
|
6
|
+
// Strongly typed RTL character ranges. Referenced unicode's DerivedBidiClass.txt, excluding things in the 2 bit range.
|
|
7
|
+
var RTL_CHAR_REGEX = /[\u0590-\u05FF\u0600-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/g;
|
|
8
|
+
var URL_CHAR_REGEX = /http\S+|www\S+|https\S+|<a\s+(?:[^>]*?\s+)?href=(["']).*?\1.*?>.*?<\/a>/g;
|
|
9
|
+
var WHITESPACE_REGEX = /\s/g;
|
|
5
10
|
/**
|
|
6
11
|
* @internal
|
|
7
12
|
*/
|
|
@@ -10,18 +15,25 @@ define(["require", "exports", "../list/findListItemsInSameThread", "./splitSelec
|
|
|
10
15
|
var paragraphOrListItemOrTable = (0, roosterjs_content_model_dom_1.getOperationalBlocks)(model, ['ListItem'], ['TableCell']);
|
|
11
16
|
paragraphOrListItemOrTable.forEach(function (_a) {
|
|
12
17
|
var block = _a.block;
|
|
18
|
+
var calcDirection;
|
|
19
|
+
if (direction === 'auto') {
|
|
20
|
+
calcDirection = determineTextDirection(block);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
calcDirection = direction;
|
|
24
|
+
}
|
|
13
25
|
if ((0, roosterjs_content_model_dom_1.isBlockGroupOfType)(block, 'ListItem')) {
|
|
14
26
|
var items = (0, findListItemsInSameThread_1.findListItemsInSameThread)(model, block);
|
|
15
27
|
items.forEach(function (readonlyItem) {
|
|
16
28
|
var item = (0, roosterjs_content_model_dom_1.mutateBlock)(readonlyItem);
|
|
17
29
|
item.levels.forEach(function (level) {
|
|
18
|
-
level.format.direction =
|
|
30
|
+
level.format.direction = calcDirection;
|
|
19
31
|
});
|
|
20
|
-
item.blocks.forEach(function (block) { return internalSetDirection(block,
|
|
32
|
+
item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
|
|
21
33
|
});
|
|
22
34
|
}
|
|
23
35
|
else if (block) {
|
|
24
|
-
internalSetDirection(block,
|
|
36
|
+
internalSetDirection(block, calcDirection);
|
|
25
37
|
}
|
|
26
38
|
});
|
|
27
39
|
return paragraphOrListItemOrTable.length > 0;
|
|
@@ -68,5 +80,30 @@ define(["require", "exports", "../list/findListItemsInSameThread", "./splitSelec
|
|
|
68
80
|
delete format[key];
|
|
69
81
|
}
|
|
70
82
|
}
|
|
83
|
+
// Designed to match browser's 'auto' detection, by scanning over the inner text until it hits a strong LTR/RTL character
|
|
84
|
+
function determineTextDirection(block) {
|
|
85
|
+
if (block.blockType === 'Paragraph') {
|
|
86
|
+
var findTextSegements = block.segments.filter(function (seg) { return seg.segmentType === 'Text'; });
|
|
87
|
+
var innerText = findTextSegements.length > 0
|
|
88
|
+
? findTextSegements.reduce(function (prev, seg) { return prev + seg.text; }, '')
|
|
89
|
+
: undefined;
|
|
90
|
+
if (!!innerText) {
|
|
91
|
+
// Remove links
|
|
92
|
+
innerText = innerText.replace(URL_CHAR_REGEX, '');
|
|
93
|
+
// Remove whitespace
|
|
94
|
+
innerText = innerText.replace(WHITESPACE_REGEX, '');
|
|
95
|
+
var rtlMatches = innerText.match(RTL_CHAR_REGEX);
|
|
96
|
+
var rtlCount = rtlMatches ? rtlMatches.length : 0;
|
|
97
|
+
var ltrCount = innerText.length - rtlCount;
|
|
98
|
+
return rtlCount > ltrCount ? 'rtl' : 'ltr';
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
return 'ltr'; // Default to LTR if no text is found
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
return 'ltr';
|
|
106
|
+
}
|
|
107
|
+
}
|
|
71
108
|
});
|
|
72
109
|
//# sourceMappingURL=setModelDirection.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelDirection.ts"],"names":[],"mappings":";;;;
|
|
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,EAAE,UAAU,CAAC,EAAE;gBAC7D,IAAM,KAAK,GAAG,IAAA,qDAAyB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC;gBAEtD,KAAK,CAAC,OAAO,CAAC,UAAA,YAAY;oBACtB,IAAM,IAAI,GAAG,IAAA,yCAAW,EAAC,YAAY,CAAC,CAAC;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GACT,iBAAiB,CAAC,MAAM,GAAG,CAAC;gBACxB,CAAC,CAAC,iBAAiB,CAAC,MAAM,CAAC,UAAC,IAAI,EAAE,GAAG,IAAK,OAAA,IAAI,GAAG,GAAG,CAAC,IAAI,EAAf,CAAe,EAAE,EAAE,CAAC;gBAC9D,CAAC,CAAC,SAAS,CAAC;YACpB,IAAI,CAAC,CAAC,SAAS,EAAE;gBACb,eAAe;gBACf,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;gBAElD,oBAAoB;gBACpB,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,gBAAgB,EAAE,EAAE,CAAC,CAAC;gBAEpD,IAAM,UAAU,GAAG,SAAS,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;gBACnD,IAAM,QAAQ,GAAG,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"]}
|
|
@@ -2,6 +2,6 @@ import type { IEditor } from 'roosterjs-content-model-types';
|
|
|
2
2
|
/**
|
|
3
3
|
* Set text direction of selected paragraphs (Left to right or Right to left)
|
|
4
4
|
* @param editor The editor to set alignment
|
|
5
|
-
* @param direction Direction value: ltr (Left to right) or rtl (Right to left)
|
|
5
|
+
* @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)
|
|
6
6
|
*/
|
|
7
|
-
export declare function setDirection(editor: IEditor, direction: 'ltr' | 'rtl'): void;
|
|
7
|
+
export declare function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto'): void;
|
|
@@ -5,7 +5,7 @@ define(["require", "exports", "../../modelApi/block/setModelDirection"], functio
|
|
|
5
5
|
/**
|
|
6
6
|
* Set text direction of selected paragraphs (Left to right or Right to left)
|
|
7
7
|
* @param editor The editor to set alignment
|
|
8
|
-
* @param direction Direction value: ltr (Left to right) or rtl (Right to left)
|
|
8
|
+
* @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)
|
|
9
9
|
*/
|
|
10
10
|
function setDirection(editor, direction) {
|
|
11
11
|
editor.focus();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setDirection.ts"],"names":[],"mappings":";;;;IAGA;;;;OAIG;IACH,SAAgB,YAAY,CAAC,MAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"setDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setDirection.ts"],"names":[],"mappings":";;;;IAGA;;;;OAIG;IACH,SAAgB,YAAY,CAAC,MAAe,EAAE,SAAiC;QAC3E,MAAM,CAAC,KAAK,EAAE,CAAC;QAEf,MAAM,CAAC,kBAAkB,CAAC,UAAA,KAAK,IAAI,OAAA,IAAA,qCAAiB,EAAC,KAAK,EAAE,SAAS,CAAC,EAAnC,CAAmC,EAAE;YACpE,OAAO,EAAE,cAAc;SAC1B,CAAC,CAAC;IACP,CAAC;IAND,oCAMC","sourcesContent":["import { setModelDirection } from '../../modelApi/block/setModelDirection';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Set text direction of selected paragraphs (Left to right or Right to left)\n * @param editor The editor to set alignment\n * @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)\n */\nexport function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto') {\n editor.focus();\n\n editor.formatContentModel(model => setModelDirection(model, direction), {\n apiName: 'setDirection',\n });\n}\n"]}
|
|
@@ -2,4 +2,4 @@ import type { ReadonlyContentModelDocument } from 'roosterjs-content-model-types
|
|
|
2
2
|
/**
|
|
3
3
|
* @internal
|
|
4
4
|
*/
|
|
5
|
-
export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl'): boolean;
|
|
5
|
+
export declare function setModelDirection(model: ReadonlyContentModelDocument, direction: 'ltr' | 'rtl' | 'auto'): boolean;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { findListItemsInSameThread } from '../list/findListItemsInSameThread';
|
|
2
2
|
import { splitSelectedParagraphByBr } from './splitSelectedParagraphByBr';
|
|
3
3
|
import { applyTableFormat, getOperationalBlocks, isBlockGroupOfType, mutateBlock, updateTableCellMetadata, } from 'roosterjs-content-model-dom';
|
|
4
|
+
// Regexes for character direction detection
|
|
5
|
+
// Strongly typed RTL character ranges. Referenced unicode's DerivedBidiClass.txt, excluding things in the 2 bit range.
|
|
6
|
+
var RTL_CHAR_REGEX = /[\u0590-\u05FF\u0600-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/g;
|
|
7
|
+
var URL_CHAR_REGEX = /http\S+|www\S+|https\S+|<a\s+(?:[^>]*?\s+)?href=(["']).*?\1.*?>.*?<\/a>/g;
|
|
8
|
+
var WHITESPACE_REGEX = /\s/g;
|
|
4
9
|
/**
|
|
5
10
|
* @internal
|
|
6
11
|
*/
|
|
@@ -9,18 +14,25 @@ export function setModelDirection(model, direction) {
|
|
|
9
14
|
var paragraphOrListItemOrTable = getOperationalBlocks(model, ['ListItem'], ['TableCell']);
|
|
10
15
|
paragraphOrListItemOrTable.forEach(function (_a) {
|
|
11
16
|
var block = _a.block;
|
|
17
|
+
var calcDirection;
|
|
18
|
+
if (direction === 'auto') {
|
|
19
|
+
calcDirection = determineTextDirection(block);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
calcDirection = direction;
|
|
23
|
+
}
|
|
12
24
|
if (isBlockGroupOfType(block, 'ListItem')) {
|
|
13
25
|
var items = findListItemsInSameThread(model, block);
|
|
14
26
|
items.forEach(function (readonlyItem) {
|
|
15
27
|
var item = mutateBlock(readonlyItem);
|
|
16
28
|
item.levels.forEach(function (level) {
|
|
17
|
-
level.format.direction =
|
|
29
|
+
level.format.direction = calcDirection;
|
|
18
30
|
});
|
|
19
|
-
item.blocks.forEach(function (block) { return internalSetDirection(block,
|
|
31
|
+
item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
|
|
20
32
|
});
|
|
21
33
|
}
|
|
22
34
|
else if (block) {
|
|
23
|
-
internalSetDirection(block,
|
|
35
|
+
internalSetDirection(block, calcDirection);
|
|
24
36
|
}
|
|
25
37
|
});
|
|
26
38
|
return paragraphOrListItemOrTable.length > 0;
|
|
@@ -66,4 +78,29 @@ function setProperty(format, key, value) {
|
|
|
66
78
|
delete format[key];
|
|
67
79
|
}
|
|
68
80
|
}
|
|
81
|
+
// Designed to match browser's 'auto' detection, by scanning over the inner text until it hits a strong LTR/RTL character
|
|
82
|
+
function determineTextDirection(block) {
|
|
83
|
+
if (block.blockType === 'Paragraph') {
|
|
84
|
+
var findTextSegements = block.segments.filter(function (seg) { return seg.segmentType === 'Text'; });
|
|
85
|
+
var innerText = findTextSegements.length > 0
|
|
86
|
+
? findTextSegements.reduce(function (prev, seg) { return prev + seg.text; }, '')
|
|
87
|
+
: undefined;
|
|
88
|
+
if (!!innerText) {
|
|
89
|
+
// Remove links
|
|
90
|
+
innerText = innerText.replace(URL_CHAR_REGEX, '');
|
|
91
|
+
// Remove whitespace
|
|
92
|
+
innerText = innerText.replace(WHITESPACE_REGEX, '');
|
|
93
|
+
var rtlMatches = innerText.match(RTL_CHAR_REGEX);
|
|
94
|
+
var rtlCount = rtlMatches ? rtlMatches.length : 0;
|
|
95
|
+
var ltrCount = innerText.length - rtlCount;
|
|
96
|
+
return rtlCount > ltrCount ? 'rtl' : 'ltr';
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
return 'ltr'; // Default to LTR if no text is found
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
return 'ltr';
|
|
104
|
+
}
|
|
105
|
+
}
|
|
69
106
|
//# sourceMappingURL=setModelDirection.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setModelDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/modelApi/block/setModelDirection.ts"],"names":[],"mappings":"AAAA,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;
|
|
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,SAA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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"]}
|
|
@@ -2,6 +2,6 @@ import type { IEditor } from 'roosterjs-content-model-types';
|
|
|
2
2
|
/**
|
|
3
3
|
* Set text direction of selected paragraphs (Left to right or Right to left)
|
|
4
4
|
* @param editor The editor to set alignment
|
|
5
|
-
* @param direction Direction value: ltr (Left to right) or rtl (Right to left)
|
|
5
|
+
* @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)
|
|
6
6
|
*/
|
|
7
|
-
export declare function setDirection(editor: IEditor, direction: 'ltr' | 'rtl'): void;
|
|
7
|
+
export declare function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto'): void;
|
|
@@ -2,7 +2,7 @@ import { setModelDirection } from '../../modelApi/block/setModelDirection';
|
|
|
2
2
|
/**
|
|
3
3
|
* Set text direction of selected paragraphs (Left to right or Right to left)
|
|
4
4
|
* @param editor The editor to set alignment
|
|
5
|
-
* @param direction Direction value: ltr (Left to right) or rtl (Right to left)
|
|
5
|
+
* @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)
|
|
6
6
|
*/
|
|
7
7
|
export function setDirection(editor, direction) {
|
|
8
8
|
editor.focus();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setDirection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAC;AAG3E;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,MAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"setDirection.js","sourceRoot":"","sources":["../../../../../packages/roosterjs-content-model-api/lib/publicApi/block/setDirection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAC;AAG3E;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,MAAe,EAAE,SAAiC;IAC3E,MAAM,CAAC,KAAK,EAAE,CAAC;IAEf,MAAM,CAAC,kBAAkB,CAAC,UAAA,KAAK,IAAI,OAAA,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,EAAnC,CAAmC,EAAE;QACpE,OAAO,EAAE,cAAc;KAC1B,CAAC,CAAC;AACP,CAAC","sourcesContent":["import { setModelDirection } from '../../modelApi/block/setModelDirection';\nimport type { IEditor } from 'roosterjs-content-model-types';\n\n/**\n * Set text direction of selected paragraphs (Left to right or Right to left)\n * @param editor The editor to set alignment\n * @param direction Direction value: ltr (Left to right) or rtl (Right to left), or 'auto' (Based on the first characters of the document, set the direction automatically)\n */\nexport function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto') {\n editor.focus();\n\n editor.formatContentModel(model => setModelDirection(model, direction), {\n apiName: 'setDirection',\n });\n}\n"]}
|
package/package.json
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
"description": "Format API for roosterjs",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"tslib": "^2.3.1",
|
|
6
|
-
"roosterjs-content-model-dom": "^9.
|
|
7
|
-
"roosterjs-content-model-types": "^9.
|
|
6
|
+
"roosterjs-content-model-dom": "^9.32.0",
|
|
7
|
+
"roosterjs-content-model-types": "^9.32.0"
|
|
8
8
|
},
|
|
9
|
-
"version": "9.
|
|
9
|
+
"version": "9.32.0",
|
|
10
10
|
"main": "./lib/index.js",
|
|
11
11
|
"typings": "./lib/index.d.ts",
|
|
12
12
|
"module": "./lib-mjs/index.js",
|