roosterjs 9.31.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/dist/rooster.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Type definitions for roosterjs (Version 9.31.0)
1
+ // Type definitions for roosterjs (Version 9.32.0)
2
2
  // Generated by dts tool from roosterjs
3
3
  // Project: https://github.com/Microsoft/roosterjs
4
4
 
@@ -8485,9 +8485,9 @@ function setAlignment(editor: IEditor, alignment: 'left' | 'center' | 'right' |
8485
8485
  /**
8486
8486
  * Set text direction of selected paragraphs (Left to right or Right to left)
8487
8487
  * @param editor The editor to set alignment
8488
- * @param direction Direction value: ltr (Left to right) or rtl (Right to left)
8488
+ * @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)
8489
8489
  */
8490
- function setDirection(editor: IEditor, direction: 'ltr' | 'rtl'): void;
8490
+ function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto'): void;
8491
8491
 
8492
8492
  /**
8493
8493
  * Set heading level of selected paragraphs
package/dist/rooster.js CHANGED
@@ -2313,6 +2313,11 @@ exports.setModelDirection = void 0;
2313
2313
  var findListItemsInSameThread_1 = __webpack_require__(/*! ../list/findListItemsInSameThread */ "./packages/roosterjs-content-model-api/lib/modelApi/list/findListItemsInSameThread.ts");
2314
2314
  var splitSelectedParagraphByBr_1 = __webpack_require__(/*! ./splitSelectedParagraphByBr */ "./packages/roosterjs-content-model-api/lib/modelApi/block/splitSelectedParagraphByBr.ts");
2315
2315
  var roosterjs_content_model_dom_1 = __webpack_require__(/*! roosterjs-content-model-dom */ "./packages/roosterjs-content-model-dom/lib/index.ts");
2316
+ // Regexes for character direction detection
2317
+ // Strongly typed RTL character ranges. Referenced unicode's DerivedBidiClass.txt, excluding things in the 2 bit range.
2318
+ var RTL_CHAR_REGEX = /[\u0590-\u05FF\u0600-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/g;
2319
+ var URL_CHAR_REGEX = /http\S+|www\S+|https\S+|<a\s+(?:[^>]*?\s+)?href=(["']).*?\1.*?>.*?<\/a>/g;
2320
+ var WHITESPACE_REGEX = /\s/g;
2316
2321
  /**
2317
2322
  * @internal
2318
2323
  */
@@ -2321,18 +2326,25 @@ function setModelDirection(model, direction) {
2321
2326
  var paragraphOrListItemOrTable = (0, roosterjs_content_model_dom_1.getOperationalBlocks)(model, ['ListItem'], ['TableCell']);
2322
2327
  paragraphOrListItemOrTable.forEach(function (_a) {
2323
2328
  var block = _a.block;
2329
+ var calcDirection;
2330
+ if (direction === 'auto') {
2331
+ calcDirection = determineTextDirection(block);
2332
+ }
2333
+ else {
2334
+ calcDirection = direction;
2335
+ }
2324
2336
  if ((0, roosterjs_content_model_dom_1.isBlockGroupOfType)(block, 'ListItem')) {
2325
2337
  var items = (0, findListItemsInSameThread_1.findListItemsInSameThread)(model, block);
2326
2338
  items.forEach(function (readonlyItem) {
2327
2339
  var item = (0, roosterjs_content_model_dom_1.mutateBlock)(readonlyItem);
2328
2340
  item.levels.forEach(function (level) {
2329
- level.format.direction = direction;
2341
+ level.format.direction = calcDirection;
2330
2342
  });
2331
- item.blocks.forEach(function (block) { return internalSetDirection(block, direction); });
2343
+ item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
2332
2344
  });
2333
2345
  }
2334
2346
  else if (block) {
2335
- internalSetDirection(block, direction);
2347
+ internalSetDirection(block, calcDirection);
2336
2348
  }
2337
2349
  });
2338
2350
  return paragraphOrListItemOrTable.length > 0;
@@ -2379,6 +2391,31 @@ function setProperty(format, key, value) {
2379
2391
  delete format[key];
2380
2392
  }
2381
2393
  }
2394
+ // Designed to match browser's 'auto' detection, by scanning over the inner text until it hits a strong LTR/RTL character
2395
+ function determineTextDirection(block) {
2396
+ if (block.blockType === 'Paragraph') {
2397
+ var findTextSegements = block.segments.filter(function (seg) { return seg.segmentType === 'Text'; });
2398
+ var innerText = findTextSegements.length > 0
2399
+ ? findTextSegements.reduce(function (prev, seg) { return prev + seg.text; }, '')
2400
+ : undefined;
2401
+ if (!!innerText) {
2402
+ // Remove links
2403
+ innerText = innerText.replace(URL_CHAR_REGEX, '');
2404
+ // Remove whitespace
2405
+ innerText = innerText.replace(WHITESPACE_REGEX, '');
2406
+ var rtlMatches = innerText.match(RTL_CHAR_REGEX);
2407
+ var rtlCount = rtlMatches ? rtlMatches.length : 0;
2408
+ var ltrCount = innerText.length - rtlCount;
2409
+ return rtlCount > ltrCount ? 'rtl' : 'ltr';
2410
+ }
2411
+ else {
2412
+ return 'ltr'; // Default to LTR if no text is found
2413
+ }
2414
+ }
2415
+ else {
2416
+ return 'ltr';
2417
+ }
2418
+ }
2382
2419
 
2383
2420
 
2384
2421
  /***/ }),
@@ -4884,7 +4921,7 @@ var setModelDirection_1 = __webpack_require__(/*! ../../modelApi/block/setModelD
4884
4921
  /**
4885
4922
  * Set text direction of selected paragraphs (Left to right or Right to left)
4886
4923
  * @param editor The editor to set alignment
4887
- * @param direction Direction value: ltr (Left to right) or rtl (Right to left)
4924
+ * @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)
4888
4925
  */
4889
4926
  function setDirection(editor, direction) {
4890
4927
  editor.focus();
@@ -31873,7 +31910,7 @@ function keyboardEnter(editor, rawEvent, handleNormalEnter) {
31873
31910
  var steps = rawEvent.shiftKey
31874
31911
  ? []
31875
31912
  : [handleAutoLink_1.handleAutoLink, handleEnterOnList_1.handleEnterOnList, deleteEmptyQuote_1.deleteEmptyQuote];
31876
- if (handleNormalEnter || hasEnterForEntity((_a = result.insertPoint) === null || _a === void 0 ? void 0 : _a.paragraph)) {
31913
+ if (handleNormalEnter || handleEnterForEntity((_a = result.insertPoint) === null || _a === void 0 ? void 0 : _a.paragraph)) {
31877
31914
  steps.push(handleEnterOnParagraph_1.handleEnterOnParagraph);
31878
31915
  }
31879
31916
  (0, roosterjs_content_model_dom_1.runEditSteps)(steps, result);
@@ -31897,9 +31934,9 @@ function keyboardEnter(editor, rawEvent, handleNormalEnter) {
31897
31934
  });
31898
31935
  }
31899
31936
  exports.keyboardEnter = keyboardEnter;
31900
- function hasEnterForEntity(paragraph) {
31937
+ function handleEnterForEntity(paragraph) {
31901
31938
  return (paragraph &&
31902
- (paragraph.isImplicit || paragraph.segments.some(function (x) { return x.segmentType == 'SelectionMarker'; })));
31939
+ (paragraph.isImplicit || paragraph.segments.some(function (x) { return x.segmentType == 'Entity'; })));
31903
31940
  }
31904
31941
 
31905
31942