@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.
@@ -486,8 +486,11 @@ function createBoard(host, children, options) {
486
486
  },
487
487
  selection: null,
488
488
  cursor: BaseCursorStatus.select,
489
- readonly: options.readonly,
490
- allowClearBoard: options.allowClearBoard,
489
+ options: options || {
490
+ readonly: false,
491
+ allowClearBoard: false,
492
+ hideScrollbar: false
493
+ },
491
494
  undo: () => { },
492
495
  redo: () => { },
493
496
  apply: (operation) => {
@@ -732,9 +735,10 @@ function transformPoints(board, points) {
732
735
  return newPoints;
733
736
  }
734
737
  function transformPoint(board, point) {
735
- const viewBox = getViewBox(board);
736
- const x = (point[0] / viewBox.viewportWidth) * viewBox.width + viewBox.minX;
737
- const y = (point[1] / viewBox.viewportHeight) * viewBox.height + viewBox.minY;
738
+ const { width, height } = board.host.getBoundingClientRect();
739
+ const viewBox = board.host.viewBox.baseVal;
740
+ const x = (point[0] / width) * viewBox.width + viewBox.x;
741
+ const y = (point[1] / height) * viewBox.height + viewBox.y;
738
742
  const newPoint = [x, y];
739
743
  return newPoint;
740
744
  }
@@ -752,10 +756,12 @@ function getViewBox(board) {
752
756
  return { minX, minY, width, height, viewportWidth, viewportHeight };
753
757
  }
754
758
  function getViewportClientBox(board) {
759
+ const hideScrollbar = board.options.hideScrollbar;
760
+ const scrollBarWidth = hideScrollbar ? SCROLL_BAR_WIDTH : 0;
755
761
  const container = board.host?.parentElement;
756
762
  const containerRect = container?.getBoundingClientRect();
757
- const width = containerRect.width - SCROLL_BAR_WIDTH;
758
- const height = containerRect.height - SCROLL_BAR_WIDTH;
763
+ const width = containerRect.width - scrollBarWidth;
764
+ const height = containerRect.height - scrollBarWidth;
759
765
  return {
760
766
  width,
761
767
  height
@@ -969,7 +975,7 @@ function withMove(board) {
969
975
  y: 0
970
976
  };
971
977
  board.mousedown = (event) => {
972
- if (board.readonly) {
978
+ if (board.options.readonly) {
973
979
  updateCursorStatus(board, BaseCursorStatus.move);
974
980
  }
975
981
  else if (!board.selection) {
@@ -1013,7 +1019,7 @@ function withMove(board) {
1013
1019
  keydown(event);
1014
1020
  };
1015
1021
  board.keyup = (event) => {
1016
- if (board.selection && !board.readonly && event.code === 'Space') {
1022
+ if (board.selection && !board.options.readonly && event.code === 'Space') {
1017
1023
  updateCursorStatus(board, BaseCursorStatus.select);
1018
1024
  const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
1019
1025
  boardComponent.cdr.markForCheck();
@@ -1191,12 +1197,10 @@ class PlaitBoardComponent {
1191
1197
  this.elementRef = elementRef;
1192
1198
  this.hasInitialized = false;
1193
1199
  this.destroy$ = new Subject();
1194
- this.isMoving = false;
1195
1200
  this.autoFitPadding = 8;
1201
+ this.isMoving = false;
1196
1202
  this.plaitValue = [];
1197
1203
  this.plaitPlugins = [];
1198
- this.plaitReadonly = false;
1199
- this.plaitAllowClearBoard = false;
1200
1204
  this.plaitChange = new EventEmitter();
1201
1205
  this.plaitBoardInitialized = new EventEmitter();
1202
1206
  this.trackBy = (index, element) => {
@@ -1216,7 +1220,7 @@ class PlaitBoardComponent {
1216
1220
  return `plait-board-container ${this.board.cursor}`;
1217
1221
  }
1218
1222
  get readonly() {
1219
- return this.plaitReadonly;
1223
+ return this.board.options.readonly;
1220
1224
  }
1221
1225
  get moving() {
1222
1226
  return this.board.cursor === BaseCursorStatus.move && this.isMoving;
@@ -1238,15 +1242,21 @@ class PlaitBoardComponent {
1238
1242
  viewport: this.board.viewport,
1239
1243
  selection: this.board.selection
1240
1244
  };
1241
- this.updateViewport();
1245
+ if (this.board.operations.some(op => PlaitOperation.isSetViewportOperation(op))) {
1246
+ this.updateViewport();
1247
+ }
1242
1248
  this.plaitChange.emit(changeEvent);
1243
1249
  });
1244
1250
  this.hasInitialized = true;
1245
1251
  }
1246
1252
  ngOnChanges(changes) {
1247
- const valueChange = changes['plaitValue'];
1248
- if (valueChange && this.hasInitialized) {
1249
- this.board.children = valueChange.currentValue;
1253
+ if (this.hasInitialized) {
1254
+ const valueChange = changes['plaitValue'];
1255
+ const options = changes['plaitOptions'];
1256
+ if (valueChange)
1257
+ this.board.children = valueChange.currentValue;
1258
+ if (options)
1259
+ this.board.options = options.currentValue;
1250
1260
  this.cdr.markForCheck();
1251
1261
  }
1252
1262
  }
@@ -1256,8 +1266,7 @@ class PlaitBoardComponent {
1256
1266
  this.updateViewport();
1257
1267
  }
1258
1268
  initializePlugins() {
1259
- const options = { readonly: this.plaitReadonly, allowClearBoard: this.plaitAllowClearBoard };
1260
- let board = withMove(withHistory(withSelection(withBoard(createBoard(this.host, this.plaitValue, options)))));
1269
+ let board = withMove(withHistory(withSelection(withBoard(createBoard(this.host, this.plaitValue, this.plaitOptions)))));
1261
1270
  this.plaitPlugins.forEach(plugin => {
1262
1271
  board = plugin(board);
1263
1272
  });
@@ -1325,15 +1334,27 @@ class PlaitBoardComponent {
1325
1334
  this.board?.setFragment(event.clipboardData);
1326
1335
  this.board?.deleteFragment(event.clipboardData);
1327
1336
  });
1337
+ fromEvent(this.contentContainer.nativeElement, 'wheel')
1338
+ .pipe(takeUntil(this.destroy$), filter((e) => {
1339
+ if (!this.isFocused) {
1340
+ e.preventDefault();
1341
+ e.stopPropagation();
1342
+ }
1343
+ return !!this.isFocused;
1344
+ }))
1345
+ .subscribe();
1328
1346
  fromEvent(this.contentContainer.nativeElement, 'scroll')
1329
- .pipe(takeUntil(this.destroy$), filter(() => {
1347
+ .pipe(takeUntil(this.destroy$), filter((e) => {
1348
+ if (!this.isFocused) {
1349
+ e.preventDefault();
1350
+ e.stopPropagation();
1351
+ }
1330
1352
  return !!this.isFocused;
1331
1353
  }))
1332
1354
  .subscribe((event) => {
1333
1355
  const scrollLeft = event.target.scrollLeft;
1334
1356
  const scrollTop = event.target.scrollTop;
1335
1357
  this.getScrollOffset(scrollLeft, scrollTop);
1336
- this.setScroll(scrollLeft, scrollTop);
1337
1358
  });
1338
1359
  window.onresize = () => {
1339
1360
  this.updateViewport();
@@ -1346,8 +1367,10 @@ class PlaitBoardComponent {
1346
1367
  resizeViewport() {
1347
1368
  const container = this.elementRef.nativeElement?.parentElement;
1348
1369
  const containerRect = container?.getBoundingClientRect();
1349
- const width = `${containerRect.width + SCROLL_BAR_WIDTH}px`;
1350
- const height = `${containerRect.height + SCROLL_BAR_WIDTH}px`;
1370
+ const hideScrollbar = this.board.options.hideScrollbar;
1371
+ const scrollBarWidth = hideScrollbar ? SCROLL_BAR_WIDTH : 0;
1372
+ const width = `${containerRect.width + scrollBarWidth}px`;
1373
+ const height = `${containerRect.height + scrollBarWidth}px`;
1351
1374
  this.renderer2.setStyle(this.contentContainer.nativeElement, 'width', width);
1352
1375
  this.renderer2.setStyle(this.contentContainer.nativeElement, 'height', height);
1353
1376
  this.renderer2.setStyle(this.contentContainer.nativeElement, 'maxWidth', width);
@@ -1370,7 +1393,8 @@ class PlaitBoardComponent {
1370
1393
  offsetYRatio: scrollTopRatio
1371
1394
  });
1372
1395
  }
1373
- viewportChange(viewBox) {
1396
+ viewportChange() {
1397
+ const viewBox = getViewBox(this.board);
1374
1398
  const offsetXRatio = this.board.viewport.offsetXRatio;
1375
1399
  const offsetYRatio = this.board.viewport.offsetYRatio;
1376
1400
  const viewportBox = getViewportClientBox(this.board);
@@ -1378,17 +1402,18 @@ class PlaitBoardComponent {
1378
1402
  const box = [minX, minY, width, height];
1379
1403
  const scrollLeft = (viewportWidth - viewportBox.width) * offsetXRatio;
1380
1404
  const scrollTop = (viewportHeight - viewportBox.height) * offsetYRatio;
1381
- this.resizeViewport();
1382
1405
  this.renderer2.setStyle(this.host, 'display', 'block');
1383
1406
  this.renderer2.setStyle(this.host, 'width', `${viewportWidth}px`);
1384
1407
  this.renderer2.setStyle(this.host, 'height', `${viewportHeight}px`);
1385
- this.renderer2.setStyle(this.host, 'cursor', this.plaitReadonly ? 'grab' : 'default');
1386
- this.renderer2.setAttribute(this.host, 'viewBox', box.join());
1408
+ this.renderer2.setStyle(this.host, 'cursor', this.isMoveMode ? 'grab' : 'default');
1409
+ if (width > 0 && height > 0) {
1410
+ this.renderer2.setAttribute(this.host, 'viewBox', box.join());
1411
+ }
1387
1412
  this.setScroll(scrollLeft, scrollTop);
1388
1413
  }
1389
1414
  updateViewport() {
1390
- const viewBox = getViewBox(this.board);
1391
- this.viewportChange(viewBox);
1415
+ this.resizeViewport();
1416
+ this.viewportChange();
1392
1417
  }
1393
1418
  // 拖拽模式
1394
1419
  changeMoveMode(cursorStatus) {
@@ -1448,7 +1473,7 @@ class PlaitBoardComponent {
1448
1473
  }
1449
1474
  }
1450
1475
  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 });
1451
- 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: `
1476
+ 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: `
1452
1477
  <div class="container" #container>
1453
1478
  <svg #svg width="100%" height="100%" style="position: relative"></svg>
1454
1479
  <plait-element
@@ -1510,9 +1535,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImpo
1510
1535
  type: Input
1511
1536
  }], plaitPlugins: [{
1512
1537
  type: Input
1513
- }], plaitReadonly: [{
1514
- type: Input
1515
- }], plaitAllowClearBoard: [{
1538
+ }], plaitOptions: [{
1516
1539
  type: Input
1517
1540
  }], plaitChange: [{
1518
1541
  type: Output