@plait/common 0.32.0 → 0.34.0

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.
@@ -1,4 +1,4 @@
1
- import { createG, RectangleClient, drawRectangle, drawCircle, PlaitBoard, createForeignObject, updateForeignObject, getSelectedElements, PlaitElement, Transforms, PlaitPointerType, isMainPointer, transformPoint, toPoint, preventTouchMove, distanceBetweenPointAndPoint, PRESS_AND_MOVE_BUFFER, throttleRAF, handleTouchTarget, MERGING, Direction, Point, hotkeys, PlaitContextService, downScale, PlaitPluginElementComponent, isSelectionMoving, ACTIVE_STROKE_WIDTH } from '@plait/core';
1
+ import { createG, RectangleClient, drawRectangle, drawCircle, PlaitBoard, createForeignObject, updateForeignObject, Direction, distanceBetweenPointAndPoint, Point, hotkeys, PlaitElement, PlaitContextService, downScale, getSelectedElements, Transforms, PlaitPointerType, isMainPointer, transformPoint, toPoint, preventTouchMove, PRESS_AND_MOVE_BUFFER, throttleRAF, handleTouchTarget, MERGING, PlaitPluginElementComponent, isSelectionMoving, ACTIVE_STROKE_WIDTH } from '@plait/core';
2
2
  import { isKeyHotkey } from 'is-hotkey';
3
3
  import * as i0 from '@angular/core';
4
4
  import { Directive, Input } from '@angular/core';
@@ -42,11 +42,11 @@ class Generator {
42
42
  this.board = board;
43
43
  this.options = options;
44
44
  }
45
- draw(element, parentG, data) {
45
+ processDrawing(element, parentG, data) {
46
46
  if (this.canDraw && this.canDraw(element, data)) {
47
- const g = this.baseDraw(element, data);
47
+ const g = this.draw(element, data);
48
48
  if (g) {
49
- if (this.g) {
49
+ if (this.g && parentG.contains(this.g)) {
50
50
  this.g.replaceWith(g);
51
51
  }
52
52
  else {
@@ -94,7 +94,7 @@ class ActiveGenerator extends Generator {
94
94
  return false;
95
95
  }
96
96
  }
97
- baseDraw(element, data) {
97
+ draw(element, data) {
98
98
  const activeG = createG();
99
99
  const rectangle = this.options.getRectangle(element);
100
100
  const delta = this.options.getStrokeWidth();
@@ -131,7 +131,7 @@ class ImageGenerator extends Generator {
131
131
  canDraw(element, data) {
132
132
  return !!this.options.getImageItem(element);
133
133
  }
134
- baseDraw(element, viewContainerRef) {
134
+ draw(element, viewContainerRef) {
135
135
  const g = createG();
136
136
  const foreignRectangle = this.options.getRectangle(element);
137
137
  this.foreignObject = createForeignObject(foreignRectangle.x, foreignRectangle.y, foreignRectangle.width, foreignRectangle.height);
@@ -173,54 +173,6 @@ class ImageGenerator extends Generator {
173
173
  }
174
174
  }
175
175
 
176
- const setProperty = (board, options, callback) => {
177
- const selectedElements = getSelectedElements(board);
178
- selectedElements.forEach(element => {
179
- const path = PlaitBoard.findPath(board, element);
180
- if (callback) {
181
- const component = PlaitElement.getComponent(element);
182
- callback(component, path);
183
- }
184
- else {
185
- Transforms.setNode(board, options, path);
186
- }
187
- });
188
- };
189
-
190
- const setStrokeStyle = (board, strokeStyle) => {
191
- setProperty(board, { strokeStyle });
192
- };
193
- const setFillColor = (board, fill) => {
194
- setProperty(board, { fill });
195
- };
196
- const setStrokeColor = (board, strokeColor) => {
197
- setProperty(board, { strokeColor });
198
- };
199
- const setStrokeWidth = (board, strokeWidth) => {
200
- setProperty(board, { strokeWidth });
201
- };
202
- const PropertyTransforms = {
203
- setFillColor,
204
- setStrokeStyle,
205
- setProperty,
206
- setStrokeWidth,
207
- setStrokeColor
208
- };
209
-
210
- const normalizeShapePoints = (points, shift = false) => {
211
- let start = points[0];
212
- let end = points[1];
213
- if (shift) {
214
- const width = Math.abs(start[0] - end[0]);
215
- const height = Math.abs(start[1] - end[1]);
216
- const edge = Math.max(height, width);
217
- end = [start[0] + (end[0] > start[0] ? edge : -edge), start[1] + (end[1] > start[1] ? edge : -edge)];
218
- }
219
- const leftTopPoint = [Math.min(start[0], end[0]), Math.min(start[1], end[1])];
220
- let rightBottomPoint = [Math.max(start[0], end[0]), Math.max(start[1], end[1])];
221
- return [leftTopPoint, rightBottomPoint];
222
- };
223
-
224
176
  const getResizeHandleByIndex = (index) => {
225
177
  switch (index) {
226
178
  case 0:
@@ -312,107 +264,6 @@ const removeResizing = (board, key) => {
312
264
  IS_RESIZING.delete(board);
313
265
  };
314
266
 
315
- const generalCanResize = (board, event) => {
316
- return (PlaitBoard.isReadonly(board) ||
317
- PlaitBoard.hasBeenTextEditing(board) ||
318
- !PlaitBoard.isPointer(board, PlaitPointerType.hand) ||
319
- !isMainPointer(event));
320
- };
321
- const withResize = (board, options) => {
322
- const { pointerDown, pointerMove, globalPointerUp } = board;
323
- let resizeDetectResult = null;
324
- let resizeRef = null;
325
- let startPoint = null;
326
- let hoveDetectResult = null;
327
- board.pointerDown = (event) => {
328
- if (!options.canResize() || !generalCanResize(board, event)) {
329
- pointerDown(event);
330
- return;
331
- }
332
- const point = transformPoint(board, toPoint(event.x, event.y, PlaitBoard.getHost(board)));
333
- resizeDetectResult = options.detect(point);
334
- if (resizeDetectResult) {
335
- if (resizeDetectResult.cursorClass) {
336
- PlaitBoard.getBoardContainer(board).classList.add(`${resizeDetectResult.cursorClass}`);
337
- }
338
- startPoint = [event.x, event.y];
339
- resizeRef = {
340
- path: PlaitBoard.findPath(board, resizeDetectResult.element),
341
- element: resizeDetectResult.element,
342
- handle: resizeDetectResult.handle
343
- };
344
- preventTouchMove(board, event, true);
345
- }
346
- pointerDown(event);
347
- };
348
- board.pointerMove = (event) => {
349
- if (!options.canResize() || !generalCanResize(board, event)) {
350
- pointerMove(event);
351
- return;
352
- }
353
- if (startPoint && resizeDetectResult && !isResizing(board)) {
354
- // prevent text from being selected
355
- event.preventDefault();
356
- const endPoint = transformPoint(board, toPoint(event.x, event.y, PlaitBoard.getHost(board)));
357
- const distance = distanceBetweenPointAndPoint(startPoint[0], startPoint[1], endPoint[0], endPoint[1]);
358
- if (distance > PRESS_AND_MOVE_BUFFER) {
359
- addResizing(board, resizeRef, options.key);
360
- }
361
- }
362
- if (isResizing(board) && startPoint) {
363
- // prevent text from being selected
364
- event.preventDefault();
365
- const endTransformPoint = transformPoint(board, toPoint(event.x, event.y, PlaitBoard.getHost(board)));
366
- throttleRAF(() => {
367
- const endPoint = [event.x, event.y];
368
- if (startPoint && resizeRef) {
369
- handleTouchTarget(board);
370
- const offsetX = endPoint[0] - startPoint[0];
371
- const offsetY = endPoint[1] - startPoint[1];
372
- options.onResize(resizeRef, { offsetX, offsetY, endTransformPoint });
373
- MERGING.set(board, true);
374
- }
375
- });
376
- return;
377
- }
378
- else {
379
- const point = transformPoint(board, toPoint(event.x, event.y, PlaitBoard.getHost(board)));
380
- const resizeDetectResult = options.detect(point);
381
- if (resizeDetectResult) {
382
- if (hoveDetectResult && resizeDetectResult.cursorClass !== hoveDetectResult.cursorClass) {
383
- PlaitBoard.getBoardContainer(board).classList.remove(`${hoveDetectResult.cursorClass}`);
384
- }
385
- hoveDetectResult = resizeDetectResult;
386
- if (hoveDetectResult.cursorClass) {
387
- PlaitBoard.getBoardContainer(board).classList.add(`${hoveDetectResult.cursorClass}`);
388
- }
389
- }
390
- else {
391
- if (hoveDetectResult) {
392
- if (hoveDetectResult.cursorClass) {
393
- PlaitBoard.getBoardContainer(board).classList.remove(`${hoveDetectResult.cursorClass}`);
394
- }
395
- hoveDetectResult = null;
396
- }
397
- }
398
- }
399
- pointerMove(event);
400
- };
401
- board.globalPointerUp = (event) => {
402
- globalPointerUp(event);
403
- if (isResizing(board) || resizeDetectResult) {
404
- options.endResize && options.endResize(resizeRef);
405
- removeResizing(board, options.key);
406
- startPoint = null;
407
- resizeDetectResult = null;
408
- resizeRef = null;
409
- MERGING.set(board, false);
410
- preventTouchMove(board, event, false);
411
- }
412
- };
413
- return board;
414
- };
415
-
416
267
  const handleDirectionFactors = {
417
268
  [Direction.left]: { x: -1, y: 0 },
418
269
  [Direction.right]: { x: 1, y: 0 },
@@ -1156,6 +1007,164 @@ const getNextPoint = (point, outerRectangle, direction) => {
1156
1007
  }
1157
1008
  };
1158
1009
 
1010
+ const map = new Map();
1011
+ const memorizeLatest = (memorizedKey, propertyKey, propertyValue) => {
1012
+ let obj = map.has(memorizedKey) ? map.get(memorizedKey) : {};
1013
+ obj[propertyKey] = propertyValue;
1014
+ map.set(memorizedKey, obj);
1015
+ };
1016
+ const getMemorizedLatest = (memorizedKey) => {
1017
+ return map.get(memorizedKey);
1018
+ };
1019
+
1020
+ const setProperty = (board, properties, options) => {
1021
+ const selectedElements = getSelectedElements(board);
1022
+ selectedElements.forEach(element => {
1023
+ const path = PlaitBoard.findPath(board, element);
1024
+ const memorizeKey = options?.getMemorizeKey ? options?.getMemorizeKey(element) : '';
1025
+ for (let key in properties) {
1026
+ memorizeKey && memorizeLatest(memorizeKey, key, properties[key]);
1027
+ }
1028
+ if (options?.callback) {
1029
+ options.callback(element, path);
1030
+ }
1031
+ else {
1032
+ Transforms.setNode(board, properties, path);
1033
+ }
1034
+ });
1035
+ };
1036
+ const setStrokeStyle = (board, strokeStyle) => {
1037
+ setProperty(board, { strokeStyle });
1038
+ };
1039
+ const setFillColor = (board, fill) => {
1040
+ setProperty(board, { fill });
1041
+ };
1042
+ const setStrokeColor = (board, strokeColor) => {
1043
+ setProperty(board, { strokeColor });
1044
+ };
1045
+ const setStrokeWidth = (board, strokeWidth) => {
1046
+ setProperty(board, { strokeWidth });
1047
+ };
1048
+ const PropertyTransforms = {
1049
+ setFillColor,
1050
+ setStrokeStyle,
1051
+ setProperty,
1052
+ setStrokeWidth,
1053
+ setStrokeColor
1054
+ };
1055
+
1056
+ const normalizeShapePoints = (points, shift = false) => {
1057
+ let start = points[0];
1058
+ let end = points[1];
1059
+ if (shift) {
1060
+ const width = Math.abs(start[0] - end[0]);
1061
+ const height = Math.abs(start[1] - end[1]);
1062
+ const edge = Math.max(height, width);
1063
+ end = [start[0] + (end[0] > start[0] ? edge : -edge), start[1] + (end[1] > start[1] ? edge : -edge)];
1064
+ }
1065
+ const leftTopPoint = [Math.min(start[0], end[0]), Math.min(start[1], end[1])];
1066
+ let rightBottomPoint = [Math.max(start[0], end[0]), Math.max(start[1], end[1])];
1067
+ return [leftTopPoint, rightBottomPoint];
1068
+ };
1069
+
1070
+ const generalCanResize = (board, event) => {
1071
+ return (!PlaitBoard.isReadonly(board) && !PlaitBoard.hasBeenTextEditing(board) && PlaitBoard.isPointer(board, PlaitPointerType.selection));
1072
+ };
1073
+ const withResize = (board, options) => {
1074
+ const { pointerDown, pointerMove, globalPointerUp } = board;
1075
+ let resizeDetectResult = null;
1076
+ let resizeRef = null;
1077
+ let startPoint = null;
1078
+ let hoveDetectResult = null;
1079
+ board.pointerDown = (event) => {
1080
+ if (!options.canResize() || !generalCanResize(board, event) || !isMainPointer(event)) {
1081
+ pointerDown(event);
1082
+ return;
1083
+ }
1084
+ const point = transformPoint(board, toPoint(event.x, event.y, PlaitBoard.getHost(board)));
1085
+ resizeDetectResult = options.detect(point);
1086
+ if (resizeDetectResult) {
1087
+ if (resizeDetectResult.cursorClass) {
1088
+ PlaitBoard.getBoardContainer(board).classList.add(`${resizeDetectResult.cursorClass}`);
1089
+ }
1090
+ startPoint = [event.x, event.y];
1091
+ resizeRef = {
1092
+ path: PlaitBoard.findPath(board, resizeDetectResult.element),
1093
+ element: resizeDetectResult.element,
1094
+ handle: resizeDetectResult.handle
1095
+ };
1096
+ preventTouchMove(board, event, true);
1097
+ }
1098
+ pointerDown(event);
1099
+ };
1100
+ board.pointerMove = (event) => {
1101
+ if (!options.canResize() || !generalCanResize(board, event)) {
1102
+ pointerMove(event);
1103
+ return;
1104
+ }
1105
+ if (startPoint && resizeDetectResult && !isResizing(board)) {
1106
+ // prevent text from being selected
1107
+ event.preventDefault();
1108
+ const endPoint = transformPoint(board, toPoint(event.x, event.y, PlaitBoard.getHost(board)));
1109
+ const distance = distanceBetweenPointAndPoint(startPoint[0], startPoint[1], endPoint[0], endPoint[1]);
1110
+ if (distance > PRESS_AND_MOVE_BUFFER) {
1111
+ addResizing(board, resizeRef, options.key);
1112
+ }
1113
+ }
1114
+ if (isResizing(board) && startPoint) {
1115
+ // prevent text from being selected
1116
+ event.preventDefault();
1117
+ const endTransformPoint = transformPoint(board, toPoint(event.x, event.y, PlaitBoard.getHost(board)));
1118
+ throttleRAF(() => {
1119
+ const endPoint = [event.x, event.y];
1120
+ if (startPoint && resizeRef) {
1121
+ handleTouchTarget(board);
1122
+ const offsetX = endPoint[0] - startPoint[0];
1123
+ const offsetY = endPoint[1] - startPoint[1];
1124
+ options.onResize(resizeRef, { offsetX, offsetY, endTransformPoint });
1125
+ MERGING.set(board, true);
1126
+ }
1127
+ });
1128
+ return;
1129
+ }
1130
+ else {
1131
+ const point = transformPoint(board, toPoint(event.x, event.y, PlaitBoard.getHost(board)));
1132
+ const resizeDetectResult = options.detect(point);
1133
+ if (resizeDetectResult) {
1134
+ if (hoveDetectResult && resizeDetectResult.cursorClass !== hoveDetectResult.cursorClass) {
1135
+ PlaitBoard.getBoardContainer(board).classList.remove(`${hoveDetectResult.cursorClass}`);
1136
+ }
1137
+ hoveDetectResult = resizeDetectResult;
1138
+ if (hoveDetectResult.cursorClass) {
1139
+ PlaitBoard.getBoardContainer(board).classList.add(`${hoveDetectResult.cursorClass}`);
1140
+ }
1141
+ }
1142
+ else {
1143
+ if (hoveDetectResult) {
1144
+ if (hoveDetectResult.cursorClass) {
1145
+ PlaitBoard.getBoardContainer(board).classList.remove(`${hoveDetectResult.cursorClass}`);
1146
+ }
1147
+ hoveDetectResult = null;
1148
+ }
1149
+ }
1150
+ }
1151
+ pointerMove(event);
1152
+ };
1153
+ board.globalPointerUp = (event) => {
1154
+ globalPointerUp(event);
1155
+ if (isResizing(board) || resizeDetectResult) {
1156
+ options.endResize && options.endResize(resizeRef);
1157
+ removeResizing(board, options.key);
1158
+ startPoint = null;
1159
+ resizeDetectResult = null;
1160
+ resizeRef = null;
1161
+ MERGING.set(board, false);
1162
+ preventTouchMove(board, event, false);
1163
+ }
1164
+ };
1165
+ return board;
1166
+ };
1167
+
1159
1168
  class CommonPluginElement extends PlaitPluginElementComponent {
1160
1169
  constructor() {
1161
1170
  super(...arguments);
@@ -1226,7 +1235,7 @@ class ImageBaseComponent {
1226
1235
  drawFocus() {
1227
1236
  if (this.initialized) {
1228
1237
  const activeG = PlaitBoard.getElementActiveHost(this.board);
1229
- this.activeGenerator.draw({}, activeG, { selected: this._isFocus });
1238
+ this.activeGenerator.processDrawing({}, activeG, { selected: this._isFocus });
1230
1239
  }
1231
1240
  }
1232
1241
  ngOnDestroy() {
@@ -1266,5 +1275,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImpo
1266
1275
  * Generated bundle index. Do not edit.
1267
1276
  */
1268
1277
 
1269
- export { AStar, ActiveGenerator, BASE, BoardCreationMode, CommonPluginElement, DEFAULT_ROUTE_MARGIN, Generator, IS_RESIZING, ImageBaseComponent, ImageGenerator, MediaKeys, PICTURE_ACCEPTED_UPLOAD_SIZE, PRIMARY_COLOR, PointGraph, PointNode, PriorityQueue, PropertyTransforms, RESIZE_HANDLE_DIAMETER, ResizeCursorClass, ResizeHandle, TRANSPARENT, WithCommonPluginKey, WithTextPluginKey, acceptImageTypes, addElementOfFocusedImage, addResizing, buildImage, calculatePolylineLength, createGraph, generateElbowLineRoute, getCreationMode, getDirection, getDirectionBetweenPointAndPoint, getDirectionByPointOfRectangle, getDirectionByVector, getDirectionFactor, getEdgeCenter, getElementOfFocusedImage, getExtendPoint, getFactorByPoints, getGraphPoints, getNextPoint, getOppositeDirection, getPointByVector, getPointOnPolyline, getPoints, getRatioByPoint, getRectangleByPoints, getRectangleResizeHandleRefs, getTextEditors, getTextManages, hasAfterDraw, isDelete, isDndMode, isDrawingMode, isEnterHotkey, isExpandHotkey, isPointOnLineSegment, isResizing, isResizingByCondition, isSpaceHotkey, isTabHotkey, isVirtualKey, normalizeShapePoints, reduceRouteMargin, removeDuplicatePoints, removeElementOfFocusedImage, removeResizing, rotateVectorAnti90, selectImage, setCreationMode, withResize };
1278
+ export { AStar, ActiveGenerator, BASE, BoardCreationMode, CommonPluginElement, DEFAULT_ROUTE_MARGIN, Generator, IS_RESIZING, ImageBaseComponent, ImageGenerator, MediaKeys, PICTURE_ACCEPTED_UPLOAD_SIZE, PRIMARY_COLOR, PointGraph, PointNode, PriorityQueue, PropertyTransforms, RESIZE_HANDLE_DIAMETER, ResizeCursorClass, ResizeHandle, TRANSPARENT, WithCommonPluginKey, WithTextPluginKey, acceptImageTypes, addElementOfFocusedImage, addResizing, buildImage, calculatePolylineLength, createGraph, generateElbowLineRoute, getCreationMode, getDirection, getDirectionBetweenPointAndPoint, getDirectionByPointOfRectangle, getDirectionByVector, getDirectionFactor, getEdgeCenter, getElementOfFocusedImage, getExtendPoint, getFactorByPoints, getGraphPoints, getMemorizedLatest, getNextPoint, getOppositeDirection, getPointByVector, getPointOnPolyline, getPoints, getRatioByPoint, getRectangleByPoints, getRectangleResizeHandleRefs, getTextEditors, getTextManages, hasAfterDraw, isDelete, isDndMode, isDrawingMode, isEnterHotkey, isExpandHotkey, isPointOnLineSegment, isResizing, isResizingByCondition, isSpaceHotkey, isTabHotkey, isVirtualKey, memorizeLatest, normalizeShapePoints, reduceRouteMargin, removeDuplicatePoints, removeElementOfFocusedImage, removeResizing, rotateVectorAnti90, routeAdjust, selectImage, setCreationMode, setProperty, withResize };
1270
1279
  //# sourceMappingURL=plait-common.mjs.map