@plait/core 0.0.57 → 0.0.58

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.
@@ -371,81 +371,68 @@ const ViewportTransforms = {
371
371
  setViewport
372
372
  };
373
373
 
374
- const Transforms = {
375
- ...GeneralTransforms,
376
- ...ViewportTransforms,
377
- ...SelectionTransforms,
378
- ...NodeTransforms
379
- };
380
-
381
- // record richtext type status
382
- const FLUSHING = new WeakMap();
383
- const IS_TEXT_EDITABLE = new WeakMap();
384
- const BOARD_TO_ON_CHANGE = new WeakMap();
385
- const HOST_TO_ROUGH_SVG = new WeakMap();
386
- const PLAIT_BOARD_TO_COMPONENT = new WeakMap();
374
+ function transformPoints(board, points) {
375
+ const newPoints = points.map(point => {
376
+ return transformPoint(board, point);
377
+ });
378
+ return newPoints;
379
+ }
380
+ function transformPoint(board, point) {
381
+ const { width, height } = board.host.getBoundingClientRect();
382
+ const viewBox = board.host.viewBox.baseVal;
383
+ const x = (point[0] / width) * viewBox.width + viewBox.x;
384
+ const y = (point[1] / height) * viewBox.height + viewBox.y;
385
+ const newPoint = [x, y];
386
+ return newPoint;
387
+ }
388
+ function isNoSelectionElement(e) {
389
+ return e.target?.closest('.plait-board-attached');
390
+ }
387
391
 
388
- function createBoard(host, children, options) {
389
- const board = {
390
- host,
391
- viewport: {
392
- zoom: 1,
393
- viewBackgroundColor: '#000'
394
- },
395
- children,
396
- operations: [],
397
- history: {
398
- redos: [],
399
- undos: []
400
- },
401
- selection: null,
402
- pointer: PlaitPointerType.selection,
403
- options: options || {
404
- readonly: false,
405
- allowClearBoard: false,
406
- hideScrollbar: false
407
- },
408
- undo: () => { },
409
- redo: () => { },
410
- apply: (operation) => {
411
- board.operations.push(operation);
412
- Transforms.transform(board, operation);
413
- if (!FLUSHING.get(board)) {
414
- FLUSHING.set(board, true);
415
- Promise.resolve().then(() => {
416
- FLUSHING.set(board, false);
417
- board.onChange();
418
- board.operations = [];
419
- });
420
- }
421
- },
422
- onChange: () => { },
423
- mousedown: (event) => { },
424
- globalMouseup: (event) => { },
425
- mousemove: (event) => { },
426
- keydown: (event) => { },
427
- keyup: (event) => { },
428
- dblclick: (event) => { },
429
- setFragment: (data) => { },
430
- insertFragment: (data) => { },
431
- deleteFragment: (data) => { },
432
- drawElement: (context) => [],
433
- redrawElement: (context, previousContext) => { },
434
- destroyElement: (context) => { }
435
- };
436
- return board;
392
+ const NS = 'http://www.w3.org/2000/svg';
393
+ function toPoint(x, y, container) {
394
+ const rect = container.getBoundingClientRect();
395
+ return [x - rect.x, y - rect.y];
396
+ }
397
+ function createG() {
398
+ const newG = document.createElementNS(NS, 'g');
399
+ return newG;
400
+ }
401
+ function createSVG() {
402
+ const svg = document.createElementNS(NS, 'svg');
403
+ return svg;
404
+ }
405
+ function createText(x, y, fill, textContent) {
406
+ var text = document.createElementNS(NS, 'text');
407
+ text.setAttribute('x', `${x}`);
408
+ text.setAttribute('y', `${y}`);
409
+ text.setAttribute('fill', fill);
410
+ text.textContent = textContent;
411
+ return text;
437
412
  }
438
413
 
439
- function withBoard(board) {
440
- const { onChange } = board;
441
- board.onChange = () => {
442
- const onContextChange = BOARD_TO_ON_CHANGE.get(board);
443
- if (onContextChange) {
444
- onContextChange();
445
- }
446
- onChange();
447
- };
448
- return board;
414
+ const IS_IOS = typeof navigator !== 'undefined' &&
415
+ typeof window !== 'undefined' &&
416
+ /iPad|iPhone|iPod/.test(navigator.userAgent) &&
417
+ !window.MSStream;
418
+ const IS_APPLE = typeof navigator !== 'undefined' && /Mac OS X/.test(navigator.userAgent);
419
+ const IS_FIREFOX = typeof navigator !== 'undefined' && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
420
+ const IS_SAFARI = typeof navigator !== 'undefined' && /Version\/[\d\.]+.*Safari/.test(navigator.userAgent);
421
+ // "modern" Edge was released at 79.x
422
+ const IS_EDGE_LEGACY = typeof navigator !== 'undefined' && /Edge?\/(?:[0-6][0-9]|[0-7][0-8])/i.test(navigator.userAgent);
423
+ const IS_CHROME = typeof navigator !== 'undefined' && /Chrome/i.test(navigator.userAgent);
424
+ // Native beforeInput events don't work well with react on Chrome 75 and older, Chrome 76+ can use beforeInput
425
+ const IS_CHROME_LEGACY = typeof navigator !== 'undefined' && /Chrome?\/(?:[0-7][0-5]|[0-6][0-9])/i.test(navigator.userAgent);
426
+
427
+ function toRectangleClient(points) {
428
+ const xArray = points.map(ele => ele[0]);
429
+ const yArray = points.map(ele => ele[1]);
430
+ const xMin = Math.min(...xArray);
431
+ const xMax = Math.max(...xArray);
432
+ const yMin = Math.min(...yArray);
433
+ const yMax = Math.max(...yArray);
434
+ const rect = { x: xMin, y: yMin, width: xMax - xMin, height: yMax - yMin };
435
+ return rect;
449
436
  }
450
437
 
451
438
  /**
@@ -538,70 +525,6 @@ const PlaitOperation = {
538
525
  const SAVING = new WeakMap();
539
526
  const MERGING = new WeakMap();
540
527
 
541
- function transformPoints(board, points) {
542
- const newPoints = points.map(point => {
543
- return transformPoint(board, point);
544
- });
545
- return newPoints;
546
- }
547
- function transformPoint(board, point) {
548
- const { width, height } = board.host.getBoundingClientRect();
549
- const viewBox = board.host.viewBox.baseVal;
550
- const x = (point[0] / width) * viewBox.width + viewBox.x;
551
- const y = (point[1] / height) * viewBox.height + viewBox.y;
552
- const newPoint = [x, y];
553
- return newPoint;
554
- }
555
- function isNoSelectionElement(e) {
556
- return e.target?.closest('.plait-board-attached');
557
- }
558
-
559
- const NS = 'http://www.w3.org/2000/svg';
560
- function toPoint(x, y, container) {
561
- const rect = container.getBoundingClientRect();
562
- return [x - rect.x, y - rect.y];
563
- }
564
- function createG() {
565
- const newG = document.createElementNS(NS, 'g');
566
- return newG;
567
- }
568
- function createSVG() {
569
- const svg = document.createElementNS(NS, 'svg');
570
- return svg;
571
- }
572
- function createText(x, y, fill, textContent) {
573
- var text = document.createElementNS(NS, 'text');
574
- text.setAttribute('x', `${x}`);
575
- text.setAttribute('y', `${y}`);
576
- text.setAttribute('fill', fill);
577
- text.textContent = textContent;
578
- return text;
579
- }
580
-
581
- const IS_IOS = typeof navigator !== 'undefined' &&
582
- typeof window !== 'undefined' &&
583
- /iPad|iPhone|iPod/.test(navigator.userAgent) &&
584
- !window.MSStream;
585
- const IS_APPLE = typeof navigator !== 'undefined' && /Mac OS X/.test(navigator.userAgent);
586
- const IS_FIREFOX = typeof navigator !== 'undefined' && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
587
- const IS_SAFARI = typeof navigator !== 'undefined' && /Version\/[\d\.]+.*Safari/.test(navigator.userAgent);
588
- // "modern" Edge was released at 79.x
589
- const IS_EDGE_LEGACY = typeof navigator !== 'undefined' && /Edge?\/(?:[0-6][0-9]|[0-7][0-8])/i.test(navigator.userAgent);
590
- const IS_CHROME = typeof navigator !== 'undefined' && /Chrome/i.test(navigator.userAgent);
591
- // Native beforeInput events don't work well with react on Chrome 75 and older, Chrome 76+ can use beforeInput
592
- const IS_CHROME_LEGACY = typeof navigator !== 'undefined' && /Chrome?\/(?:[0-7][0-5]|[0-6][0-9])/i.test(navigator.userAgent);
593
-
594
- function toRectangleClient(points) {
595
- const xArray = points.map(ele => ele[0]);
596
- const yArray = points.map(ele => ele[1]);
597
- const xMin = Math.min(...xArray);
598
- const xMax = Math.max(...xArray);
599
- const yMin = Math.min(...yArray);
600
- const yMax = Math.max(...yArray);
601
- const rect = { x: xMin, y: yMin, width: xMax - xMin, height: yMax - yMin };
602
- return rect;
603
- }
604
-
605
528
  /**
606
529
  * Check whether to merge an operation into the previous operation.
607
530
  */
@@ -981,6 +904,92 @@ function clampZoomLevel(zoom, minZoom = 0.2, maxZoom = 4) {
981
904
  return zoom < minZoom ? minZoom : zoom > maxZoom ? maxZoom : zoom;
982
905
  }
983
906
 
907
+ // record richtext type status
908
+ const FLUSHING = new WeakMap();
909
+ const IS_TEXT_EDITABLE = new WeakMap();
910
+ const BOARD_TO_ON_CHANGE = new WeakMap();
911
+ const HOST_TO_ROUGH_SVG = new WeakMap();
912
+ const PLAIT_BOARD_TO_COMPONENT = new WeakMap();
913
+
914
+ const updatePointerType = (board, pointer) => {
915
+ board.pointer = pointer;
916
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
917
+ boardComponent?.markForCheck();
918
+ };
919
+ const BoardTransforms = {
920
+ updatePointerType
921
+ };
922
+
923
+ const Transforms = {
924
+ ...GeneralTransforms,
925
+ ...ViewportTransforms,
926
+ ...SelectionTransforms,
927
+ ...NodeTransforms
928
+ };
929
+
930
+ function createBoard(host, children, options) {
931
+ const board = {
932
+ host,
933
+ viewport: {
934
+ zoom: 1,
935
+ viewBackgroundColor: '#000'
936
+ },
937
+ children,
938
+ operations: [],
939
+ history: {
940
+ redos: [],
941
+ undos: []
942
+ },
943
+ selection: null,
944
+ pointer: PlaitPointerType.selection,
945
+ options: options || {
946
+ readonly: false,
947
+ allowClearBoard: false,
948
+ hideScrollbar: false
949
+ },
950
+ undo: () => { },
951
+ redo: () => { },
952
+ apply: (operation) => {
953
+ board.operations.push(operation);
954
+ Transforms.transform(board, operation);
955
+ if (!FLUSHING.get(board)) {
956
+ FLUSHING.set(board, true);
957
+ Promise.resolve().then(() => {
958
+ FLUSHING.set(board, false);
959
+ board.onChange();
960
+ board.operations = [];
961
+ });
962
+ }
963
+ },
964
+ onChange: () => { },
965
+ mousedown: (event) => { },
966
+ globalMouseup: (event) => { },
967
+ mousemove: (event) => { },
968
+ keydown: (event) => { },
969
+ keyup: (event) => { },
970
+ dblclick: (event) => { },
971
+ setFragment: (data) => { },
972
+ insertFragment: (data) => { },
973
+ deleteFragment: (data) => { },
974
+ drawElement: (context) => [],
975
+ redrawElement: (context, previousContext) => { },
976
+ destroyElement: (context) => { }
977
+ };
978
+ return board;
979
+ }
980
+
981
+ function withBoard(board) {
982
+ const { onChange } = board;
983
+ board.onChange = () => {
984
+ const onContextChange = BOARD_TO_ON_CHANGE.get(board);
985
+ if (onContextChange) {
986
+ onContextChange();
987
+ }
988
+ onChange();
989
+ };
990
+ return board;
991
+ }
992
+
984
993
  function withHistory(board) {
985
994
  const { apply, keydown } = board;
986
995
  board.history = { undos: [], redos: [] };
@@ -1065,12 +1074,6 @@ function withHistory(board) {
1065
1074
  return board;
1066
1075
  }
1067
1076
 
1068
- const updatePointerType = (board, pointer) => {
1069
- board.pointer = pointer;
1070
- const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
1071
- boardComponent?.markForCheck();
1072
- };
1073
-
1074
1077
  function withHandPointer(board) {
1075
1078
  const { mousedown, mousemove, globalMouseup, keydown, keyup } = board;
1076
1079
  const plaitBoardMove = {
@@ -1335,8 +1338,7 @@ class PlaitBoardComponent {
1335
1338
  this.destroy$ = new Subject();
1336
1339
  this.viewportState = {
1337
1340
  zoom: 1,
1338
- autoFitPadding: 8,
1339
- scrollBarWidth: SCROLL_BAR_WIDTH
1341
+ autoFitPadding: 8
1340
1342
  };
1341
1343
  this.isMoving = false;
1342
1344
  this.plaitValue = [];
@@ -1411,12 +1413,10 @@ class PlaitBoardComponent {
1411
1413
  this.board = board;
1412
1414
  if (this.plaitViewport) {
1413
1415
  this.board.viewport = this.plaitViewport;
1416
+ this.updateViewportState({
1417
+ zoom: this.plaitViewport?.zoom ?? 1
1418
+ });
1414
1419
  }
1415
- const scrollBarWidth = this.plaitOptions?.hideScrollbar ? SCROLL_BAR_WIDTH : 0;
1416
- this.updateViewportState({
1417
- scrollBarWidth,
1418
- zoom: this.board.viewport?.zoom ?? 1
1419
- });
1420
1420
  }
1421
1421
  initializeEvents() {
1422
1422
  fromEvent(this.host, 'mousedown')
@@ -1516,8 +1516,7 @@ class PlaitBoardComponent {
1516
1516
  this.renderer2.setStyle(this.viewportContainer.nativeElement, 'width', `${width}px`);
1517
1517
  this.renderer2.setStyle(this.viewportContainer.nativeElement, 'height', `${height}px`);
1518
1518
  }
1519
- initViewport() {
1520
- const viewport = this.board.viewport;
1519
+ initViewport(viewport = this.board.viewport) {
1521
1520
  const originationCoord = viewport?.originationCoord;
1522
1521
  const zoom = viewport?.zoom ?? this.viewportState.zoom;
1523
1522
  if (originationCoord) {
@@ -1535,7 +1534,7 @@ class PlaitBoardComponent {
1535
1534
  }
1536
1535
  calcViewBox(zoom = this.viewportState.zoom) {
1537
1536
  zoom = clampZoomLevel(zoom);
1538
- const scrollBarWidth = this.viewportState.scrollBarWidth;
1537
+ const scrollBarWidth = this.plaitOptions?.hideScrollbar ? SCROLL_BAR_WIDTH : 0;
1539
1538
  const viewportContainerBox = getViewportContainerBox(this.board);
1540
1539
  const groupBBox = getRootGroupBBox(this.board, zoom);
1541
1540
  const horizontalPadding = viewportContainerBox.width / 2;
@@ -1584,13 +1583,15 @@ class PlaitBoardComponent {
1584
1583
  }
1585
1584
  setScrollLeft(left) {
1586
1585
  const viewportContainerBox = getViewportContainerBox(this.board);
1587
- const { viewportWidth, scrollBarWidth } = this.viewportState;
1586
+ const scrollBarWidth = this.plaitOptions?.hideScrollbar ? SCROLL_BAR_WIDTH : 0;
1587
+ const { viewportWidth } = this.viewportState;
1588
1588
  const width = viewportWidth - viewportContainerBox.width + scrollBarWidth;
1589
1589
  this.viewportState.scrollLeft = left < 0 ? 0 : left > width ? width : left;
1590
1590
  }
1591
1591
  setScrollTop(top) {
1592
1592
  const viewportContainerBox = getViewportContainerBox(this.board);
1593
- const { viewportHeight, scrollBarWidth } = this.viewportState;
1593
+ const scrollBarWidth = this.plaitOptions?.hideScrollbar ? SCROLL_BAR_WIDTH : 0;
1594
+ const { viewportHeight } = this.viewportState;
1594
1595
  const height = viewportHeight - viewportContainerBox.height + scrollBarWidth;
1595
1596
  this.viewportState.scrollTop = top < 0 ? 0 : top > height ? height : top;
1596
1597
  }
@@ -1698,8 +1699,8 @@ class PlaitBoardComponent {
1698
1699
  const svgRect = this.host.getBoundingClientRect();
1699
1700
  const viewportContainerBox = getViewportContainerBox(this.board);
1700
1701
  if (svgRect.width > viewportContainerBox.width || svgRect.height > viewportContainerBox.height) {
1702
+ const scrollBarWidth = this.plaitOptions?.hideScrollbar ? SCROLL_BAR_WIDTH : 0;
1701
1703
  const matrix = this.getMatrix();
1702
- const scrollBarWidth = this.viewportState.scrollBarWidth;
1703
1704
  const [nodePointX, nodePointY] = convertToViewportCoordinates([client.x, client.y], matrix);
1704
1705
  const [fullNodePointX, fullNodePointY] = convertToViewportCoordinates([client.x + client.width, client.y + client.height], matrix);
1705
1706
  let newLeft = this.viewportState.scrollLeft;
@@ -1894,5 +1895,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImpo
1894
1895
  * Generated bundle index. Do not edit.
1895
1896
  */
1896
1897
 
1897
- export { BOARD_TO_ON_CHANGE, CLIP_BOARD_FORMAT_KEY, ELEMENT_TO_PLUGIN_COMPONENT, FLUSHING, HOST_TO_ROUGH_SVG, IS_APPLE, IS_CHROME, IS_CHROME_LEGACY, IS_EDGE_LEGACY, IS_FIREFOX, IS_IOS, IS_SAFARI, IS_TEXT_EDITABLE, MERGING, NS, PLAIT_BOARD_TO_COMPONENT, Path, PlaitBoardComponent, PlaitElementComponent, PlaitHistoryBoard, PlaitModule, PlaitNode, PlaitOperation, PlaitPluginElementComponent, PlaitPointerType, PlaitToolbarComponent, SAVING, SCROLL_BAR_WIDTH, Transforms, Viewport, clampZoomLevel, convertToViewportCoordinates, createG, createSVG, createText, distanceBetweenPointAndPoint, distanceBetweenPointAndSegment, getBoardClientBox, getRootGroupBBox, getViewportContainerBox, hasBeforeContextChange, hotkeys, idCreator, inverse, invertMatrix, invertViewportCoordinates, isNoSelectionElement, isNullOrUndefined, isSetViewportOperation, normalizePoint, rotate, shouldClear, shouldMerge, shouldSave, toPoint, toRectangleClient, transformMat3, transformPoint, transformPoints };
1898
+ export { BOARD_TO_ON_CHANGE, BoardTransforms, CLIP_BOARD_FORMAT_KEY, ELEMENT_TO_PLUGIN_COMPONENT, FLUSHING, HOST_TO_ROUGH_SVG, IS_APPLE, IS_CHROME, IS_CHROME_LEGACY, IS_EDGE_LEGACY, IS_FIREFOX, IS_IOS, IS_SAFARI, IS_TEXT_EDITABLE, MERGING, NS, PLAIT_BOARD_TO_COMPONENT, Path, PlaitBoardComponent, PlaitElementComponent, PlaitHistoryBoard, PlaitModule, PlaitNode, PlaitOperation, PlaitPluginElementComponent, PlaitPointerType, PlaitToolbarComponent, SAVING, SCROLL_BAR_WIDTH, Transforms, Viewport, clampZoomLevel, convertToViewportCoordinates, createG, createSVG, createText, distanceBetweenPointAndPoint, distanceBetweenPointAndSegment, getBoardClientBox, getRootGroupBBox, getViewportContainerBox, hasBeforeContextChange, hotkeys, idCreator, inverse, invertMatrix, invertViewportCoordinates, isNoSelectionElement, isNullOrUndefined, isSetViewportOperation, normalizePoint, rotate, shouldClear, shouldMerge, shouldSave, toPoint, toRectangleClient, transformMat3, transformPoint, transformPoints };
1898
1899
  //# sourceMappingURL=plait-core.mjs.map