@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.
@@ -270,6 +270,7 @@ const FLUSHING = new WeakMap();
270
270
  const IS_TEXT_EDITABLE = new WeakMap();
271
271
  const BOARD_TO_ON_CHANGE = new WeakMap();
272
272
  const HOST_TO_ROUGH_SVG = new WeakMap();
273
+ const PLAIT_BOARD_TO_COMPONENT = new WeakMap();
273
274
 
274
275
  const applyToDraft = (board, selection, viewport, op) => {
275
276
  switch (op.type) {
@@ -828,7 +829,7 @@ const PlaitHistoryBoard = {
828
829
  }
829
830
  };
830
831
 
831
- function withHistroy(board) {
832
+ function withHistory(board) {
832
833
  const { apply, keydown } = board;
833
834
  board.history = { undos: [], redos: [] };
834
835
  board.redo = () => {
@@ -912,6 +913,69 @@ function withHistroy(board) {
912
913
  return board;
913
914
  }
914
915
 
916
+ function withMove(board) {
917
+ const { mousedown, mousemove, globalMouseup, keydown, keyup } = board;
918
+ const plaitBoardMove = {
919
+ x: 0,
920
+ y: 0
921
+ };
922
+ board.mousedown = (event) => {
923
+ if (board.readonly) {
924
+ updateCursorStatus(board, BaseCursorStatus.move);
925
+ }
926
+ else if (!board.selection) {
927
+ updateCursorStatus(board, BaseCursorStatus.select);
928
+ }
929
+ if (board.cursor === BaseCursorStatus.move && board.selection) {
930
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
931
+ boardComponent.movingChange(true);
932
+ plaitBoardMove.x = event.x;
933
+ plaitBoardMove.y = event.y;
934
+ boardComponent.cdr.detectChanges();
935
+ }
936
+ mousedown(event);
937
+ };
938
+ board.mousemove = (event) => {
939
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
940
+ if (board.cursor === BaseCursorStatus.move && board.selection && boardComponent.isMoving) {
941
+ const viewport = board?.viewport;
942
+ Transforms.setViewport(board, {
943
+ ...viewport,
944
+ offsetX: viewport.offsetX + ((event.x - plaitBoardMove.x) * 100) / transformZoom(board.viewport.zoom),
945
+ offsetY: viewport.offsetY + ((event.y - plaitBoardMove.y) * 100) / transformZoom(board.viewport.zoom)
946
+ });
947
+ plaitBoardMove.x = event.x;
948
+ plaitBoardMove.y = event.y;
949
+ }
950
+ mousemove(event);
951
+ };
952
+ board.globalMouseup = (event) => {
953
+ if (board.selection) {
954
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
955
+ boardComponent.movingChange(false);
956
+ plaitBoardMove.x = 0;
957
+ plaitBoardMove.y = 0;
958
+ }
959
+ globalMouseup(event);
960
+ };
961
+ board.keydown = (event) => {
962
+ if (board.selection && event.code === 'Space') {
963
+ updateCursorStatus(board, BaseCursorStatus.move);
964
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
965
+ boardComponent.cdr.detectChanges();
966
+ event.preventDefault();
967
+ }
968
+ keydown(event);
969
+ };
970
+ board.keyup = (event) => {
971
+ board.selection && !board.readonly && event.code === 'Space' && updateCursorStatus(board, BaseCursorStatus.select);
972
+ const boardComponent = PLAIT_BOARD_TO_COMPONENT.get(board);
973
+ boardComponent.cdr.detectChanges();
974
+ keyup(event);
975
+ };
976
+ return board;
977
+ }
978
+
915
979
  function withSelection(board) {
916
980
  const { mousedown, mousemove, globalMouseup } = board;
917
981
  let start = null;
@@ -1082,13 +1146,10 @@ class PlaitBoardComponent {
1082
1146
  this.cdr = cdr;
1083
1147
  this.elementRef = elementRef;
1084
1148
  this.renderer2 = renderer2;
1149
+ this.hasInitialized = false;
1085
1150
  this.destroy$ = new Subject();
1086
1151
  this._viewZoom = 100;
1087
- this.plaitBoardMove = {
1088
- isMoving: false,
1089
- x: 0,
1090
- y: 0
1091
- };
1152
+ this.isMoving = false;
1092
1153
  this.plaitValue = [];
1093
1154
  this.plaitPlugins = [];
1094
1155
  this.plaitReadonly = false;
@@ -1116,11 +1177,14 @@ class PlaitBoardComponent {
1116
1177
  return this.svg.nativeElement;
1117
1178
  }
1118
1179
  get isFocused() {
1119
- if (!this.board?.selection && this.board.cursor === BaseCursorStatus.move) {
1120
- this.changeMoveMode(BaseCursorStatus.select);
1121
- }
1122
1180
  return this.board?.selection;
1123
1181
  }
1182
+ get readonly() {
1183
+ return this.plaitReadonly;
1184
+ }
1185
+ get moving() {
1186
+ return this.isMoving;
1187
+ }
1124
1188
  get focused() {
1125
1189
  return this.isFocused;
1126
1190
  }
@@ -1130,6 +1194,7 @@ class PlaitBoardComponent {
1130
1194
  this.initializePlugins();
1131
1195
  this.initializeEvents();
1132
1196
  this.updateViewport();
1197
+ PLAIT_BOARD_TO_COMPONENT.set(this.board, this);
1133
1198
  BOARD_TO_ON_CHANGE.set(this.board, () => {
1134
1199
  this.cdr.detectChanges();
1135
1200
  const changeEvent = {
@@ -1144,13 +1209,21 @@ class PlaitBoardComponent {
1144
1209
  }
1145
1210
  this.plaitChange.emit(changeEvent);
1146
1211
  });
1212
+ this.hasInitialized = true;
1213
+ }
1214
+ ngOnChanges(changes) {
1215
+ const valueChange = changes['plaitValue'];
1216
+ if (valueChange && this.hasInitialized) {
1217
+ this.board.children = valueChange.currentValue;
1218
+ this.cdr.markForCheck();
1219
+ }
1147
1220
  }
1148
1221
  ngAfterViewInit() {
1149
1222
  this.plaitBoardInitialized.emit(this.board);
1150
1223
  }
1151
1224
  initializePlugins() {
1152
1225
  const options = { readonly: this.plaitReadonly, allowClearBoard: this.plaitAllowClearBoard };
1153
- let board = withHistroy(withSelection(withBoard(createBoard(this.host, this.plaitValue, options))));
1226
+ let board = withMove(withHistory(withSelection(withBoard(createBoard(this.host, this.plaitValue, options)))));
1154
1227
  this.plaitPlugins.forEach(plugin => {
1155
1228
  board = plugin(board);
1156
1229
  });
@@ -1164,19 +1237,16 @@ class PlaitBoardComponent {
1164
1237
  .pipe(takeUntil(this.destroy$))
1165
1238
  .subscribe((event) => {
1166
1239
  this.board.mousedown(event);
1167
- this.isFocused && this.isMoveMode && this.initMove(event);
1168
1240
  });
1169
1241
  fromEvent(this.host, 'mousemove')
1170
1242
  .pipe(takeUntil(this.destroy$))
1171
1243
  .subscribe((event) => {
1172
1244
  this.board.mousemove(event);
1173
- this.isFocused && this.isMoveMode && this.plaitBoardMove.isMoving && this.moving(event);
1174
1245
  });
1175
1246
  fromEvent(document, 'mouseup')
1176
1247
  .pipe(takeUntil(this.destroy$))
1177
1248
  .subscribe((event) => {
1178
1249
  this.board.globalMouseup(event);
1179
- this.isFocused && this.moveEnd();
1180
1250
  });
1181
1251
  fromEvent(this.host, 'dblclick')
1182
1252
  .pipe(takeUntil(this.destroy$))
@@ -1202,10 +1272,6 @@ class PlaitBoardComponent {
1202
1272
  }))
1203
1273
  .subscribe((event) => {
1204
1274
  this.board?.keydown(event);
1205
- if (this.isFocused && event.code === 'Space') {
1206
- this.changeMoveMode(BaseCursorStatus.move);
1207
- event.preventDefault();
1208
- }
1209
1275
  });
1210
1276
  fromEvent(document, 'keyup')
1211
1277
  .pipe(takeUntil(this.destroy$), filter(() => {
@@ -1213,7 +1279,6 @@ class PlaitBoardComponent {
1213
1279
  }))
1214
1280
  .subscribe((event) => {
1215
1281
  this.board?.keyup(event);
1216
- this.isFocused && event.code === 'Space' && this.changeMoveMode(BaseCursorStatus.select);
1217
1282
  });
1218
1283
  fromEvent(document, 'copy')
1219
1284
  .pipe(takeUntil(this.destroy$), filter(() => {
@@ -1252,27 +1317,6 @@ class PlaitBoardComponent {
1252
1317
  const viewBox = getViewBox(this.board);
1253
1318
  this.renderer2.setAttribute(this.host, 'viewBox', `${viewBox.minX}, ${viewBox.minY}, ${viewBox.width}, ${viewBox.height}`);
1254
1319
  }
1255
- initMove(e) {
1256
- this.plaitBoardMove.isMoving = true;
1257
- this.plaitBoardMove.x = e.x;
1258
- this.plaitBoardMove.y = e.y;
1259
- this.cdr.detectChanges();
1260
- }
1261
- moving(e) {
1262
- const viewport = this.board?.viewport;
1263
- Transforms.setViewport(this.board, {
1264
- ...viewport,
1265
- offsetX: viewport.offsetX + ((e.x - this.plaitBoardMove.x) * 100) / this._viewZoom,
1266
- offsetY: viewport.offsetY + ((e.y - this.plaitBoardMove.y) * 100) / this._viewZoom
1267
- });
1268
- this.plaitBoardMove.x = e.x;
1269
- this.plaitBoardMove.y = e.y;
1270
- }
1271
- moveEnd() {
1272
- this.plaitBoardMove.isMoving = false;
1273
- this.plaitBoardMove.x = 0;
1274
- this.plaitBoardMove.y = 0;
1275
- }
1276
1320
  // 拖拽模式
1277
1321
  changeMoveMode(cursorStatus) {
1278
1322
  updateCursorStatus(this.board, cursorStatus);
@@ -1320,16 +1364,13 @@ class PlaitBoardComponent {
1320
1364
  this.destroy$.complete();
1321
1365
  HOST_TO_ROUGH_SVG.delete(this.host);
1322
1366
  }
1367
+ movingChange(isMoving) {
1368
+ this.isMoving = isMoving;
1369
+ }
1323
1370
  }
1324
1371
  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 });
1325
- 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: `
1326
- <svg
1327
- #svg
1328
- width="100%"
1329
- height="100%"
1330
- style="position: relative"
1331
- [style.cursor]="isMoveMode ? (plaitBoardMove.isMoving ? 'grabbing' : 'grab') : 'auto'"
1332
- ></svg>
1372
+ 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: `
1373
+ <svg #svg width="100%" height="100%" style="position: relative"></svg>
1333
1374
  <plait-toolbar
1334
1375
  *ngIf="isFocused && !toolbarTemplateRef"
1335
1376
  [cursorStatus]="board.cursor"
@@ -1356,13 +1397,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImpo
1356
1397
  args: [{
1357
1398
  selector: 'plait-board',
1358
1399
  template: `
1359
- <svg
1360
- #svg
1361
- width="100%"
1362
- height="100%"
1363
- style="position: relative"
1364
- [style.cursor]="isMoveMode ? (plaitBoardMove.isMoving ? 'grabbing' : 'grab') : 'auto'"
1365
- ></svg>
1400
+ <svg #svg width="100%" height="100%" style="position: relative"></svg>
1366
1401
  <plait-toolbar
1367
1402
  *ngIf="isFocused && !toolbarTemplateRef"
1368
1403
  [cursorStatus]="board.cursor"
@@ -1409,6 +1444,12 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImpo
1409
1444
  type: Output
1410
1445
  }], plaitBoardInitialized: [{
1411
1446
  type: Output
1447
+ }], readonly: [{
1448
+ type: HostBinding,
1449
+ args: ['class.readonly']
1450
+ }], moving: [{
1451
+ type: HostBinding,
1452
+ args: ['class.moving']
1412
1453
  }], focused: [{
1413
1454
  type: HostBinding,
1414
1455
  args: ['class.focused']
@@ -1439,5 +1480,5 @@ const CLIP_BOARD_FORMAT_KEY = 'x-plait-fragment';
1439
1480
  * Generated bundle index. Do not edit.
1440
1481
  */
1441
1482
 
1442
- 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 };
1483
+ 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 };
1443
1484
  //# sourceMappingURL=plait-core.mjs.map