@buerli.io/core 1.0.0 → 1.0.1-beta.2

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.
Files changed (74) hide show
  1. package/README.md +19 -19
  2. package/build/cjs/geometry/ThreeGeometryCreator.js +320 -320
  3. package/build/cjs/geometry/index.js +17 -17
  4. package/build/cjs/graphic/filters.js +25 -25
  5. package/build/cjs/index.js +26 -26
  6. package/build/cjs/re-export.js +29 -29
  7. package/build/cjs/store/index.js +37 -37
  8. package/build/cjs/store/states/buerli.js +98 -98
  9. package/build/cjs/store/states/cad.js +47 -47
  10. package/build/cjs/store/states/drawing.js +49 -49
  11. package/build/cjs/store/states/geometry.js +157 -157
  12. package/build/cjs/store/states/graphic.js +115 -115
  13. package/build/cjs/store/states/interaction.js +55 -55
  14. package/build/cjs/store/states/message.js +25 -25
  15. package/build/cjs/store/states/plugin.js +115 -115
  16. package/build/cjs/store/states/selection.js +267 -267
  17. package/build/cjs/store/states/structure.js +250 -250
  18. package/build/cjs/store/store.js +9 -9
  19. package/build/cjs/store/subscribtion/selection.js +49 -49
  20. package/build/cjs/store/subscribtion/subscribeToDrawing.js +13 -13
  21. package/build/cjs/types/InteractionInfo.js +11 -11
  22. package/build/cjs/types/index.js +34 -34
  23. package/build/cjs/utils/Math.js +91 -91
  24. package/build/cjs/utils/Measure.js +576 -576
  25. package/build/cjs/utils/Timing.js +34 -34
  26. package/build/esm/geometry/ThreeGeometryCreator.js +291 -291
  27. package/build/esm/geometry/index.js +1 -1
  28. package/build/esm/graphic/filters.js +12 -12
  29. package/build/esm/index.js +10 -10
  30. package/build/esm/re-export.js +23 -23
  31. package/build/esm/store/index.js +26 -26
  32. package/build/esm/store/states/buerli.js +92 -92
  33. package/build/esm/store/states/cad.js +42 -42
  34. package/build/esm/store/states/drawing.js +45 -45
  35. package/build/esm/store/states/geometry.js +129 -129
  36. package/build/esm/store/states/graphic.js +110 -110
  37. package/build/esm/store/states/interaction.js +50 -50
  38. package/build/esm/store/states/message.js +21 -21
  39. package/build/esm/store/states/plugin.js +109 -109
  40. package/build/esm/store/states/selection.js +261 -261
  41. package/build/esm/store/states/structure.js +219 -219
  42. package/build/esm/store/store.js +3 -3
  43. package/build/esm/store/subscribtion/selection.js +44 -44
  44. package/build/esm/store/subscribtion/subscribeToDrawing.js +9 -9
  45. package/build/esm/types/InteractionInfo.js +6 -6
  46. package/build/esm/types/index.js +16 -16
  47. package/build/esm/utils/Math.js +65 -65
  48. package/build/esm/utils/Measure.js +550 -550
  49. package/build/esm/utils/Timing.js +31 -31
  50. package/build/types/geometry/ThreeGeometryCreator.d.ts +256 -256
  51. package/build/types/geometry/index.d.ts +1 -1
  52. package/build/types/graphic/filters.d.ts +11 -11
  53. package/build/types/index.d.ts +38 -38
  54. package/build/types/re-export.d.ts +70 -70
  55. package/build/types/store/index.d.ts +16 -16
  56. package/build/types/store/states/buerli.d.ts +41 -41
  57. package/build/types/store/states/cad.d.ts +56 -56
  58. package/build/types/store/states/drawing.d.ts +45 -45
  59. package/build/types/store/states/geometry.d.ts +64 -64
  60. package/build/types/store/states/graphic.d.ts +21 -21
  61. package/build/types/store/states/interaction.d.ts +34 -34
  62. package/build/types/store/states/message.d.ts +30 -30
  63. package/build/types/store/states/plugin.d.ts +53 -53
  64. package/build/types/store/states/selection.d.ts +69 -69
  65. package/build/types/store/states/structure.d.ts +47 -47
  66. package/build/types/store/store.d.ts +2 -2
  67. package/build/types/store/subscribtion/selection.d.ts +7 -7
  68. package/build/types/store/subscribtion/subscribeToDrawing.d.ts +4 -4
  69. package/build/types/types/InteractionInfo.d.ts +42 -42
  70. package/build/types/types/index.d.ts +7 -7
  71. package/build/types/utils/Math.d.ts +21 -21
  72. package/build/types/utils/Measure.d.ts +64 -64
  73. package/build/types/utils/Timing.d.ts +8 -8
  74. package/package.json +45 -45
@@ -1,115 +1,115 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.graphicApi = exports.graphicData = void 0;
4
- ////////////////////////////////////////////////////
5
- // DATA
6
- ////////////////////////////////////////////////////
7
- const graphicData = () => ({
8
- containers: {},
9
- });
10
- exports.graphicData = graphicData;
11
- ////////////////////////////////////////////////////
12
- // API
13
- ////////////////////////////////////////////////////
14
- const graphicApi = (set, get, buerliApi) => ({
15
- addPackages: (packages) => {
16
- // Timing.dump('load', `add graphic packages ${graphic.length} ${graphic[0].containers.length}`)
17
- if (!Array.isArray(packages)) {
18
- packages = [packages];
19
- }
20
- let cacheChanged = false;
21
- const newChache = Object.assign({}, get().geometry.cache);
22
- const addedContainers = {};
23
- packages.forEach(gr => {
24
- gr.containers.forEach(cont => {
25
- addedContainers[cont.id] = cont;
26
- // If the cached geometry for the added container already exist,
27
- // it has to be removed from the cache to ensure geometry updates.
28
- if (newChache[cont.id]) {
29
- delete newChache[cont.id];
30
- cacheChanged = true;
31
- }
32
- });
33
- });
34
- set(state => ({
35
- graphic: Object.assign(Object.assign({}, state.graphic), { containers: Object.assign(Object.assign({}, state.graphic.containers), addedContainers) }),
36
- geometry: cacheChanged ? Object.assign(Object.assign({}, state.geometry), { cache: newChache, stamp: Date.now() }) : state.geometry,
37
- }));
38
- const { graphic, api } = get();
39
- /**
40
- * Update the bounds with the newly added graphic.
41
- */
42
- api.geometry.updateBounds();
43
- /**
44
- * Create the geometry for the graphic packages.
45
- * Only the geometry which does not already exist will be created (@see createGeometryIfNotCached).
46
- */
47
- const containerIds = Object.getOwnPropertyNames(graphic.containers);
48
- for (const id of containerIds) {
49
- api.geometry.createGeometryIfNotCached(id);
50
- }
51
- // console.log('graphicContainers after add:', Object.getOwnPropertyNames(get().graphic.containers))
52
- },
53
- cleanupGraphicRecords: () => {
54
- const { graphic, geometry, api } = get();
55
- // collect the used solid ids from the structure tree
56
- const solidIds = api.structure.collectUsedSolidIds();
57
- // console.log('solidIds before cleanup:', solidIds)
58
- // cleanup the graphic containers in the state
59
- const usedContainers = {};
60
- const existingContainers = graphic.containers;
61
- const existingContainerIds = Object.getOwnPropertyNames(existingContainers);
62
- if (existingContainerIds.length > 0) {
63
- existingContainerIds.forEach(contIdStr => {
64
- const contId = Number(contIdStr);
65
- if (solidIds.indexOf(contId) >= 0) {
66
- usedContainers[contId] = existingContainers[contId];
67
- }
68
- });
69
- set({ graphic: Object.assign(Object.assign({}, graphic), { containers: usedContainers }) });
70
- // console.log('graphicContainers after cleanup:', Object.getOwnPropertyNames(get().graphic.ontainers))
71
- }
72
- // cleanup the cached geometry depending on the uses solids
73
- const usedGeometry = {};
74
- const existingGeometry = geometry.cache;
75
- const existingGeometryIds = Object.getOwnPropertyNames(existingGeometry);
76
- if (existingGeometryIds.length > 0) {
77
- existingGeometryIds.forEach(contIdStr => {
78
- const contId = Number(contIdStr);
79
- if (solidIds.indexOf(contId) >= 0) {
80
- usedGeometry[contId] = existingGeometry[contId];
81
- }
82
- });
83
- set({ geometry: Object.assign(Object.assign({}, geometry), { cache: usedGeometry, stamp: Date.now() }) });
84
- }
85
- },
86
- filter: (cb, options) => {
87
- const result = [];
88
- const { containers } = get().graphic;
89
- const contIds = Object.getOwnPropertyNames(containers).map(n => Number(n));
90
- for (const contId of contIds) {
91
- if ((options === null || options === void 0 ? void 0 : options.containerId) && contId !== options.containerId) {
92
- continue;
93
- }
94
- const cont = containers[contId];
95
- const items = [
96
- ...((cont === null || cont === void 0 ? void 0 : cont.arcs) || []),
97
- ...((cont === null || cont === void 0 ? void 0 : cont.cones) || []),
98
- ...((cont === null || cont === void 0 ? void 0 : cont.edges) || []),
99
- ...((cont === null || cont === void 0 ? void 0 : cont.lines) || []),
100
- ...((cont === null || cont === void 0 ? void 0 : cont.meshes) || []),
101
- ...((cont === null || cont === void 0 ? void 0 : cont.vertices) || []),
102
- ];
103
- for (const elem of items) {
104
- if (cb(elem)) {
105
- result.push({ elem, solidId: cont.owner, containerId: cont.id });
106
- if (options === null || options === void 0 ? void 0 : options.onlyOne) {
107
- return result;
108
- }
109
- }
110
- }
111
- }
112
- return result;
113
- },
114
- });
115
- exports.graphicApi = graphicApi;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.graphicApi = exports.graphicData = void 0;
4
+ ////////////////////////////////////////////////////
5
+ // DATA
6
+ ////////////////////////////////////////////////////
7
+ const graphicData = () => ({
8
+ containers: {},
9
+ });
10
+ exports.graphicData = graphicData;
11
+ ////////////////////////////////////////////////////
12
+ // API
13
+ ////////////////////////////////////////////////////
14
+ const graphicApi = (set, get, buerliApi) => ({
15
+ addPackages: (packages) => {
16
+ // Timing.dump('load', `add graphic packages ${graphic.length} ${graphic[0].containers.length}`)
17
+ if (!Array.isArray(packages)) {
18
+ packages = [packages];
19
+ }
20
+ let cacheChanged = false;
21
+ const newChache = Object.assign({}, get().geometry.cache);
22
+ const addedContainers = {};
23
+ packages.forEach(gr => {
24
+ gr.containers.forEach(cont => {
25
+ addedContainers[cont.id] = cont;
26
+ // If the cached geometry for the added container already exist,
27
+ // it has to be removed from the cache to ensure geometry updates.
28
+ if (newChache[cont.id]) {
29
+ delete newChache[cont.id];
30
+ cacheChanged = true;
31
+ }
32
+ });
33
+ });
34
+ set(state => ({
35
+ graphic: Object.assign(Object.assign({}, state.graphic), { containers: Object.assign(Object.assign({}, state.graphic.containers), addedContainers) }),
36
+ geometry: cacheChanged ? Object.assign(Object.assign({}, state.geometry), { cache: newChache, stamp: Date.now() }) : state.geometry,
37
+ }));
38
+ const { graphic, api } = get();
39
+ /**
40
+ * Update the bounds with the newly added graphic.
41
+ */
42
+ api.geometry.updateBounds();
43
+ /**
44
+ * Create the geometry for the graphic packages.
45
+ * Only the geometry which does not already exist will be created (@see createGeometryIfNotCached).
46
+ */
47
+ const containerIds = Object.getOwnPropertyNames(graphic.containers);
48
+ for (const id of containerIds) {
49
+ api.geometry.createGeometryIfNotCached(id);
50
+ }
51
+ // console.log('graphicContainers after add:', Object.getOwnPropertyNames(get().graphic.containers))
52
+ },
53
+ cleanupGraphicRecords: () => {
54
+ const { graphic, geometry, api } = get();
55
+ // collect the used solid ids from the structure tree
56
+ const solidIds = api.structure.collectUsedSolidIds();
57
+ // console.log('solidIds before cleanup:', solidIds)
58
+ // cleanup the graphic containers in the state
59
+ const usedContainers = {};
60
+ const existingContainers = graphic.containers;
61
+ const existingContainerIds = Object.getOwnPropertyNames(existingContainers);
62
+ if (existingContainerIds.length > 0) {
63
+ existingContainerIds.forEach(contIdStr => {
64
+ const contId = Number(contIdStr);
65
+ if (solidIds.indexOf(contId) >= 0) {
66
+ usedContainers[contId] = existingContainers[contId];
67
+ }
68
+ });
69
+ set({ graphic: Object.assign(Object.assign({}, graphic), { containers: usedContainers }) });
70
+ // console.log('graphicContainers after cleanup:', Object.getOwnPropertyNames(get().graphic.ontainers))
71
+ }
72
+ // cleanup the cached geometry depending on the uses solids
73
+ const usedGeometry = {};
74
+ const existingGeometry = geometry.cache;
75
+ const existingGeometryIds = Object.getOwnPropertyNames(existingGeometry);
76
+ if (existingGeometryIds.length > 0) {
77
+ existingGeometryIds.forEach(contIdStr => {
78
+ const contId = Number(contIdStr);
79
+ if (solidIds.indexOf(contId) >= 0) {
80
+ usedGeometry[contId] = existingGeometry[contId];
81
+ }
82
+ });
83
+ set({ geometry: Object.assign(Object.assign({}, geometry), { cache: usedGeometry, stamp: Date.now() }) });
84
+ }
85
+ },
86
+ filter: (cb, options) => {
87
+ const result = [];
88
+ const { containers } = get().graphic;
89
+ const contIds = Object.getOwnPropertyNames(containers).map(n => Number(n));
90
+ for (const contId of contIds) {
91
+ if ((options === null || options === void 0 ? void 0 : options.containerId) && contId !== options.containerId) {
92
+ continue;
93
+ }
94
+ const cont = containers[contId];
95
+ const items = [
96
+ ...((cont === null || cont === void 0 ? void 0 : cont.arcs) || []),
97
+ ...((cont === null || cont === void 0 ? void 0 : cont.cones) || []),
98
+ ...((cont === null || cont === void 0 ? void 0 : cont.edges) || []),
99
+ ...((cont === null || cont === void 0 ? void 0 : cont.lines) || []),
100
+ ...((cont === null || cont === void 0 ? void 0 : cont.meshes) || []),
101
+ ...((cont === null || cont === void 0 ? void 0 : cont.vertices) || []),
102
+ ];
103
+ for (const elem of items) {
104
+ if (cb(elem)) {
105
+ result.push({ elem, solidId: cont.owner, containerId: cont.id });
106
+ if (options === null || options === void 0 ? void 0 : options.onlyOne) {
107
+ return result;
108
+ }
109
+ }
110
+ }
111
+ }
112
+ return result;
113
+ },
114
+ });
115
+ exports.graphicApi = graphicApi;
@@ -1,55 +1,55 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.interactionApi = exports.interactionData = void 0;
4
- ////////////////////////////////////////////////////
5
- // DATA
6
- ////////////////////////////////////////////////////
7
- const interactionData = () => ({
8
- hovered: null,
9
- selected: [],
10
- });
11
- exports.interactionData = interactionData;
12
- ////////////////////////////////////////////////////
13
- // API
14
- ////////////////////////////////////////////////////
15
- const interactionApi = (set, get, buerliApi) => ({
16
- /**
17
- * Sets the hovered item in the store to the given item.
18
- *
19
- * @param item The item.
20
- */
21
- setHovered: (item) => {
22
- set(state => (Object.assign(Object.assign({}, state), { interaction: Object.assign(Object.assign({}, state.interaction), { hovered: item }) })));
23
- },
24
- /**
25
- * Reset the 'selected' array to the given items.
26
- *
27
- * @param items The items.
28
- */
29
- setSelected: (items) => {
30
- set(state => (Object.assign(Object.assign({}, state), { interaction: Object.assign(Object.assign({}, state.interaction), { selected: items }) })));
31
- },
32
- /**
33
- * Adds the given item to the 'selected' array if it is not yet selected.
34
- * If the item is already selected, it will be removed from the 'selected' array.
35
- *
36
- * With 'multi' enabled, the already selected items will remain selected.
37
- *
38
- * @param item The item to select.
39
- * @param multi Do multi select.
40
- */
41
- select: (item, multi) => {
42
- const setSelected = get().api.interaction.setSelected;
43
- const selected = get().interaction.selected || [];
44
- const newKey = item.uniqueIdent;
45
- let newSel = selected.length === 1 && selected[0].uniqueIdent === newKey ? [] : [item];
46
- if (multi) {
47
- newSel = selected.filter(s => s.uniqueIdent != newKey);
48
- if (newSel.length === selected.length) {
49
- newSel.push(item);
50
- }
51
- }
52
- setSelected(newSel);
53
- },
54
- });
55
- exports.interactionApi = interactionApi;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.interactionApi = exports.interactionData = void 0;
4
+ ////////////////////////////////////////////////////
5
+ // DATA
6
+ ////////////////////////////////////////////////////
7
+ const interactionData = () => ({
8
+ hovered: null,
9
+ selected: [],
10
+ });
11
+ exports.interactionData = interactionData;
12
+ ////////////////////////////////////////////////////
13
+ // API
14
+ ////////////////////////////////////////////////////
15
+ const interactionApi = (set, get, buerliApi) => ({
16
+ /**
17
+ * Sets the hovered item in the store to the given item.
18
+ *
19
+ * @param item The item.
20
+ */
21
+ setHovered: (item) => {
22
+ set(state => (Object.assign(Object.assign({}, state), { interaction: Object.assign(Object.assign({}, state.interaction), { hovered: item }) })));
23
+ },
24
+ /**
25
+ * Reset the 'selected' array to the given items.
26
+ *
27
+ * @param items The items.
28
+ */
29
+ setSelected: (items) => {
30
+ set(state => (Object.assign(Object.assign({}, state), { interaction: Object.assign(Object.assign({}, state.interaction), { selected: items }) })));
31
+ },
32
+ /**
33
+ * Adds the given item to the 'selected' array if it is not yet selected.
34
+ * If the item is already selected, it will be removed from the 'selected' array.
35
+ *
36
+ * With 'multi' enabled, the already selected items will remain selected.
37
+ *
38
+ * @param item The item to select.
39
+ * @param multi Do multi select.
40
+ */
41
+ select: (item, multi) => {
42
+ const setSelected = get().api.interaction.setSelected;
43
+ const selected = get().interaction.selected || [];
44
+ const newKey = item.uniqueIdent;
45
+ let newSel = selected.length === 1 && selected[0].uniqueIdent === newKey ? [] : [item];
46
+ if (multi) {
47
+ newSel = selected.filter(s => s.uniqueIdent != newKey);
48
+ if (newSel.length === selected.length) {
49
+ newSel.push(item);
50
+ }
51
+ }
52
+ setSelected(newSel);
53
+ },
54
+ });
55
+ exports.interactionApi = interactionApi;
@@ -1,25 +1,25 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getInitialMessageState = exports.BuerliMessageScope = void 0;
4
- const uuid_1 = require("uuid");
5
- exports.BuerliMessageScope = 'BuerliMessageScope';
6
- const getInitialMessageState = (set, get, api) => ({
7
- message: {
8
- data: null,
9
- timestamp: -1,
10
- api: messageApi(set, get, api),
11
- },
12
- });
13
- exports.getInitialMessageState = getInitialMessageState;
14
- const messageApi = (set, get, api) => ({
15
- show(message) {
16
- const newId = (0, uuid_1.v4)();
17
- const msg = get().message;
18
- set({ message: Object.assign(Object.assign({}, msg), { data: Object.assign({ id: newId }, message), timestamp: Date.now() }) });
19
- return newId;
20
- },
21
- update(id, message) {
22
- const msg = get().message;
23
- set({ message: Object.assign(Object.assign({}, msg), { data: Object.assign({ id }, message), timestamp: Date.now() }) });
24
- },
25
- });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getInitialMessageState = exports.BuerliMessageScope = void 0;
4
+ const uuid_1 = require("uuid");
5
+ exports.BuerliMessageScope = 'BuerliMessageScope';
6
+ const getInitialMessageState = (set, get, api) => ({
7
+ message: {
8
+ data: null,
9
+ timestamp: -1,
10
+ api: messageApi(set, get, api),
11
+ },
12
+ });
13
+ exports.getInitialMessageState = getInitialMessageState;
14
+ const messageApi = (set, get, api) => ({
15
+ show(message) {
16
+ const newId = (0, uuid_1.v4)();
17
+ const msg = get().message;
18
+ set({ message: Object.assign(Object.assign({}, msg), { data: Object.assign({ id: newId }, message), timestamp: Date.now() }) });
19
+ return newId;
20
+ },
21
+ update(id, message) {
22
+ const msg = get().message;
23
+ set({ message: Object.assign(Object.assign({}, msg), { data: Object.assign({ id }, message), timestamp: Date.now() }) });
24
+ },
25
+ });
@@ -1,115 +1,115 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isPromisePlugin = exports.pluginApi = exports.pluginData = void 0;
4
- const uuid_1 = require("uuid");
5
- const pluginData = () => ({
6
- feature: [],
7
- global: [],
8
- refs: {},
9
- active: {
10
- feature: null,
11
- global: [],
12
- },
13
- visible: [],
14
- });
15
- exports.pluginData = pluginData;
16
- const pluginApi = (set, get, buerliApi) => ({
17
- setActiveFeature: (id) => {
18
- set(state => {
19
- let plugins = state.plugin.refs;
20
- const oldPlugin = state.plugin.active.feature !== null && state.plugin.refs[state.plugin.active.feature];
21
- if (oldPlugin && state.plugin.active.feature !== id && !oldPlugin.persistent && id !== null) {
22
- // Reset plugin-state when the plugin is re-activated, not persistent and has not been active all along
23
- const initState = oldPlugin.initialState || {};
24
- plugins = Object.assign(Object.assign({}, plugins), { [oldPlugin.id]: Object.assign(Object.assign({}, oldPlugin), { state: typeof initState === 'function' ? initState(get().id, id) : initState }) });
25
- }
26
- return {
27
- plugin: Object.assign(Object.assign({}, state.plugin), { active: Object.assign(Object.assign({}, state.plugin.active), { feature: id }), refs: plugins }),
28
- selection: Object.assign(Object.assign({}, state.selection), { active: null }),
29
- };
30
- });
31
- },
32
- setVisiblePlugin: (id, visible) => {
33
- set(currentState => {
34
- const filtered = currentState.plugin.visible.filter(p => p !== id);
35
- if (visible)
36
- filtered.push(id);
37
- return {
38
- plugin: Object.assign(Object.assign({}, currentState.plugin), { visible: filtered }),
39
- };
40
- });
41
- },
42
- setActiveGlobal: (id, active) => {
43
- set(currentState => {
44
- if (currentState.plugin.global.indexOf(id) === -1) {
45
- console.warn('Given plugin id does not exist.');
46
- return currentState;
47
- }
48
- const filtered = currentState.plugin.active.global.filter(p => p !== id);
49
- if (active)
50
- filtered.push(id);
51
- return {
52
- plugin: Object.assign(Object.assign({}, currentState.plugin), { active: Object.assign(Object.assign({}, currentState.plugin.active), { global: filtered }) }),
53
- };
54
- });
55
- },
56
- // TODO remove defaultId and useEmbeddedPlugin ?
57
- createPlugin: (def, defaultId, drawingId, objectId) => {
58
- const id = defaultId !== void 0 ? defaultId : (0, uuid_1.v4)();
59
- function init(definition) {
60
- const { description, Root = null, View = null } = definition;
61
- const initialState = description.initialState || {};
62
- const localState = typeof initialState === 'function' ? initialState(drawingId, objectId) : initialState;
63
- set(currentState => {
64
- const newPlugin = Object.assign(Object.assign({ id }, description), { drawingId,
65
- objectId, state: localState, Root,
66
- View, set: (sel) => {
67
- // set function can be called when drawing doesn't exist anymore, in this case nothing should be done.
68
- if (get() === undefined)
69
- return;
70
- set(state => {
71
- const newState = typeof sel === 'function' ? sel(state.plugin.refs[id].state) : sel;
72
- return {
73
- plugin: Object.assign(Object.assign({}, state.plugin), { refs: Object.assign(Object.assign({}, state.plugin.refs), { [id]: Object.assign(Object.assign({}, state.plugin.refs[id]), { state: Object.assign(Object.assign({}, state.plugin.refs[id].state), newState) }) }) }),
74
- };
75
- });
76
- } });
77
- return {
78
- plugin: Object.assign(Object.assign({}, currentState.plugin), { feature: description.global
79
- ? currentState.plugin.feature
80
- : [...currentState.plugin.feature, id], global: description.global ? [...currentState.plugin.global, id] : currentState.plugin.global, refs: Object.assign(Object.assign({}, currentState.plugin.refs), { [id]: Object.assign(Object.assign({}, currentState.plugin.refs[id]), newPlugin) }) }),
81
- };
82
- });
83
- }
84
- // Initialize plugin definition, which may be a promise or an object
85
- if (isPromisePlugin(def)) {
86
- // The definition is async, so we create a plugin slot right away and complete it later
87
- init({
88
- description: { author: '', name: '', persistent: false, global: false, version: '' },
89
- Root: null,
90
- View: null,
91
- });
92
- def.then(init).catch();
93
- }
94
- else
95
- init(def);
96
- // Return unsubscribe
97
- return (deactivate = true) => {
98
- // Use 'get' to check if the drawing still exists
99
- get &&
100
- get() &&
101
- set(state => {
102
- const newPlugins = Object.assign({}, state.plugin.refs);
103
- delete newPlugins[id];
104
- return {
105
- plugin: Object.assign(Object.assign({}, state.plugin), { active: Object.assign(Object.assign({}, state.plugin.active), { feature: deactivate ? null : state.plugin.active.feature }), feature: state.plugin.feature.filter(pluginId => pluginId !== id), global: state.plugin.global.filter(pluginId => pluginId !== id), refs: newPlugins }),
106
- };
107
- });
108
- };
109
- },
110
- });
111
- exports.pluginApi = pluginApi;
112
- function isPromisePlugin(def) {
113
- return def.then !== undefined;
114
- }
115
- exports.isPromisePlugin = isPromisePlugin;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPromisePlugin = exports.pluginApi = exports.pluginData = void 0;
4
+ const uuid_1 = require("uuid");
5
+ const pluginData = () => ({
6
+ feature: [],
7
+ global: [],
8
+ refs: {},
9
+ active: {
10
+ feature: null,
11
+ global: [],
12
+ },
13
+ visible: [],
14
+ });
15
+ exports.pluginData = pluginData;
16
+ const pluginApi = (set, get, buerliApi) => ({
17
+ setActiveFeature: (id) => {
18
+ set(state => {
19
+ let plugins = state.plugin.refs;
20
+ const oldPlugin = state.plugin.active.feature !== null && state.plugin.refs[state.plugin.active.feature];
21
+ if (oldPlugin && state.plugin.active.feature !== id && !oldPlugin.persistent && id !== null) {
22
+ // Reset plugin-state when the plugin is re-activated, not persistent and has not been active all along
23
+ const initState = oldPlugin.initialState || {};
24
+ plugins = Object.assign(Object.assign({}, plugins), { [oldPlugin.id]: Object.assign(Object.assign({}, oldPlugin), { state: typeof initState === 'function' ? initState(get().id, id) : initState }) });
25
+ }
26
+ return {
27
+ plugin: Object.assign(Object.assign({}, state.plugin), { active: Object.assign(Object.assign({}, state.plugin.active), { feature: id }), refs: plugins }),
28
+ selection: Object.assign(Object.assign({}, state.selection), { active: null }),
29
+ };
30
+ });
31
+ },
32
+ setVisiblePlugin: (id, visible) => {
33
+ set(currentState => {
34
+ const filtered = currentState.plugin.visible.filter(p => p !== id);
35
+ if (visible)
36
+ filtered.push(id);
37
+ return {
38
+ plugin: Object.assign(Object.assign({}, currentState.plugin), { visible: filtered }),
39
+ };
40
+ });
41
+ },
42
+ setActiveGlobal: (id, active) => {
43
+ set(currentState => {
44
+ if (currentState.plugin.global.indexOf(id) === -1) {
45
+ console.warn('Given plugin id does not exist.');
46
+ return currentState;
47
+ }
48
+ const filtered = currentState.plugin.active.global.filter(p => p !== id);
49
+ if (active)
50
+ filtered.push(id);
51
+ return {
52
+ plugin: Object.assign(Object.assign({}, currentState.plugin), { active: Object.assign(Object.assign({}, currentState.plugin.active), { global: filtered }) }),
53
+ };
54
+ });
55
+ },
56
+ // TODO remove defaultId and useEmbeddedPlugin ?
57
+ createPlugin: (def, defaultId, drawingId, objectId) => {
58
+ const id = defaultId !== void 0 ? defaultId : (0, uuid_1.v4)();
59
+ function init(definition) {
60
+ const { description, Root = null, View = null } = definition;
61
+ const initialState = description.initialState || {};
62
+ const localState = typeof initialState === 'function' ? initialState(drawingId, objectId) : initialState;
63
+ set(currentState => {
64
+ const newPlugin = Object.assign(Object.assign({ id }, description), { drawingId,
65
+ objectId, state: localState, Root,
66
+ View, set: (sel) => {
67
+ // set function can be called when drawing doesn't exist anymore, in this case nothing should be done.
68
+ if (get() === undefined)
69
+ return;
70
+ set(state => {
71
+ const newState = typeof sel === 'function' ? sel(state.plugin.refs[id].state) : sel;
72
+ return {
73
+ plugin: Object.assign(Object.assign({}, state.plugin), { refs: Object.assign(Object.assign({}, state.plugin.refs), { [id]: Object.assign(Object.assign({}, state.plugin.refs[id]), { state: Object.assign(Object.assign({}, state.plugin.refs[id].state), newState) }) }) }),
74
+ };
75
+ });
76
+ } });
77
+ return {
78
+ plugin: Object.assign(Object.assign({}, currentState.plugin), { feature: description.global
79
+ ? currentState.plugin.feature
80
+ : [...currentState.plugin.feature, id], global: description.global ? [...currentState.plugin.global, id] : currentState.plugin.global, refs: Object.assign(Object.assign({}, currentState.plugin.refs), { [id]: Object.assign(Object.assign({}, currentState.plugin.refs[id]), newPlugin) }) }),
81
+ };
82
+ });
83
+ }
84
+ // Initialize plugin definition, which may be a promise or an object
85
+ if (isPromisePlugin(def)) {
86
+ // The definition is async, so we create a plugin slot right away and complete it later
87
+ init({
88
+ description: { author: '', name: '', persistent: false, global: false, version: '' },
89
+ Root: null,
90
+ View: null,
91
+ });
92
+ def.then(init).catch();
93
+ }
94
+ else
95
+ init(def);
96
+ // Return unsubscribe
97
+ return (deactivate = true) => {
98
+ // Use 'get' to check if the drawing still exists
99
+ get &&
100
+ get() &&
101
+ set(state => {
102
+ const newPlugins = Object.assign({}, state.plugin.refs);
103
+ delete newPlugins[id];
104
+ return {
105
+ plugin: Object.assign(Object.assign({}, state.plugin), { active: Object.assign(Object.assign({}, state.plugin.active), { feature: deactivate ? null : state.plugin.active.feature }), feature: state.plugin.feature.filter(pluginId => pluginId !== id), global: state.plugin.global.filter(pluginId => pluginId !== id), refs: newPlugins }),
106
+ };
107
+ });
108
+ };
109
+ },
110
+ });
111
+ exports.pluginApi = pluginApi;
112
+ function isPromisePlugin(def) {
113
+ return def.then !== undefined;
114
+ }
115
+ exports.isPromisePlugin = isPromisePlugin;