@plait/common 0.62.0-next.0 → 0.62.0-next.10

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
- import { PlaitGroupElement, getSelectionAngle, getElementsInGroup, setAngleForG, RectangleClient, drawCircle, PlaitBoard, RESIZE_HANDLE_CLASS_NAME, createG, setStrokeLinecap, drawRectangle, SELECTION_RECTANGLE_CLASS_NAME, ResizeCursorClass, RESIZE_CURSORS, setDragging, rotatePoints, isSelectionMoving, getSelectedElements, distanceBetweenPointAndPoint, Point, Direction, hotkeys, createDebugGenerator, createForeignObject, ACTIVE_STROKE_WIDTH, updateForeignObject, Transforms, getHighestSelectedElements, getRectangleByElements, MERGING, PlaitPointerType, isMainPointer, toViewBoxPoint, toHostPoint, preventTouchMove, PRESS_AND_MOVE_BUFFER, isDragging, throttleRAF, handleTouchTarget, getRectangleByGroup, ElementFlavour, isSelectedElementOrGroup, Selection, getHitElementsBySelection, createGroupRectangleG, getSelectedGroups, getHighestSelectedGroups, getSelectedIsolatedElements, idCreator, getSelectedIsolatedElementsCanAddToGroup, getGroupByElement, updateForeignObjectWidth, IS_TEXT_EDITABLE } from '@plait/core';
1
+ import { PlaitGroupElement, getSelectionAngle, getElementsInGroup, setAngleForG, RectangleClient, drawCircle, PlaitBoard, RESIZE_HANDLE_CLASS_NAME, createG, setStrokeLinecap, drawRectangle, SELECTION_RECTANGLE_CLASS_NAME, ResizeCursorClass, RESIZE_CURSORS, setDragging, rotatePoints, isSelectionMoving, getSelectedElements, distanceBetweenPointAndPoint, Point, Direction, hotkeys, createDebugGenerator, createForeignObject, ACTIVE_STROKE_WIDTH, updateForeignObject, Transforms, getHighestSelectedElements, getRectangleByElements, MERGING, PlaitPointerType, isMainPointer, toViewBoxPoint, toHostPoint, preventTouchMove, PRESS_AND_MOVE_BUFFER, isDragging, throttleRAF, handleTouchTarget, getRectangleByGroup, ElementFlavour, isSelectedElementOrGroup, Selection, getHitElementsBySelection, createGroupRectangleG, getSelectedGroups, getHighestSelectedGroups, getSelectedIsolatedElements, idCreator, getSelectedIsolatedElementsCanAddToGroup, getGroupByElement, debounce, updateForeignObjectWidth, IS_TEXT_EDITABLE } from '@plait/core';
2
2
  import { isKeyHotkey } from 'is-hotkey';
3
3
  import { Node, Operation, Transforms as Transforms$1, Range, Editor } from 'slate';
4
4
  import { fromEvent, timer } from 'rxjs';
@@ -12,6 +12,7 @@ const TRANSPARENT = 'transparent';
12
12
  const ROTATE_HANDLE_DISTANCE_TO_ELEMENT = 20;
13
13
  const ROTATE_HANDLE_SIZE = 18;
14
14
  const DEFAULT_FONT_FAMILY = 'PingFangSC-Regular, "PingFang SC"';
15
+ const DEFAULT_FILL = 'none';
15
16
 
16
17
  var MediaKeys;
17
18
  (function (MediaKeys) {
@@ -1226,6 +1227,63 @@ const removeRotating = (board) => {
1226
1227
  setDragging(board, false);
1227
1228
  };
1228
1229
 
1230
+ const getElementArea = (board, element) => {
1231
+ const rectangle = board.getRectangle(element);
1232
+ if (rectangle) {
1233
+ return rectangle.width * rectangle.height;
1234
+ }
1235
+ return 0;
1236
+ };
1237
+ const sortElementsByArea = (board, elements, direction = 'asc') => {
1238
+ return elements.sort((a, b) => {
1239
+ const areaA = getElementArea(board, a);
1240
+ const areaB = getElementArea(board, b);
1241
+ return direction === 'asc' ? areaA - areaB : areaB - areaA;
1242
+ });
1243
+ };
1244
+ const isFilled = (fill) => {
1245
+ return fill && fill !== DEFAULT_FILL && fill !== TRANSPARENT;
1246
+ };
1247
+
1248
+ function animate(tween, duration, ease, callback) {
1249
+ const start = getTimestamp();
1250
+ let stopAnimation = false;
1251
+ function tick(now) {
1252
+ if (stopAnimation) {
1253
+ return;
1254
+ }
1255
+ const elapsed = now - start;
1256
+ const t = Math.min(elapsed / duration, 1);
1257
+ tween(ease(t));
1258
+ if (t < 1) {
1259
+ requestAnimationFrame(tick);
1260
+ }
1261
+ else if (callback) {
1262
+ callback();
1263
+ }
1264
+ }
1265
+ requestAnimationFrame(tick);
1266
+ return {
1267
+ stop: () => (stopAnimation = true),
1268
+ start: () => {
1269
+ stopAnimation = false;
1270
+ requestAnimationFrame(tick);
1271
+ }
1272
+ };
1273
+ }
1274
+ function getTimestamp() {
1275
+ if (window.performance && window.performance.now) {
1276
+ return window.performance.now();
1277
+ }
1278
+ else {
1279
+ return Date.now();
1280
+ }
1281
+ }
1282
+ function linear(t) {
1283
+ return t;
1284
+ }
1285
+
1286
+ const FOREIGN_OBJECT_IMAGE_CLASS_NAME = 'foreign-object-image';
1229
1287
  class ImageGenerator extends Generator {
1230
1288
  static { this.key = 'image-generator'; }
1231
1289
  constructor(board, options) {
@@ -1242,6 +1300,7 @@ class ImageGenerator extends Generator {
1242
1300
  const g = createG();
1243
1301
  const foreignRectangle = this.options.getRectangle(element);
1244
1302
  this.foreignObject = createForeignObject(foreignRectangle.x, foreignRectangle.y, foreignRectangle.width, foreignRectangle.height);
1303
+ this.foreignObject.classList.add(FOREIGN_OBJECT_IMAGE_CLASS_NAME);
1245
1304
  g.append(this.foreignObject);
1246
1305
  const props = {
1247
1306
  board: this.board,
@@ -1886,29 +1945,12 @@ const updateSiblingElementGroupId = (board, removeGroups) => {
1886
1945
  };
1887
1946
 
1888
1947
  class ImageBaseComponent {
1889
- constructor() {
1890
- this.initialized = false;
1891
- }
1892
- set imageItem(value) {
1893
- this._imageItem = value;
1894
- if (this.initialized) {
1895
- this.afterImageItemChange(this._imageItem, value);
1896
- }
1897
- }
1898
- get imageItem() {
1899
- return this._imageItem;
1900
- }
1901
1948
  set isFocus(value) {
1902
1949
  this._isFocus = value;
1903
1950
  }
1904
1951
  get isFocus() {
1905
1952
  return this._isFocus;
1906
1953
  }
1907
- initialize() {
1908
- this.initialized = true;
1909
- }
1910
- destroy() {
1911
- }
1912
1954
  }
1913
1955
 
1914
1956
  function measureElement(element, options, containerMaxWidth = 10000) {
@@ -1975,6 +2017,15 @@ class TextManage {
1975
2017
  this.board = board;
1976
2018
  this.options = options;
1977
2019
  this.isEditing = false;
2020
+ // add debounce to avoid trigger more times(from onChange and onComposition) onChange when user is typing chinese
2021
+ // be going to attract board children are overwritten when fired more times onChange(eg: board is embed in editor)
2022
+ this.textChange = debounce((data) => {
2023
+ if (!data) {
2024
+ return;
2025
+ }
2026
+ this.options.onChange && this.options.onChange({ ...data });
2027
+ MERGING.set(this.board, true);
2028
+ }, 0);
1978
2029
  this.getSize = (element) => {
1979
2030
  const computedStyle = window.getComputedStyle(this.foreignObject.children[0]);
1980
2031
  const fontFamily = computedStyle.fontFamily;
@@ -2005,8 +2056,7 @@ class TextManage {
2005
2056
  onChange: (data) => {
2006
2057
  if (data.operations.some(op => !Operation.isSelectionOperation(op))) {
2007
2058
  const { width, height } = this.getSize();
2008
- this.options.onChange && this.options.onChange({ ...data, width, height });
2009
- MERGING.set(this.board, true);
2059
+ this.textChange({ ...data, width, height });
2010
2060
  }
2011
2061
  },
2012
2062
  afterInit: (editor) => {
@@ -2016,8 +2066,7 @@ class TextManage {
2016
2066
  const fakeRoot = buildCompositionData(this.editor, event.data);
2017
2067
  if (fakeRoot) {
2018
2068
  const sizeData = this.getSize(fakeRoot.children[0]);
2019
- this.options.onChange && this.options.onChange(sizeData);
2020
- MERGING.set(this.board, true);
2069
+ this.textChange(sizeData);
2021
2070
  }
2022
2071
  }
2023
2072
  };
@@ -2039,7 +2088,7 @@ class TextManage {
2039
2088
  };
2040
2089
  this.textComponentRef.update(props);
2041
2090
  }
2042
- edit(callback) {
2091
+ edit(callback, exitEdit) {
2043
2092
  this.isEditing = true;
2044
2093
  IS_TEXT_EDITABLE.set(this.board, true);
2045
2094
  const props = {
@@ -2063,7 +2112,7 @@ class TextManage {
2063
2112
  if (event.isComposing) {
2064
2113
  return;
2065
2114
  }
2066
- if (event.key === 'Escape' || (event.key === 'Enter' && !event.shiftKey) || event.key === 'Tab') {
2115
+ if (event.key === 'Escape' || event.key === 'Tab' || (exitEdit ? exitEdit(event) : false)) {
2067
2116
  event.preventDefault();
2068
2117
  event.stopPropagation();
2069
2118
  exitCallback();
@@ -2135,5 +2184,5 @@ const withImage = (board) => {
2135
2184
  * Generated bundle index. Do not edit.
2136
2185
  */
2137
2186
 
2138
- export { AStar, ActiveGenerator, AlignTransform, Alignment, BASE, BoardCreationMode, CommonElementFlavour, DEFAULT_FONT_FAMILY, DEFAULT_ROUTE_MARGIN, ELEMENT_TO_TEXT_MANAGES, Generator, GroupComponent, IS_RESIZING, IS_ROTATING, ImageBaseComponent, ImageGenerator, MediaKeys, PICTURE_ACCEPTED_UPLOAD_SIZE, PRIMARY_COLOR, PlaitCommonElementRef, PointGraph, PointNode, PriorityQueue, PropertyTransforms, RESIZE_HANDLE_DIAMETER, ROTATE_HANDLE_DISTANCE_TO_ELEMENT, ROTATE_HANDLE_SIZE, ResizeHandle, TRANSPARENT, TextManage, WithCommonPluginKey, WithTextPluginKey, acceptImageTypes, addElementOfFocusedImage, addResizing, addRotating, alignBottom, alignHorizontalCenter, alignLeft, alignRight, alignTop, alignVerticalCenter, buildCompositionData, buildImage, buildText, calculatePolylineLength, canResize, createGraph, distributeHorizontal, distributeVertical, drawFillPrimaryHandle, drawHandle, drawPrimaryHandle, drawRotateHandle, findFirstTextEditor, generateElbowLineRoute, getCreationMode, getCrossingPointsBetweenPointAndSegment, getDirectionBetweenPointAndPoint, getDirectionByPointOfRectangle, getDirectionByVector, getDirectionFactor, getDirectionFactorByDirectionComponent, getEditingTextEditor, getElementOfFocusedImage, getElementsText, getExtendPoint, getFirstTextEditor, getFirstTextManage, getGraphPoints, getIndexByResizeHandle, getLineHeightByFontSize, getMemorizedLatest, getNextPoint, getOppositeDirection, getPointByVectorComponent, getPointByVectorDirectionComponent, getPointOnPolyline, getPoints, getRatioByPoint, getRectangleResizeHandleRefs, getResizeHandleByIndex, getResizeHandlePointByIndex, getRotatedResizeCursorClassByAngle, getSourceAndTargetOuterRectangle, getSymmetricHandleIndex, getTextEditors, getTextEditorsByElement, getTextManages, getUnitVectorByPointAndPoint, hasAfterDraw, isCornerHandle, isDelete, isDndMode, isDrawingMode, isEdgeHandle, isEnterHotkey, isExpandHotkey, isPointOnSegment, isResizing, isResizingByCondition, isRotating, isSourceAndTargetIntersect, isSpaceHotkey, isTabHotkey, isVirtualKey, measureElement, memorizeLatest, normalizeShapePoints, reduceRouteMargin, removeDuplicatePoints, removeElementOfFocusedImage, removeResizing, removeRotating, resetPointsAfterResize, rotateVector, rotateVectorAnti90, routeAdjust, selectImage, setCreationMode, setProperty, simplifyOrthogonalPoints, withGroup, withImage, withResize, withText };
2187
+ export { AStar, ActiveGenerator, AlignTransform, Alignment, BASE, BoardCreationMode, CommonElementFlavour, DEFAULT_FILL, DEFAULT_FONT_FAMILY, DEFAULT_ROUTE_MARGIN, ELEMENT_TO_TEXT_MANAGES, FOREIGN_OBJECT_IMAGE_CLASS_NAME, Generator, GroupComponent, IS_RESIZING, IS_ROTATING, ImageBaseComponent, ImageGenerator, MediaKeys, PICTURE_ACCEPTED_UPLOAD_SIZE, PRIMARY_COLOR, PlaitCommonElementRef, PointGraph, PointNode, PriorityQueue, PropertyTransforms, RESIZE_HANDLE_DIAMETER, ROTATE_HANDLE_DISTANCE_TO_ELEMENT, ROTATE_HANDLE_SIZE, ResizeHandle, TRANSPARENT, TextManage, WithCommonPluginKey, WithTextPluginKey, acceptImageTypes, addElementOfFocusedImage, addResizing, addRotating, alignBottom, alignHorizontalCenter, alignLeft, alignRight, alignTop, alignVerticalCenter, animate, buildCompositionData, buildImage, buildText, calculatePolylineLength, canResize, createGraph, distributeHorizontal, distributeVertical, drawFillPrimaryHandle, drawHandle, drawPrimaryHandle, drawRotateHandle, findFirstTextEditor, generateElbowLineRoute, getCreationMode, getCrossingPointsBetweenPointAndSegment, getDirectionBetweenPointAndPoint, getDirectionByPointOfRectangle, getDirectionByVector, getDirectionFactor, getDirectionFactorByDirectionComponent, getEditingTextEditor, getElementArea, getElementOfFocusedImage, getElementsText, getExtendPoint, getFirstTextEditor, getFirstTextManage, getGraphPoints, getIndexByResizeHandle, getLineHeightByFontSize, getMemorizedLatest, getNextPoint, getOppositeDirection, getPointByVectorComponent, getPointByVectorDirectionComponent, getPointOnPolyline, getPoints, getRatioByPoint, getRectangleResizeHandleRefs, getResizeHandleByIndex, getResizeHandlePointByIndex, getRotatedResizeCursorClassByAngle, getSourceAndTargetOuterRectangle, getSymmetricHandleIndex, getTextEditors, getTextEditorsByElement, getTextManages, getTimestamp, getUnitVectorByPointAndPoint, hasAfterDraw, isCornerHandle, isDelete, isDndMode, isDrawingMode, isEdgeHandle, isEnterHotkey, isExpandHotkey, isFilled, isPointOnSegment, isResizing, isResizingByCondition, isRotating, isSourceAndTargetIntersect, isSpaceHotkey, isTabHotkey, isVirtualKey, linear, measureElement, memorizeLatest, normalizeShapePoints, reduceRouteMargin, removeDuplicatePoints, removeElementOfFocusedImage, removeResizing, removeRotating, resetPointsAfterResize, rotateVector, rotateVectorAnti90, routeAdjust, selectImage, setCreationMode, setProperty, simplifyOrthogonalPoints, sortElementsByArea, withGroup, withImage, withResize, withText };
2139
2188
  //# sourceMappingURL=plait-common.mjs.map