roosterjs 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.
@@ -1,4 +1,4 @@
1
- // Type definitions for roosterjs (Version 9.30.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
 
@@ -5100,6 +5100,13 @@ export interface Snapshot {
5100
5100
  * HTML content string
5101
5101
  */
5102
5102
  html: string;
5103
+ /**
5104
+ * Additional state supplied by plugins. When doing an undo/redo to this snapshot, this state will be added to the
5105
+ * content model context as additional state.
5106
+ */
5107
+ additionalState?: {
5108
+ [key: string]: string;
5109
+ };
5103
5110
  /**
5104
5111
  * Entity states related to this undo snapshots. When undo/redo to this snapshot, each entity state will trigger
5105
5112
  * an EntityOperation event with operation = EntityOperation.UpdateEntityState
@@ -5873,6 +5880,18 @@ export interface BasePluginDomEvent<TPluginEventType extends PluginEventType, TR
5873
5880
  rawEvent: TRawEvent;
5874
5881
  }
5875
5882
 
5883
+ /**
5884
+ * Fired when an undo snapshot is about to be added
5885
+ */
5886
+ export interface BeforeAddUndoSnapshotEvent extends BasePluginEvent<'beforeAddUndoSnapshot'> {
5887
+ /**
5888
+ * Additional state to be added to the snapshot
5889
+ */
5890
+ additionalState: {
5891
+ [key: string]: string;
5892
+ };
5893
+ }
5894
+
5876
5895
  /**
5877
5896
  * Provides a chance for plugin to change the content before it is copied from editor.
5878
5897
  */
@@ -5980,6 +5999,12 @@ export interface ContentChangedEvent extends BasePluginEvent<'contentChanged'> {
5980
5999
  * Entities got changed (added or removed) during the content change process
5981
6000
  */
5982
6001
  readonly changedEntities?: ChangedEntity[];
6002
+ /**
6003
+ * Additional state added to the snapshot by plugins
6004
+ */
6005
+ readonly additionalState?: {
6006
+ [key: string]: string;
6007
+ };
5983
6008
  /**
5984
6009
  * Entity states related to this event
5985
6010
  */
@@ -6235,7 +6260,7 @@ export interface MouseUpEvent extends BasePluginDomEvent<'mouseUp', MouseEvent>
6235
6260
  /**
6236
6261
  * Editor plugin event interface
6237
6262
  */
6238
- export type PluginEvent = BeforeCutCopyEvent | BeforeDisposeEvent | BeforeKeyboardEditingEvent | BeforeLogicalRootChangeEvent | BeforePasteEvent | BeforeSetContentEvent | CompositionEndEvent | ContentChangedEvent | ContextMenuEvent | RewriteFromModelEvent | EditImageEvent | EditorReadyEvent | EnterShadowEditEvent | EntityOperationEvent | ExtractContentWithDomEvent | EditorInputEvent | KeyDownEvent | KeyPressEvent | KeyUpEvent | LeaveShadowEditEvent | LogicalRootChangedEvent | MouseDownEvent | MouseUpEvent | ScrollEvent | SelectionChangedEvent | ZoomChangedEvent;
6263
+ export type PluginEvent = BeforeAddUndoSnapshotEvent | BeforeCutCopyEvent | BeforeDisposeEvent | BeforeKeyboardEditingEvent | BeforeLogicalRootChangeEvent | BeforePasteEvent | BeforeSetContentEvent | CompositionEndEvent | ContentChangedEvent | ContextMenuEvent | RewriteFromModelEvent | EditImageEvent | EditorReadyEvent | EnterShadowEditEvent | EntityOperationEvent | ExtractContentWithDomEvent | EditorInputEvent | KeyDownEvent | KeyPressEvent | KeyUpEvent | LeaveShadowEditEvent | LogicalRootChangedEvent | MouseDownEvent | MouseUpEvent | ScrollEvent | SelectionChangedEvent | ZoomChangedEvent;
6239
6264
 
6240
6265
  /**
6241
6266
  * A type to extract data part of a plugin event type. Data part is the plugin event without eventType field.
@@ -6375,7 +6400,12 @@ export type PluginEventType = /**
6375
6400
  * This event is used to clean up any features from the old logical root
6376
6401
  * before the new logical root is set.
6377
6402
  */
6378
- | 'beforeLogicalRootChange';
6403
+ | 'beforeLogicalRootChange'
6404
+ /**
6405
+ * Before an undo snapshot is added to the undo stack.
6406
+ * This event is used to give plugins a chance to add additional state to the snapshot.
6407
+ */
6408
+ | 'beforeAddUndoSnapshot';
6379
6409
 
6380
6410
  /**
6381
6411
  * This interface represents a PluginEvent wrapping native scroll event
@@ -8454,9 +8484,9 @@ export function setAlignment(editor: IEditor, alignment: 'left' | 'center' | 'ri
8454
8484
  /**
8455
8485
  * Set text direction of selected paragraphs (Left to right or Right to left)
8456
8486
  * @param editor The editor to set alignment
8457
- * @param direction Direction value: ltr (Left to right) or rtl (Right to left)
8487
+ * @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)
8458
8488
  */
8459
- export function setDirection(editor: IEditor, direction: 'ltr' | 'rtl'): void;
8489
+ export function setDirection(editor: IEditor, direction: 'ltr' | 'rtl' | 'auto'): void;
8460
8490
 
8461
8491
  /**
8462
8492
  * Set heading level of selected paragraphs
@@ -2312,6 +2312,11 @@ exports.setModelDirection = void 0;
2312
2312
  var findListItemsInSameThread_1 = __webpack_require__(/*! ../list/findListItemsInSameThread */ "./packages/roosterjs-content-model-api/lib/modelApi/list/findListItemsInSameThread.ts");
2313
2313
  var splitSelectedParagraphByBr_1 = __webpack_require__(/*! ./splitSelectedParagraphByBr */ "./packages/roosterjs-content-model-api/lib/modelApi/block/splitSelectedParagraphByBr.ts");
2314
2314
  var roosterjs_content_model_dom_1 = __webpack_require__(/*! roosterjs-content-model-dom */ "./packages/roosterjs-content-model-dom/lib/index.ts");
2315
+ // Regexes for character direction detection
2316
+ // Strongly typed RTL character ranges. Referenced unicode's DerivedBidiClass.txt, excluding things in the 2 bit range.
2317
+ var RTL_CHAR_REGEX = /[\u0590-\u05FF\u0600-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/g;
2318
+ var URL_CHAR_REGEX = /http\S+|www\S+|https\S+|<a\s+(?:[^>]*?\s+)?href=(["']).*?\1.*?>.*?<\/a>/g;
2319
+ var WHITESPACE_REGEX = /\s/g;
2315
2320
  /**
2316
2321
  * @internal
2317
2322
  */
@@ -2320,18 +2325,25 @@ function setModelDirection(model, direction) {
2320
2325
  var paragraphOrListItemOrTable = (0, roosterjs_content_model_dom_1.getOperationalBlocks)(model, ['ListItem'], ['TableCell']);
2321
2326
  paragraphOrListItemOrTable.forEach(function (_a) {
2322
2327
  var block = _a.block;
2328
+ var calcDirection;
2329
+ if (direction === 'auto') {
2330
+ calcDirection = determineTextDirection(block);
2331
+ }
2332
+ else {
2333
+ calcDirection = direction;
2334
+ }
2323
2335
  if ((0, roosterjs_content_model_dom_1.isBlockGroupOfType)(block, 'ListItem')) {
2324
2336
  var items = (0, findListItemsInSameThread_1.findListItemsInSameThread)(model, block);
2325
2337
  items.forEach(function (readonlyItem) {
2326
2338
  var item = (0, roosterjs_content_model_dom_1.mutateBlock)(readonlyItem);
2327
2339
  item.levels.forEach(function (level) {
2328
- level.format.direction = direction;
2340
+ level.format.direction = calcDirection;
2329
2341
  });
2330
- item.blocks.forEach(function (block) { return internalSetDirection(block, direction); });
2342
+ item.blocks.forEach(function (block) { return internalSetDirection(block, calcDirection); });
2331
2343
  });
2332
2344
  }
2333
2345
  else if (block) {
2334
- internalSetDirection(block, direction);
2346
+ internalSetDirection(block, calcDirection);
2335
2347
  }
2336
2348
  });
2337
2349
  return paragraphOrListItemOrTable.length > 0;
@@ -2378,6 +2390,31 @@ function setProperty(format, key, value) {
2378
2390
  delete format[key];
2379
2391
  }
2380
2392
  }
2393
+ // Designed to match browser's 'auto' detection, by scanning over the inner text until it hits a strong LTR/RTL character
2394
+ function determineTextDirection(block) {
2395
+ if (block.blockType === 'Paragraph') {
2396
+ var findTextSegements = block.segments.filter(function (seg) { return seg.segmentType === 'Text'; });
2397
+ var innerText = findTextSegements.length > 0
2398
+ ? findTextSegements.reduce(function (prev, seg) { return prev + seg.text; }, '')
2399
+ : undefined;
2400
+ if (!!innerText) {
2401
+ // Remove links
2402
+ innerText = innerText.replace(URL_CHAR_REGEX, '');
2403
+ // Remove whitespace
2404
+ innerText = innerText.replace(WHITESPACE_REGEX, '');
2405
+ var rtlMatches = innerText.match(RTL_CHAR_REGEX);
2406
+ var rtlCount = rtlMatches ? rtlMatches.length : 0;
2407
+ var ltrCount = innerText.length - rtlCount;
2408
+ return rtlCount > ltrCount ? 'rtl' : 'ltr';
2409
+ }
2410
+ else {
2411
+ return 'ltr'; // Default to LTR if no text is found
2412
+ }
2413
+ }
2414
+ else {
2415
+ return 'ltr';
2416
+ }
2417
+ }
2381
2418
 
2382
2419
 
2383
2420
  /***/ }),
@@ -4883,7 +4920,7 @@ var setModelDirection_1 = __webpack_require__(/*! ../../modelApi/block/setModelD
4883
4920
  /**
4884
4921
  * Set text direction of selected paragraphs (Left to right or Right to left)
4885
4922
  * @param editor The editor to set alignment
4886
- * @param direction Direction value: ltr (Left to right) or rtl (Right to left)
4923
+ * @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)
4887
4924
  */
4888
4925
  function setDirection(editor, direction) {
4889
4926
  editor.focus();
@@ -8560,6 +8597,12 @@ var addUndoSnapshot = function (core, canUndoByBackspace, entityStates) {
8560
8597
  var lifecycle = core.lifecycle, physicalRoot = core.physicalRoot, logicalRoot = core.logicalRoot, undo = core.undo;
8561
8598
  var snapshot = null;
8562
8599
  if (!lifecycle.shadowEditFragment) {
8600
+ // Give plugins the chance to add additional state to the snapshot
8601
+ var beforeAddUndoSnapshotEvent = {
8602
+ eventType: 'beforeAddUndoSnapshot',
8603
+ additionalState: {},
8604
+ };
8605
+ core.api.triggerEvent(core, beforeAddUndoSnapshotEvent, false);
8563
8606
  // Need to create snapshot selection before retrieve innerHTML since HTML can be changed during creating selection when normalize table
8564
8607
  var selection = (0, createSnapshotSelection_1.createSnapshotSelection)(core);
8565
8608
  var html = physicalRoot.innerHTML;
@@ -8596,6 +8639,7 @@ var addUndoSnapshot = function (core, canUndoByBackspace, entityStates) {
8596
8639
  }
8597
8640
  snapshot = {
8598
8641
  html: html,
8642
+ additionalState: beforeAddUndoSnapshotEvent.additionalState,
8599
8643
  entityStates: entityStates,
8600
8644
  isDarkMode: !!lifecycle.isDarkMode,
8601
8645
  selection: selection,
@@ -9708,6 +9752,7 @@ var restoreUndoSnapshot = function (core, snapshot) {
9708
9752
  (0, restoreSnapshotColors_1.restoreSnapshotColors)(core, snapshot);
9709
9753
  var event_1 = {
9710
9754
  eventType: 'contentChanged',
9755
+ additionalState: snapshot.additionalState,
9711
9756
  entityStates: snapshot.entityStates,
9712
9757
  source: roosterjs_content_model_dom_1.ChangeSource.SetContent,
9713
9758
  };
@@ -14366,6 +14411,8 @@ var SnapshotsManagerImpl = /** @class */ (function () {
14366
14411
  var currentSnapshot = this.snapshots.snapshots[this.snapshots.currentIndex];
14367
14412
  var isSameSnapshot = currentSnapshot &&
14368
14413
  currentSnapshot.html == snapshot.html &&
14414
+ !currentSnapshot.additionalState &&
14415
+ !snapshot.additionalState &&
14369
14416
  !currentSnapshot.entityStates &&
14370
14417
  !snapshot.entityStates;
14371
14418
  var addSnapshot = !currentSnapshot || shouldAddSnapshot(currentSnapshot, snapshot);
@@ -14431,6 +14478,11 @@ function createSnapshotsManager(snapshots) {
14431
14478
  exports.createSnapshotsManager = createSnapshotsManager;
14432
14479
  function shouldAddSnapshot(currentSnapshot, snapshot) {
14433
14480
  return (currentSnapshot.html !== snapshot.html ||
14481
+ (currentSnapshot.additionalState &&
14482
+ snapshot.additionalState &&
14483
+ JSON.stringify(currentSnapshot.additionalState) !==
14484
+ JSON.stringify(snapshot.additionalState)) ||
14485
+ (!currentSnapshot.additionalState && snapshot.additionalState) ||
14434
14486
  (currentSnapshot.entityStates &&
14435
14487
  snapshot.entityStates &&
14436
14488
  currentSnapshot.entityStates !== snapshot.entityStates) ||
@@ -31857,7 +31909,7 @@ function keyboardEnter(editor, rawEvent, handleNormalEnter) {
31857
31909
  var steps = rawEvent.shiftKey
31858
31910
  ? []
31859
31911
  : [handleAutoLink_1.handleAutoLink, handleEnterOnList_1.handleEnterOnList, deleteEmptyQuote_1.deleteEmptyQuote];
31860
- if (handleNormalEnter || hasEnterForEntity((_a = result.insertPoint) === null || _a === void 0 ? void 0 : _a.paragraph)) {
31912
+ if (handleNormalEnter || handleEnterForEntity((_a = result.insertPoint) === null || _a === void 0 ? void 0 : _a.paragraph)) {
31861
31913
  steps.push(handleEnterOnParagraph_1.handleEnterOnParagraph);
31862
31914
  }
31863
31915
  (0, roosterjs_content_model_dom_1.runEditSteps)(steps, result);
@@ -31881,9 +31933,9 @@ function keyboardEnter(editor, rawEvent, handleNormalEnter) {
31881
31933
  });
31882
31934
  }
31883
31935
  exports.keyboardEnter = keyboardEnter;
31884
- function hasEnterForEntity(paragraph) {
31936
+ function handleEnterForEntity(paragraph) {
31885
31937
  return (paragraph &&
31886
- (paragraph.isImplicit || paragraph.segments.some(function (x) { return x.segmentType == 'SelectionMarker'; })));
31938
+ (paragraph.isImplicit || paragraph.segments.some(function (x) { return x.segmentType == 'Entity'; })));
31887
31939
  }
31888
31940
 
31889
31941