@plait/core 0.0.43 → 0.0.45

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.
@@ -462,8 +462,11 @@ function createBoard(host, children, options) {
462
462
  },
463
463
  selection: null,
464
464
  cursor: BaseCursorStatus.select,
465
- readonly: options.readonly,
466
- allowClearBoard: options.allowClearBoard,
465
+ options: options || {
466
+ readonly: false,
467
+ allowClearBoard: false,
468
+ hideScrollbar: false
469
+ },
467
470
  undo: () => { },
468
471
  redo: () => { },
469
472
  apply: (operation) => {
@@ -708,9 +711,10 @@ function transformPoints(board, points) {
708
711
  return newPoints;
709
712
  }
710
713
  function transformPoint(board, point) {
711
- const viewBox = getViewBox(board);
712
- const x = (point[0] / viewBox.viewportWidth) * viewBox.width + viewBox.minX;
713
- const y = (point[1] / viewBox.viewportHeight) * viewBox.height + viewBox.minY;
714
+ const { width, height } = board.host.getBoundingClientRect();
715
+ const viewBox = board.host.viewBox.baseVal;
716
+ const x = (point[0] / width) * viewBox.width + viewBox.x;
717
+ const y = (point[1] / height) * viewBox.height + viewBox.y;
714
718
  const newPoint = [x, y];
715
719
  return newPoint;
716
720
  }
@@ -729,10 +733,12 @@ function getViewBox(board) {
729
733
  }
730
734
  function getViewportClientBox(board) {
731
735
  var _a;
736
+ const hideScrollbar = board.options.hideScrollbar;
737
+ const scrollBarWidth = hideScrollbar ? SCROLL_BAR_WIDTH : 0;
732
738
  const container = (_a = board.host) === null || _a === void 0 ? void 0 : _a.parentElement;
733
739
  const containerRect = container === null || container === void 0 ? void 0 : container.getBoundingClientRect();
734
- const width = containerRect.width - SCROLL_BAR_WIDTH;
735
- const height = containerRect.height - SCROLL_BAR_WIDTH;
740
+ const width = containerRect.width - scrollBarWidth;
741
+ const height = containerRect.height - scrollBarWidth;
736
742
  return {
737
743
  width,
738
744
  height
@@ -947,7 +953,7 @@ function withMove(board) {
947
953
  y: 0
948
954
  };
949
955
  board.mousedown = (event) => {
950
- if (board.readonly) {
956
+ if (board.options.readonly) {
951
957
  updateCursorStatus(board, BaseCursorStatus.move);
952
958
  }
953
959
  else if (!board.selection) {
@@ -991,7 +997,7 @@ function withMove(board) {
991
997
  keydown(event);
992
998
  };
993
999
  board.keyup = (event) => {
994
- if (board.selection && !board.readonly && event.code === 'Space') {
1000
+ if (board.selection && !board.options.readonly && event.code === 'Space') {
995
1001
  updateCursorStatus(board, BaseCursorStatus.select);
996
1002
  const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
997
1003
  boardComponent.cdr.markForCheck();
@@ -1169,12 +1175,10 @@ class PlaitBoardComponent {
1169
1175
  this.elementRef = elementRef;
1170
1176
  this.hasInitialized = false;
1171
1177
  this.destroy$ = new Subject();
1172
- this.isMoving = false;
1173
1178
  this.autoFitPadding = 8;
1179
+ this.isMoving = false;
1174
1180
  this.plaitValue = [];
1175
1181
  this.plaitPlugins = [];
1176
- this.plaitReadonly = false;
1177
- this.plaitAllowClearBoard = false;
1178
1182
  this.plaitChange = new EventEmitter();
1179
1183
  this.plaitBoardInitialized = new EventEmitter();
1180
1184
  this.trackBy = (index, element) => {
@@ -1195,7 +1199,7 @@ class PlaitBoardComponent {
1195
1199
  return `plait-board-container ${this.board.cursor}`;
1196
1200
  }
1197
1201
  get readonly() {
1198
- return this.plaitReadonly;
1202
+ return this.board.options.readonly;
1199
1203
  }
1200
1204
  get moving() {
1201
1205
  return this.board.cursor === BaseCursorStatus.move && this.isMoving;
@@ -1217,15 +1221,21 @@ class PlaitBoardComponent {
1217
1221
  viewport: this.board.viewport,
1218
1222
  selection: this.board.selection
1219
1223
  };
1220
- this.updateViewport();
1224
+ if (this.board.operations.some(op => PlaitOperation.isSetViewportOperation(op))) {
1225
+ this.updateViewport();
1226
+ }
1221
1227
  this.plaitChange.emit(changeEvent);
1222
1228
  });
1223
1229
  this.hasInitialized = true;
1224
1230
  }
1225
1231
  ngOnChanges(changes) {
1226
- const valueChange = changes['plaitValue'];
1227
- if (valueChange && this.hasInitialized) {
1228
- this.board.children = valueChange.currentValue;
1232
+ if (this.hasInitialized) {
1233
+ const valueChange = changes['plaitValue'];
1234
+ const options = changes['plaitOptions'];
1235
+ if (valueChange)
1236
+ this.board.children = valueChange.currentValue;
1237
+ if (options)
1238
+ this.board.options = options.currentValue;
1229
1239
  this.cdr.markForCheck();
1230
1240
  }
1231
1241
  }
@@ -1235,8 +1245,7 @@ class PlaitBoardComponent {
1235
1245
  this.updateViewport();
1236
1246
  }
1237
1247
  initializePlugins() {
1238
- const options = { readonly: this.plaitReadonly, allowClearBoard: this.plaitAllowClearBoard };
1239
- let board = withMove(withHistory(withSelection(withBoard(createBoard(this.host, this.plaitValue, options)))));
1248
+ let board = withMove(withHistory(withSelection(withBoard(createBoard(this.host, this.plaitValue, this.plaitOptions)))));
1240
1249
  this.plaitPlugins.forEach(plugin => {
1241
1250
  board = plugin(board);
1242
1251
  });
@@ -1309,15 +1318,27 @@ class PlaitBoardComponent {
1309
1318
  (_a = this.board) === null || _a === void 0 ? void 0 : _a.setFragment(event.clipboardData);
1310
1319
  (_b = this.board) === null || _b === void 0 ? void 0 : _b.deleteFragment(event.clipboardData);
1311
1320
  });
1321
+ fromEvent(this.contentContainer.nativeElement, 'wheel')
1322
+ .pipe(takeUntil(this.destroy$), filter((e) => {
1323
+ if (!this.isFocused) {
1324
+ e.preventDefault();
1325
+ e.stopPropagation();
1326
+ }
1327
+ return !!this.isFocused;
1328
+ }))
1329
+ .subscribe();
1312
1330
  fromEvent(this.contentContainer.nativeElement, 'scroll')
1313
- .pipe(takeUntil(this.destroy$), filter(() => {
1331
+ .pipe(takeUntil(this.destroy$), filter((e) => {
1332
+ if (!this.isFocused) {
1333
+ e.preventDefault();
1334
+ e.stopPropagation();
1335
+ }
1314
1336
  return !!this.isFocused;
1315
1337
  }))
1316
1338
  .subscribe((event) => {
1317
1339
  const scrollLeft = event.target.scrollLeft;
1318
1340
  const scrollTop = event.target.scrollTop;
1319
1341
  this.getScrollOffset(scrollLeft, scrollTop);
1320
- this.setScroll(scrollLeft, scrollTop);
1321
1342
  });
1322
1343
  window.onresize = () => {
1323
1344
  this.updateViewport();
@@ -1331,8 +1352,10 @@ class PlaitBoardComponent {
1331
1352
  var _a;
1332
1353
  const container = (_a = this.elementRef.nativeElement) === null || _a === void 0 ? void 0 : _a.parentElement;
1333
1354
  const containerRect = container === null || container === void 0 ? void 0 : container.getBoundingClientRect();
1334
- const width = `${containerRect.width + SCROLL_BAR_WIDTH}px`;
1335
- const height = `${containerRect.height + SCROLL_BAR_WIDTH}px`;
1355
+ const hideScrollbar = this.board.options.hideScrollbar;
1356
+ const scrollBarWidth = hideScrollbar ? SCROLL_BAR_WIDTH : 0;
1357
+ const width = `${containerRect.width + scrollBarWidth}px`;
1358
+ const height = `${containerRect.height + scrollBarWidth}px`;
1336
1359
  this.renderer2.setStyle(this.contentContainer.nativeElement, 'width', width);
1337
1360
  this.renderer2.setStyle(this.contentContainer.nativeElement, 'height', height);
1338
1361
  this.renderer2.setStyle(this.contentContainer.nativeElement, 'maxWidth', width);
@@ -1355,7 +1378,8 @@ class PlaitBoardComponent {
1355
1378
  offsetYRatio: scrollTopRatio
1356
1379
  });
1357
1380
  }
1358
- viewportChange(viewBox) {
1381
+ viewportChange() {
1382
+ const viewBox = getViewBox(this.board);
1359
1383
  const offsetXRatio = this.board.viewport.offsetXRatio;
1360
1384
  const offsetYRatio = this.board.viewport.offsetYRatio;
1361
1385
  const viewportBox = getViewportClientBox(this.board);
@@ -1363,17 +1387,18 @@ class PlaitBoardComponent {
1363
1387
  const box = [minX, minY, width, height];
1364
1388
  const scrollLeft = (viewportWidth - viewportBox.width) * offsetXRatio;
1365
1389
  const scrollTop = (viewportHeight - viewportBox.height) * offsetYRatio;
1366
- this.resizeViewport();
1367
1390
  this.renderer2.setStyle(this.host, 'display', 'block');
1368
1391
  this.renderer2.setStyle(this.host, 'width', `${viewportWidth}px`);
1369
1392
  this.renderer2.setStyle(this.host, 'height', `${viewportHeight}px`);
1370
- this.renderer2.setStyle(this.host, 'cursor', this.plaitReadonly ? 'grab' : 'default');
1371
- this.renderer2.setAttribute(this.host, 'viewBox', box.join());
1393
+ this.renderer2.setStyle(this.host, 'cursor', this.isMoveMode ? 'grab' : 'default');
1394
+ if (width > 0 && height > 0) {
1395
+ this.renderer2.setAttribute(this.host, 'viewBox', box.join());
1396
+ }
1372
1397
  this.setScroll(scrollLeft, scrollTop);
1373
1398
  }
1374
1399
  updateViewport() {
1375
- const viewBox = getViewBox(this.board);
1376
- this.viewportChange(viewBox);
1400
+ this.resizeViewport();
1401
+ this.viewportChange();
1377
1402
  }
1378
1403
  // 拖拽模式
1379
1404
  changeMoveMode(cursorStatus) {
@@ -1431,7 +1456,7 @@ class PlaitBoardComponent {
1431
1456
  }
1432
1457
  }
1433
1458
  PlaitBoardComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: PlaitBoardComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.Renderer2 }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Component });
1434
- PlaitBoardComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.11", type: PlaitBoardComponent, selector: "plait-board", inputs: { plaitValue: "plaitValue", plaitViewport: "plaitViewport", plaitPlugins: "plaitPlugins", plaitReadonly: "plaitReadonly", plaitAllowClearBoard: "plaitAllowClearBoard" }, outputs: { plaitChange: "plaitChange", plaitBoardInitialized: "plaitBoardInitialized" }, host: { properties: { "class": "this.hostClass", "class.readonly": "this.readonly", "class.moving": "this.moving", "class.focused": "this.focused" } }, queries: [{ propertyName: "toolbarTemplateRef", first: true, predicate: ["plaitToolbar"], descendants: true }], viewQueries: [{ propertyName: "svg", first: true, predicate: ["svg"], descendants: true, static: true }, { propertyName: "contentContainer", first: true, predicate: ["container"], descendants: true, read: ElementRef, static: true }], usesOnChanges: true, ngImport: i0, template: `
1459
+ PlaitBoardComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.11", type: PlaitBoardComponent, selector: "plait-board", inputs: { plaitValue: "plaitValue", plaitViewport: "plaitViewport", plaitPlugins: "plaitPlugins", plaitOptions: "plaitOptions" }, outputs: { plaitChange: "plaitChange", plaitBoardInitialized: "plaitBoardInitialized" }, host: { properties: { "class": "this.hostClass", "class.readonly": "this.readonly", "class.moving": "this.moving", "class.focused": "this.focused" } }, queries: [{ propertyName: "toolbarTemplateRef", first: true, predicate: ["plaitToolbar"], descendants: true }], viewQueries: [{ propertyName: "svg", first: true, predicate: ["svg"], descendants: true, static: true }, { propertyName: "contentContainer", first: true, predicate: ["container"], descendants: true, read: ElementRef, static: true }], usesOnChanges: true, ngImport: i0, template: `
1435
1460
  <div class="container" #container>
1436
1461
  <svg #svg width="100%" height="100%" style="position: relative"></svg>
1437
1462
  <plait-element
@@ -1493,9 +1518,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImpo
1493
1518
  type: Input
1494
1519
  }], plaitPlugins: [{
1495
1520
  type: Input
1496
- }], plaitReadonly: [{
1497
- type: Input
1498
- }], plaitAllowClearBoard: [{
1521
+ }], plaitOptions: [{
1499
1522
  type: Input
1500
1523
  }], plaitChange: [{
1501
1524
  type: Output