@plait/core 0.0.39 → 0.0.41

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.
@@ -250,6 +250,7 @@ const FLUSHING = new WeakMap();
250
250
  const IS_TEXT_EDITABLE = new WeakMap();
251
251
  const BOARD_TO_ON_CHANGE = new WeakMap();
252
252
  const HOST_TO_ROUGH_SVG = new WeakMap();
253
+ const PLAIT_BOARD_TO_COMPONENT = new WeakMap();
253
254
 
254
255
  const applyToDraft = (board, selection, viewport, op) => {
255
256
  var _a, _b;
@@ -805,7 +806,7 @@ const PlaitHistoryBoard = {
805
806
  }
806
807
  };
807
808
 
808
- function withHistroy(board) {
809
+ function withHistory(board) {
809
810
  const { apply, keydown } = board;
810
811
  board.history = { undos: [], redos: [] };
811
812
  board.redo = () => {
@@ -889,6 +890,65 @@ function withHistroy(board) {
889
890
  return board;
890
891
  }
891
892
 
893
+ function withMove(board) {
894
+ const { mousedown, mousemove, globalMouseup, keydown, keyup } = board;
895
+ const plaitBoardMove = {
896
+ x: 0,
897
+ y: 0
898
+ };
899
+ board.mousedown = (event) => {
900
+ if (board.readonly) {
901
+ updateCursorStatus(board, BaseCursorStatus.move);
902
+ }
903
+ else if (!board.selection) {
904
+ updateCursorStatus(board, BaseCursorStatus.select);
905
+ }
906
+ if (board.cursor === BaseCursorStatus.move && board.selection) {
907
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
908
+ boardComponent.movingChange(true);
909
+ plaitBoardMove.x = event.x;
910
+ plaitBoardMove.y = event.y;
911
+ boardComponent.cdr.detectChanges();
912
+ }
913
+ mousedown(event);
914
+ };
915
+ board.mousemove = (event) => {
916
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
917
+ if (board.cursor === BaseCursorStatus.move && board.selection && boardComponent.isMoving) {
918
+ const viewport = board === null || board === void 0 ? void 0 : board.viewport;
919
+ Transforms.setViewport(board, Object.assign(Object.assign({}, viewport), { offsetX: viewport.offsetX + ((event.x - plaitBoardMove.x) * 100) / transformZoom(board.viewport.zoom), offsetY: viewport.offsetY + ((event.y - plaitBoardMove.y) * 100) / transformZoom(board.viewport.zoom) }));
920
+ plaitBoardMove.x = event.x;
921
+ plaitBoardMove.y = event.y;
922
+ }
923
+ mousemove(event);
924
+ };
925
+ board.globalMouseup = (event) => {
926
+ if (board.selection) {
927
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
928
+ boardComponent.movingChange(false);
929
+ plaitBoardMove.x = 0;
930
+ plaitBoardMove.y = 0;
931
+ }
932
+ globalMouseup(event);
933
+ };
934
+ board.keydown = (event) => {
935
+ if (board.selection && event.code === 'Space') {
936
+ updateCursorStatus(board, BaseCursorStatus.move);
937
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
938
+ boardComponent.cdr.detectChanges();
939
+ event.preventDefault();
940
+ }
941
+ keydown(event);
942
+ };
943
+ board.keyup = (event) => {
944
+ board.selection && !board.readonly && event.code === 'Space' && updateCursorStatus(board, BaseCursorStatus.select);
945
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
946
+ boardComponent.cdr.detectChanges();
947
+ keyup(event);
948
+ };
949
+ return board;
950
+ }
951
+
892
952
  function withSelection(board) {
893
953
  const { mousedown, mousemove, globalMouseup } = board;
894
954
  let start = null;
@@ -1059,13 +1119,10 @@ class PlaitBoardComponent {
1059
1119
  this.cdr = cdr;
1060
1120
  this.elementRef = elementRef;
1061
1121
  this.renderer2 = renderer2;
1122
+ this.hasInitialized = false;
1062
1123
  this.destroy$ = new Subject();
1063
1124
  this._viewZoom = 100;
1064
- this.plaitBoardMove = {
1065
- isMoving: false,
1066
- x: 0,
1067
- y: 0
1068
- };
1125
+ this.isMoving = false;
1069
1126
  this.plaitValue = [];
1070
1127
  this.plaitPlugins = [];
1071
1128
  this.plaitReadonly = false;
@@ -1093,11 +1150,14 @@ class PlaitBoardComponent {
1093
1150
  return this.svg.nativeElement;
1094
1151
  }
1095
1152
  get isFocused() {
1096
- var _a, _b;
1097
- if (!((_a = this.board) === null || _a === void 0 ? void 0 : _a.selection) && this.board.cursor === BaseCursorStatus.move) {
1098
- this.changeMoveMode(BaseCursorStatus.select);
1099
- }
1100
- return (_b = this.board) === null || _b === void 0 ? void 0 : _b.selection;
1153
+ var _a;
1154
+ return (_a = this.board) === null || _a === void 0 ? void 0 : _a.selection;
1155
+ }
1156
+ get readonly() {
1157
+ return this.plaitReadonly;
1158
+ }
1159
+ get moving() {
1160
+ return this.isMoving;
1101
1161
  }
1102
1162
  get focused() {
1103
1163
  return this.isFocused;
@@ -1108,6 +1168,7 @@ class PlaitBoardComponent {
1108
1168
  this.initializePlugins();
1109
1169
  this.initializeEvents();
1110
1170
  this.updateViewport();
1171
+ PLAIT_BOARD_TO_COMPONENT.set(this.board, this);
1111
1172
  BOARD_TO_ON_CHANGE.set(this.board, () => {
1112
1173
  this.cdr.detectChanges();
1113
1174
  const changeEvent = {
@@ -1122,13 +1183,21 @@ class PlaitBoardComponent {
1122
1183
  }
1123
1184
  this.plaitChange.emit(changeEvent);
1124
1185
  });
1186
+ this.hasInitialized = true;
1187
+ }
1188
+ ngOnChanges(changes) {
1189
+ const valueChange = changes['plaitValue'];
1190
+ if (valueChange && this.hasInitialized) {
1191
+ this.board.children = valueChange.currentValue;
1192
+ this.cdr.markForCheck();
1193
+ }
1125
1194
  }
1126
1195
  ngAfterViewInit() {
1127
1196
  this.plaitBoardInitialized.emit(this.board);
1128
1197
  }
1129
1198
  initializePlugins() {
1130
1199
  const options = { readonly: this.plaitReadonly, allowClearBoard: this.plaitAllowClearBoard };
1131
- let board = withHistroy(withSelection(withBoard(createBoard(this.host, this.plaitValue, options))));
1200
+ let board = withMove(withHistory(withSelection(withBoard(createBoard(this.host, this.plaitValue, options)))));
1132
1201
  this.plaitPlugins.forEach(plugin => {
1133
1202
  board = plugin(board);
1134
1203
  });
@@ -1142,19 +1211,16 @@ class PlaitBoardComponent {
1142
1211
  .pipe(takeUntil(this.destroy$))
1143
1212
  .subscribe((event) => {
1144
1213
  this.board.mousedown(event);
1145
- this.isFocused && this.isMoveMode && this.initMove(event);
1146
1214
  });
1147
1215
  fromEvent(this.host, 'mousemove')
1148
1216
  .pipe(takeUntil(this.destroy$))
1149
1217
  .subscribe((event) => {
1150
1218
  this.board.mousemove(event);
1151
- this.isFocused && this.isMoveMode && this.plaitBoardMove.isMoving && this.moving(event);
1152
1219
  });
1153
1220
  fromEvent(document, 'mouseup')
1154
1221
  .pipe(takeUntil(this.destroy$))
1155
1222
  .subscribe((event) => {
1156
1223
  this.board.globalMouseup(event);
1157
- this.isFocused && this.moveEnd();
1158
1224
  });
1159
1225
  fromEvent(this.host, 'dblclick')
1160
1226
  .pipe(takeUntil(this.destroy$))
@@ -1177,10 +1243,6 @@ class PlaitBoardComponent {
1177
1243
  .subscribe((event) => {
1178
1244
  var _a;
1179
1245
  (_a = this.board) === null || _a === void 0 ? void 0 : _a.keydown(event);
1180
- if (this.isFocused && event.code === 'Space') {
1181
- this.changeMoveMode(BaseCursorStatus.move);
1182
- event.preventDefault();
1183
- }
1184
1246
  });
1185
1247
  fromEvent(document, 'keyup')
1186
1248
  .pipe(takeUntil(this.destroy$), filter(() => {
@@ -1189,7 +1251,6 @@ class PlaitBoardComponent {
1189
1251
  .subscribe((event) => {
1190
1252
  var _a;
1191
1253
  (_a = this.board) === null || _a === void 0 ? void 0 : _a.keyup(event);
1192
- this.isFocused && event.code === 'Space' && this.changeMoveMode(BaseCursorStatus.select);
1193
1254
  });
1194
1255
  fromEvent(document, 'copy')
1195
1256
  .pipe(takeUntil(this.destroy$), filter(() => {
@@ -1232,24 +1293,6 @@ class PlaitBoardComponent {
1232
1293
  const viewBox = getViewBox(this.board);
1233
1294
  this.renderer2.setAttribute(this.host, 'viewBox', `${viewBox.minX}, ${viewBox.minY}, ${viewBox.width}, ${viewBox.height}`);
1234
1295
  }
1235
- initMove(e) {
1236
- this.plaitBoardMove.isMoving = true;
1237
- this.plaitBoardMove.x = e.x;
1238
- this.plaitBoardMove.y = e.y;
1239
- this.cdr.detectChanges();
1240
- }
1241
- moving(e) {
1242
- var _a;
1243
- const viewport = (_a = this.board) === null || _a === void 0 ? void 0 : _a.viewport;
1244
- Transforms.setViewport(this.board, Object.assign(Object.assign({}, viewport), { offsetX: viewport.offsetX + ((e.x - this.plaitBoardMove.x) * 100) / this._viewZoom, offsetY: viewport.offsetY + ((e.y - this.plaitBoardMove.y) * 100) / this._viewZoom }));
1245
- this.plaitBoardMove.x = e.x;
1246
- this.plaitBoardMove.y = e.y;
1247
- }
1248
- moveEnd() {
1249
- this.plaitBoardMove.isMoving = false;
1250
- this.plaitBoardMove.x = 0;
1251
- this.plaitBoardMove.y = 0;
1252
- }
1253
1296
  // 拖拽模式
1254
1297
  changeMoveMode(cursorStatus) {
1255
1298
  updateCursorStatus(this.board, cursorStatus);
@@ -1292,16 +1335,13 @@ class PlaitBoardComponent {
1292
1335
  this.destroy$.complete();
1293
1336
  HOST_TO_ROUGH_SVG.delete(this.host);
1294
1337
  }
1338
+ movingChange(isMoving) {
1339
+ this.isMoving = isMoving;
1340
+ }
1295
1341
  }
1296
1342
  PlaitBoardComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: PlaitBoardComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Component });
1297
- 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.focused": "this.focused" } }, queries: [{ propertyName: "toolbarTemplateRef", first: true, predicate: ["plaitToolbar"], descendants: true }], viewQueries: [{ propertyName: "svg", first: true, predicate: ["svg"], descendants: true, static: true }], ngImport: i0, template: `
1298
- <svg
1299
- #svg
1300
- width="100%"
1301
- height="100%"
1302
- style="position: relative"
1303
- [style.cursor]="isMoveMode ? (plaitBoardMove.isMoving ? 'grabbing' : 'grab') : 'auto'"
1304
- ></svg>
1343
+ 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 }], usesOnChanges: true, ngImport: i0, template: `
1344
+ <svg #svg width="100%" height="100%" style="position: relative"></svg>
1305
1345
  <plait-toolbar
1306
1346
  *ngIf="isFocused && !toolbarTemplateRef"
1307
1347
  [cursorStatus]="board.cursor"
@@ -1328,13 +1368,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImpo
1328
1368
  args: [{
1329
1369
  selector: 'plait-board',
1330
1370
  template: `
1331
- <svg
1332
- #svg
1333
- width="100%"
1334
- height="100%"
1335
- style="position: relative"
1336
- [style.cursor]="isMoveMode ? (plaitBoardMove.isMoving ? 'grabbing' : 'grab') : 'auto'"
1337
- ></svg>
1371
+ <svg #svg width="100%" height="100%" style="position: relative"></svg>
1338
1372
  <plait-toolbar
1339
1373
  *ngIf="isFocused && !toolbarTemplateRef"
1340
1374
  [cursorStatus]="board.cursor"
@@ -1381,6 +1415,12 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImpo
1381
1415
  type: Output
1382
1416
  }], plaitBoardInitialized: [{
1383
1417
  type: Output
1418
+ }], readonly: [{
1419
+ type: HostBinding,
1420
+ args: ['class.readonly']
1421
+ }], moving: [{
1422
+ type: HostBinding,
1423
+ args: ['class.moving']
1384
1424
  }], focused: [{
1385
1425
  type: HostBinding,
1386
1426
  args: ['class.focused']
@@ -1411,5 +1451,5 @@ const CLIP_BOARD_FORMAT_KEY = 'x-plait-fragment';
1411
1451
  * Generated bundle index. Do not edit.
1412
1452
  */
1413
1453
 
1414
- export { BOARD_TO_ON_CHANGE, BaseCursorStatus, CLIP_BOARD_FORMAT_KEY, 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, Path, PlaitBoardComponent, PlaitElementComponent, PlaitHistoryBoard, PlaitModule, PlaitNode, PlaitOperation, PlaitToolbarComponent, SAVING, Transforms, Viewport, createG, createSVG, createText, distanceBetweenPointAndPoint, distanceBetweenPointAndSegment, getViewBox, hotkeys, idCreator, inverse, isNoSelectionElement, isNullOrUndefined, isSetViewportOperation, rotate, shouldClear, shouldMerge, shouldSave, toPoint, toRectangleClient, transformPoint, transformPoints, transformViewZoom, transformZoom, updateCursorStatus };
1454
+ export { BOARD_TO_ON_CHANGE, BaseCursorStatus, CLIP_BOARD_FORMAT_KEY, 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, PlaitToolbarComponent, SAVING, Transforms, Viewport, createG, createSVG, createText, distanceBetweenPointAndPoint, distanceBetweenPointAndSegment, getViewBox, hotkeys, idCreator, inverse, isNoSelectionElement, isNullOrUndefined, isSetViewportOperation, rotate, shouldClear, shouldMerge, shouldSave, toPoint, toRectangleClient, transformPoint, transformPoints, transformViewZoom, transformZoom, updateCursorStatus };
1415
1455
  //# sourceMappingURL=plait-core.mjs.map