smoosic 2.0.1 → 2.0.3

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 (127) hide show
  1. package/build/smoosic.js +171 -78
  2. package/package.json +82 -77
  3. package/release/smoosic.js +171 -78
  4. package/release/styles/fonts/MaterialIconsOutlined-Regular.otf +0 -0
  5. package/release/styles/menus.css +26 -0
  6. package/specs/022-text-reposition-on-resize/spec.md +1 -0
  7. package/specs/027-fix-bottom-modifier-lookup/checklists/requirements.md +38 -0
  8. package/specs/027-fix-bottom-modifier-lookup/contracts/division-lookup-contract.md +51 -0
  9. package/specs/027-fix-bottom-modifier-lookup/data-model.md +43 -0
  10. package/specs/027-fix-bottom-modifier-lookup/plan.md +77 -0
  11. package/specs/027-fix-bottom-modifier-lookup/quickstart.md +36 -0
  12. package/specs/027-fix-bottom-modifier-lookup/research.md +58 -0
  13. package/specs/027-fix-bottom-modifier-lookup/spec.md +105 -0
  14. package/specs/027-fix-bottom-modifier-lookup/tasks.md +167 -0
  15. package/specs/028-unsaved-changes-indicator/checklists/requirements.md +39 -0
  16. package/specs/028-unsaved-changes-indicator/contracts/score-change-contract.md +44 -0
  17. package/specs/028-unsaved-changes-indicator/data-model.md +64 -0
  18. package/specs/028-unsaved-changes-indicator/plan.md +96 -0
  19. package/specs/028-unsaved-changes-indicator/quickstart.md +54 -0
  20. package/specs/028-unsaved-changes-indicator/research.md +68 -0
  21. package/specs/028-unsaved-changes-indicator/spec.md +135 -0
  22. package/specs/028-unsaved-changes-indicator/tasks.md +173 -0
  23. package/specs/029-remove-promise-helpers/checklists/requirements.md +39 -0
  24. package/specs/029-remove-promise-helpers/contracts/public-api-contract.md +32 -0
  25. package/specs/029-remove-promise-helpers/data-model.md +45 -0
  26. package/specs/029-remove-promise-helpers/plan.md +86 -0
  27. package/specs/029-remove-promise-helpers/quickstart.md +53 -0
  28. package/specs/029-remove-promise-helpers/research.md +71 -0
  29. package/specs/029-remove-promise-helpers/spec.md +107 -0
  30. package/specs/029-remove-promise-helpers/tasks.md +164 -0
  31. package/specs/030-copy-paste-voices/checklists/requirements.md +39 -0
  32. package/specs/030-copy-paste-voices/contracts/voice-copy-paste-contract.md +47 -0
  33. package/specs/030-copy-paste-voices/data-model.md +63 -0
  34. package/specs/030-copy-paste-voices/plan.md +80 -0
  35. package/specs/030-copy-paste-voices/quickstart.md +50 -0
  36. package/specs/030-copy-paste-voices/research.md +80 -0
  37. package/specs/030-copy-paste-voices/spec.md +124 -0
  38. package/specs/030-copy-paste-voices/tasks.md +183 -0
  39. package/specs/031-select-voice/checklists/requirements.md +39 -0
  40. package/specs/031-select-voice/contracts/select-voice-contract.md +43 -0
  41. package/specs/031-select-voice/data-model.md +47 -0
  42. package/specs/031-select-voice/plan.md +79 -0
  43. package/specs/031-select-voice/quickstart.md +43 -0
  44. package/specs/031-select-voice/research.md +55 -0
  45. package/specs/031-select-voice/spec.md +113 -0
  46. package/specs/031-select-voice/tasks.md +171 -0
  47. package/src/application/application.ts +464 -459
  48. package/src/common/promiseHelpers.ts +2 -2
  49. package/src/render/audio/samples.ts +117 -118
  50. package/src/render/sui/pageDivisions.ts +58 -0
  51. package/src/render/sui/renderState.ts +469 -464
  52. package/src/render/sui/scoreChangeState.ts +62 -0
  53. package/src/render/sui/scoreView.ts +30 -0
  54. package/src/render/sui/scoreViewOperations.ts +2555 -2380
  55. package/src/render/sui/svgPageMap.ts +16 -17
  56. package/src/render/sui/textEdit.ts +310 -311
  57. package/src/render/sui/tracker.ts +40 -0
  58. package/src/render/vex/vxNote.ts +3 -1
  59. package/src/smo/xform/copypaste.ts +304 -4
  60. package/src/smo/xform/selections.ts +68 -0
  61. package/src/smo/xform/undo.ts +9 -0
  62. package/src/styles/menus.css +26 -0
  63. package/src/ui/buttons/ribbon.ts +3 -1
  64. package/src/ui/components/buttons/sidebar.vue +15 -6
  65. package/src/ui/components/dialogs/library.vue +3 -1
  66. package/src/ui/components/dialogs/unsavedChanges.vue +44 -0
  67. package/src/ui/dialogs/adapter.ts +181 -183
  68. package/src/ui/dialogs/addMeasure.ts +97 -97
  69. package/src/ui/dialogs/annotationVue.ts +56 -56
  70. package/src/ui/dialogs/audioSettings.ts +121 -123
  71. package/src/ui/dialogs/chordChangeVue.ts +46 -46
  72. package/src/ui/dialogs/clefChange.ts +53 -53
  73. package/src/ui/dialogs/customTuplets.ts +95 -96
  74. package/src/ui/dialogs/customTupletsVue.ts +47 -47
  75. package/src/ui/dialogs/dialog.ts +627 -620
  76. package/src/ui/dialogs/durations.ts +34 -34
  77. package/src/ui/dialogs/dynamics.ts +140 -142
  78. package/src/ui/dialogs/factory.ts +198 -198
  79. package/src/ui/dialogs/fileDialogs.ts +245 -179
  80. package/src/ui/dialogs/fonts.ts +93 -93
  81. package/src/ui/dialogs/globalLayout.ts +40 -40
  82. package/src/ui/dialogs/gracenote.ts +31 -31
  83. package/src/ui/dialogs/hairpin.ts +108 -110
  84. package/src/ui/dialogs/hairpinVue.ts +51 -51
  85. package/src/ui/dialogs/instrument.ts +111 -111
  86. package/src/ui/dialogs/keySignature.ts +175 -176
  87. package/src/ui/dialogs/keySignatureVue.ts +47 -47
  88. package/src/ui/dialogs/library.ts +207 -207
  89. package/src/ui/dialogs/libraryVue.ts +62 -48
  90. package/src/ui/dialogs/lyricVue.ts +46 -46
  91. package/src/ui/dialogs/measureFormat.ts +78 -78
  92. package/src/ui/dialogs/microtones.ts +147 -147
  93. package/src/ui/dialogs/noteHead.ts +319 -319
  94. package/src/ui/dialogs/pageLayout.ts +61 -61
  95. package/src/ui/dialogs/partInfo.ts +60 -60
  96. package/src/ui/dialogs/pedalMarking.ts +117 -117
  97. package/src/ui/dialogs/pedalMarkingVue.ts +73 -73
  98. package/src/ui/dialogs/pitch.ts +352 -352
  99. package/src/ui/dialogs/preferences.ts +40 -40
  100. package/src/ui/dialogs/rehearsalMark.ts +64 -64
  101. package/src/ui/dialogs/rehearsalMarkVue.ts +57 -57
  102. package/src/ui/dialogs/scoreId.ts +35 -36
  103. package/src/ui/dialogs/scoreView.ts +42 -42
  104. package/src/ui/dialogs/slur.ts +308 -310
  105. package/src/ui/dialogs/slurVue.ts +91 -91
  106. package/src/ui/dialogs/staffGroup.ts +112 -112
  107. package/src/ui/dialogs/tabNote.ts +84 -84
  108. package/src/ui/dialogs/tabStave.ts +182 -182
  109. package/src/ui/dialogs/tempo.ts +261 -263
  110. package/src/ui/dialogs/tempoVue.ts +73 -73
  111. package/src/ui/dialogs/textBlockVue.ts +127 -127
  112. package/src/ui/dialogs/textBracket.ts +114 -116
  113. package/src/ui/dialogs/textBracketVue.ts +56 -56
  114. package/src/ui/dialogs/tie.ts +140 -142
  115. package/src/ui/dialogs/timeSignature.ts +65 -66
  116. package/src/ui/dialogs/transposeScore.ts +39 -39
  117. package/src/ui/dialogs/unsavedChanges.ts +43 -0
  118. package/src/ui/dialogs/volta.ts +120 -122
  119. package/src/ui/dialogs/voltaVue.ts +51 -51
  120. package/src/ui/fileio/library.ts +110 -111
  121. package/src/ui/menus/edit.ts +100 -1
  122. package/src/ui/menus/file.ts +19 -3
  123. package/tests/pageDivisions.ts +156 -0
  124. package/tests/promiseHelpersUsage.ts +142 -0
  125. package/tests/scoreChangeState.ts +102 -0
  126. package/tests/selectVoice.ts +189 -0
  127. package/tests/voiceCopyPaste.ts +417 -0
@@ -1,311 +1,310 @@
1
- // [Smoosic](https://github.com/AaronDavidNewman/Smoosic)
2
- // Copyright (c) Aaron David Newman 2021.
3
- import { SuiInlineText, SuiTextBlock } from './textRender';
4
- import { SuiRenderState } from './renderState';
5
- import { SuiScroller } from './scroller';
6
- import { layoutDebug } from './layoutDebug';
7
- import { PromiseHelpers } from '../../common/promiseHelpers';
8
- import { OutlineInfo, SuiTextStrokes, SvgHelpers, SuiTextStrokeName } from './svgHelpers';
9
- import { SmoScoreText, SmoTextGroup } from '../../smo/data/scoreText';
10
- import { SmoLyric } from '../../smo/data/noteModifiers';
11
- import { SmoSelector } from '../../smo/xform/selections';
12
- import { SvgBox, KeyEvent, RemoveElementLike, ElementLike } from '../../smo/data/common';
13
- import { SmoNote } from '../../smo/data/note';
14
- import { SmoScore } from '../../smo/data/score';
15
- import { SmoSelection } from '../../smo/xform/selections';
16
- import { SvgPage } from './svgPageMap';
17
- import { SuiScoreViewOperations } from './scoreViewOperations';
18
- import { SvgPageMap } from './svgPageMap';
19
- import { VexFlow, getChordSymbolGlyphFromCode } from '../../common/vex';
20
-
21
- const VF = VexFlow;
22
- declare var $: any;
23
-
24
- export interface SuiDragSessionParams {
25
- context: SvgPageMap;
26
- scroller: SuiScroller;
27
- textGroup: SmoTextGroup;
28
- debug: layoutDebug;
29
- }
30
- /**
31
- * @category SuiRender
32
- */
33
- export class SuiDragSession {
34
- pageMap: SvgPageMap;
35
- page: SvgPage;
36
- scroller: SuiScroller;
37
- outlineBox: SvgBox;
38
- textObject: SuiTextBlock;
39
- dragging: boolean = false;
40
- outlineRect: OutlineInfo | null = null;
41
- textGroup: SmoTextGroup;
42
- debug: layoutDebug;
43
- // Drag control settings (023-text-drag-controls), toggled live by textDragger.vue's checkboxes.
44
- lockHorizontal: boolean = false;
45
- lockVertical: boolean = false;
46
- snapEnabled: boolean = false;
47
- // Snapshot of the drag's starting box, used to pin a locked axis to a single value
48
- // for the life of the drag rather than re-deriving it from the mutable outlineBox.
49
- dragOriginBox: SvgBox = SvgBox.default;
50
- constructor(params: SuiDragSessionParams) {
51
- this.textGroup = params.textGroup;
52
- this.pageMap = params.context;
53
- this.scroller = params.scroller;
54
- this.debug = params.debug;
55
- this.page = this.pageMap.getRendererFromModifier(this.textGroup);
56
- // create a temporary text object for dragging
57
- this.textObject = SuiTextBlock.fromTextGroup(this.textGroup, this.page, this.pageMap, this.scroller); // SuiTextBlock
58
- this.dragging = false;
59
- this.outlineBox = this.textObject.getLogicalBox();
60
- }
61
-
62
- _outlineBox() {
63
- const outlineStroke = SuiTextStrokes['text-drag'];
64
- const x = this.outlineBox.x - this.page.box.x;
65
- const y = this.outlineBox.y - this.page.box.y;
66
- if (!this.outlineRect) {
67
- this.outlineRect = {
68
- context: this.page,
69
- box: SvgHelpers.boxPoints(x , y + this.outlineBox.height, this.outlineBox.width, this.outlineBox.height),
70
- classes: 'text-drag',
71
- stroke: outlineStroke, scroll: this.scroller.scrollState, timeOff: 1000
72
- };
73
- }
74
- this.outlineRect.box = SvgHelpers.boxPoints(x , y + this.outlineBox.height, this.outlineBox.width, this.outlineBox.height),
75
- SvgHelpers.outlineRect(this.outlineRect);
76
- }
77
- unrender() {
78
- this.textGroup.elements.forEach((el: ElementLike) => {
79
- RemoveElementLike(el);
80
- });
81
- this.textGroup.elements = [];
82
- this.textObject.unrender();
83
- }
84
- scrolledClientBox(x: number, y: number) {
85
- return { x: x + this.scroller.scrollState.x, y: y + this.scroller.scrollState.y, width: 1, height: 1 };
86
- }
87
- checkBounds() {
88
- if (this.outlineBox.y < this.outlineBox.height) {
89
- this.outlineBox.y = this.outlineBox.height;
90
- }
91
- if (this.outlineBox.x < 0) {
92
- this.outlineBox.x = 0;
93
- }
94
- if (this.outlineBox.x > this.page.box.x + this.page.box.width - this.outlineBox.width) {
95
- this.outlineBox.x = this.page.box.x + this.page.box.width - this.outlineBox.width;
96
- }
97
- if (this.outlineBox.y > this.page.box.y + this.page.box.height) {
98
- this.outlineBox.y = this.page.box.y + this.page.box.height;
99
- }
100
- }
101
- startDrag(e: any) {
102
- const evBox = this.scrolledClientBox(e.clientX, e.clientY);
103
- const svgMouseBox = this.pageMap.clientToSvg(evBox);
104
- svgMouseBox.y -= this.outlineBox.height;
105
- if (this.debug.mask & layoutDebug.values['dragDebug']) {
106
- this.debug.updateDragDebug(svgMouseBox, this.outlineBox, 'start');
107
- }
108
- if (!SvgHelpers.doesBox1ContainBox2(this.outlineBox, svgMouseBox)) {
109
- return;
110
- }
111
- this.dragging = true;
112
- this.outlineBox = svgMouseBox;
113
- this.dragOriginBox = { ...svgMouseBox };
114
- const currentBox = this.textObject.getLogicalBox();
115
- this.outlineBox.width = currentBox.width;
116
- this.outlineBox.height = currentBox.height;
117
- this.unrender();
118
- this.checkBounds();
119
- this._outlineBox();
120
- }
121
-
122
- mouseMove(e: any) {
123
- if (!this.dragging) {
124
- return;
125
- }
126
- const evBox = this.scrolledClientBox(e.clientX, e.clientY);
127
- const svgMouseBox = this.pageMap.clientToSvg(evBox);
128
- svgMouseBox.y -= this.outlineBox.height;
129
- if (this.snapEnabled) {
130
- // Snap to a grid that looks like 10 real screen pixels regardless of zoom: divide only
131
- // by svgScale (renderScale), not zoomScale, so the grid stays fixed in model space
132
- // instead of changing spacing as the user zooms (specs/023-text-drag-controls/research.md §2).
133
- const gridStep = 10 / this.pageMap.renderScale;
134
- svgMouseBox.x = Math.round(svgMouseBox.x / gridStep) * gridStep;
135
- svgMouseBox.y = Math.round(svgMouseBox.y / gridStep) * gridStep;
136
- }
137
- if (this.lockHorizontal) {
138
- svgMouseBox.x = this.dragOriginBox.x;
139
- }
140
- if (this.lockVertical) {
141
- svgMouseBox.y = this.dragOriginBox.y;
142
- }
143
- this.outlineBox = SvgHelpers.smoBox(svgMouseBox);
144
- const currentBox = this.textObject.getLogicalBox();
145
- this.outlineBox.width = currentBox.width;
146
- this.outlineBox.height = currentBox.height;
147
- this.checkBounds();
148
-
149
- this.textObject.offsetStartX(this.outlineBox.x - currentBox.x);
150
- this.textObject.offsetStartY(this.outlineBox.y - currentBox.y);
151
- this.textObject.render();
152
- if (this.debug.mask & layoutDebug.values['dragDebug']) {
153
- this.debug.updateDragDebug(svgMouseBox, this.outlineBox, 'drag');
154
- }
155
- if (this.outlineRect) {
156
- SvgHelpers.eraseOutline(this.outlineRect);
157
- this.outlineRect = null;
158
- }
159
- this._outlineBox();
160
- }
161
-
162
- endDrag() {
163
- // this.textObject.render();
164
- const newBox = this.textObject.getLogicalBox();
165
- const curBox = this.textGroup.logicalBox ?? SvgBox.default;
166
- if (this.debug.mask & layoutDebug.values['dragDebug']) {
167
- this.debug.updateDragDebug(curBox, newBox, 'end');
168
- }
169
- this.textGroup.offsetX(newBox.x - curBox.x);
170
- this.textGroup.offsetY(newBox.y - curBox.y + this.outlineBox.height);
171
- this.dragging = false;
172
- if (this.outlineRect) {
173
- SvgHelpers.eraseOutline(this.outlineRect);
174
- this.outlineRect = null;
175
- }
176
- }
177
- }
178
-
179
- export interface SuiAnnotationDragSessionParams {
180
- context: SvgPageMap;
181
- scroller: SuiScroller;
182
- annotation: SmoLyric;
183
- debug: layoutDebug;
184
- }
185
- /**
186
- * Drags a single note annotation (`SmoLyric` with `parser === annotation`) directly on the
187
- * score. Unlike `SuiDragSession` (which drags a `SmoTextGroup` and must build a temporary
188
- * `SuiTextBlock` clone to have something to render), an annotation is always exactly one
189
- * already-rendered VexFlow glyph, so the live preview here just re-points that same DOM
190
- * element's `transform` attribute -- the same attribute/convention `VxSystem._updateAnnotationOffsets`
191
- * already writes on every full render pass -- with no clone and no re-render mid-drag.
192
- * @category SuiRender
193
- */
194
- export class SuiAnnotationDragSession {
195
- pageMap: SvgPageMap;
196
- page: SvgPage;
197
- scroller: SuiScroller;
198
- annotation: SmoLyric;
199
- debug: layoutDebug;
200
- dragging: boolean = false;
201
- originBox: SvgBox;
202
- startTranslateX: number = 0;
203
- startTranslateY: number = 0;
204
- startMouse: { x: number, y: number } = { x: 0, y: 0 };
205
- currentDx: number = 0;
206
- currentDy: number = 0;
207
- outlineRect: OutlineInfo | null = null;
208
- constructor(params: SuiAnnotationDragSessionParams) {
209
- this.annotation = params.annotation;
210
- this.pageMap = params.context;
211
- this.scroller = params.scroller;
212
- this.debug = params.debug;
213
- this.page = this.pageMap.getRendererFromModifier(this.annotation);
214
- this.originBox = SvgHelpers.smoBox(this.annotation.logicalBox ?? SvgBox.default);
215
- }
216
- scrolledClientBox(x: number, y: number) {
217
- return { x: x + this.scroller.scrollState.x, y: y + this.scroller.scrollState.y, width: 1, height: 1 };
218
- }
219
- _outlineBox(box: SvgBox) {
220
- const outlineStroke = SuiTextStrokes['text-drag'];
221
- const x = box.x - this.page.box.x;
222
- const y = box.y - this.page.box.y;
223
- if (!this.outlineRect) {
224
- this.outlineRect = {
225
- context: this.page,
226
- box: SvgHelpers.boxPoints(x, y, box.width, box.height),
227
- classes: 'text-drag',
228
- stroke: outlineStroke, scroll: this.scroller.scrollState, timeOff: 1000
229
- };
230
- }
231
- this.outlineRect.box = SvgHelpers.boxPoints(x, y, box.width, box.height);
232
- SvgHelpers.outlineRect(this.outlineRect);
233
- }
234
- _eraseOutline() {
235
- if (this.outlineRect) {
236
- SvgHelpers.eraseOutline(this.outlineRect);
237
- this.outlineRect = null;
238
- }
239
- }
240
- // Clamp a candidate (dx, dy) so the annotation's origin box, shifted by it, stays within the page.
241
- _clampedDelta(dx: number, dy: number): { dx: number, dy: number } {
242
- const pageBox = this.page.box;
243
- let x = this.originBox.x + dx;
244
- let y = this.originBox.y + dy;
245
- x = Math.max(pageBox.x, Math.min(x, pageBox.x + pageBox.width - this.originBox.width));
246
- y = Math.max(pageBox.y, Math.min(y, pageBox.y + pageBox.height - this.originBox.height));
247
- return { dx: x - this.originBox.x, dy: y - this.originBox.y };
248
- }
249
- startDrag(e: any) {
250
- const evBox = this.scrolledClientBox(e.clientX, e.clientY);
251
- const svgMouseBox = this.pageMap.clientToSvg(evBox);
252
- if (this.debug.mask & layoutDebug.values['dragDebug']) {
253
- this.debug.updateDragDebug(svgMouseBox, this.originBox, 'start');
254
- }
255
- if (!SvgHelpers.doesBox1ContainBox2(this.originBox, svgMouseBox)) {
256
- return;
257
- }
258
- this.dragging = true;
259
- this.startMouse = { x: svgMouseBox.x, y: svgMouseBox.y };
260
- this.startTranslateX = this.annotation.translateX;
261
- this.startTranslateY = this.annotation.translateY;
262
- this.currentDx = 0;
263
- this.currentDy = 0;
264
- this._outlineBox(this.originBox);
265
- }
266
- mouseMove(e: any) {
267
- if (!this.dragging) {
268
- return;
269
- }
270
- const evBox = this.scrolledClientBox(e.clientX, e.clientY);
271
- const svgMouseBox = this.pageMap.clientToSvg(evBox);
272
- const rawDx = svgMouseBox.x - this.startMouse.x;
273
- const rawDy = svgMouseBox.y - this.startMouse.y;
274
- const { dx, dy } = this._clampedDelta(rawDx, rawDy);
275
- this.currentDx = dx;
276
- this.currentDy = dy;
277
- const dom = this.page.svg.getElementById('vf-' + this.annotation.attrs.id);
278
- if (dom) {
279
- const liveX = this.startTranslateX + dx;
280
- const liveY = this.startTranslateY - dy;
281
- dom.setAttributeNS('', 'transform', 'translate(' + liveX + ' ' + (-1 * liveY) + ')');
282
- }
283
- const newBox = SvgHelpers.boxPoints(this.originBox.x + dx, this.originBox.y + dy, this.originBox.width, this.originBox.height);
284
- this._eraseOutline();
285
- this._outlineBox(newBox);
286
- if (this.debug.mask & layoutDebug.values['dragDebug']) {
287
- this.debug.updateDragDebug(svgMouseBox, newBox, 'drag');
288
- }
289
- }
290
- endDrag() {
291
- if (!this.dragging) {
292
- return;
293
- }
294
- this.annotation.translateX = this.startTranslateX + this.currentDx;
295
- this.annotation.translateY = this.startTranslateY - this.currentDy;
296
- this.dragging = false;
297
- if (this.debug.mask & layoutDebug.values['dragDebug']) {
298
- const newBox = SvgHelpers.boxPoints(this.originBox.x + this.currentDx, this.originBox.y + this.currentDy,
299
- this.originBox.width, this.originBox.height);
300
- this.debug.updateDragDebug(this.originBox, newBox, 'end');
301
- }
302
- this._eraseOutline();
303
- }
304
- unrender() {
305
- if (this.dragging) {
306
- this.endDrag();
307
- }
308
- this._eraseOutline();
309
- }
310
- }
311
-
1
+ // [Smoosic](https://github.com/AaronDavidNewman/Smoosic)
2
+ // Copyright (c) Aaron David Newman 2021.
3
+ import { SuiInlineText, SuiTextBlock } from './textRender';
4
+ import { SuiRenderState } from './renderState';
5
+ import { SuiScroller } from './scroller';
6
+ import { layoutDebug } from './layoutDebug';
7
+ import { OutlineInfo, SuiTextStrokes, SvgHelpers, SuiTextStrokeName } from './svgHelpers';
8
+ import { SmoScoreText, SmoTextGroup } from '../../smo/data/scoreText';
9
+ import { SmoLyric } from '../../smo/data/noteModifiers';
10
+ import { SmoSelector } from '../../smo/xform/selections';
11
+ import { SvgBox, KeyEvent, RemoveElementLike, ElementLike } from '../../smo/data/common';
12
+ import { SmoNote } from '../../smo/data/note';
13
+ import { SmoScore } from '../../smo/data/score';
14
+ import { SmoSelection } from '../../smo/xform/selections';
15
+ import { SvgPage } from './svgPageMap';
16
+ import { SuiScoreViewOperations } from './scoreViewOperations';
17
+ import { SvgPageMap } from './svgPageMap';
18
+ import { VexFlow, getChordSymbolGlyphFromCode } from '../../common/vex';
19
+
20
+ const VF = VexFlow;
21
+ declare var $: any;
22
+
23
+ export interface SuiDragSessionParams {
24
+ context: SvgPageMap;
25
+ scroller: SuiScroller;
26
+ textGroup: SmoTextGroup;
27
+ debug: layoutDebug;
28
+ }
29
+ /**
30
+ * @category SuiRender
31
+ */
32
+ export class SuiDragSession {
33
+ pageMap: SvgPageMap;
34
+ page: SvgPage;
35
+ scroller: SuiScroller;
36
+ outlineBox: SvgBox;
37
+ textObject: SuiTextBlock;
38
+ dragging: boolean = false;
39
+ outlineRect: OutlineInfo | null = null;
40
+ textGroup: SmoTextGroup;
41
+ debug: layoutDebug;
42
+ // Drag control settings (023-text-drag-controls), toggled live by textDragger.vue's checkboxes.
43
+ lockHorizontal: boolean = false;
44
+ lockVertical: boolean = false;
45
+ snapEnabled: boolean = false;
46
+ // Snapshot of the drag's starting box, used to pin a locked axis to a single value
47
+ // for the life of the drag rather than re-deriving it from the mutable outlineBox.
48
+ dragOriginBox: SvgBox = SvgBox.default;
49
+ constructor(params: SuiDragSessionParams) {
50
+ this.textGroup = params.textGroup;
51
+ this.pageMap = params.context;
52
+ this.scroller = params.scroller;
53
+ this.debug = params.debug;
54
+ this.page = this.pageMap.getRendererFromModifier(this.textGroup);
55
+ // create a temporary text object for dragging
56
+ this.textObject = SuiTextBlock.fromTextGroup(this.textGroup, this.page, this.pageMap, this.scroller); // SuiTextBlock
57
+ this.dragging = false;
58
+ this.outlineBox = this.textObject.getLogicalBox();
59
+ }
60
+
61
+ _outlineBox() {
62
+ const outlineStroke = SuiTextStrokes['text-drag'];
63
+ const x = this.outlineBox.x - this.page.box.x;
64
+ const y = this.outlineBox.y - this.page.box.y;
65
+ if (!this.outlineRect) {
66
+ this.outlineRect = {
67
+ context: this.page,
68
+ box: SvgHelpers.boxPoints(x , y + this.outlineBox.height, this.outlineBox.width, this.outlineBox.height),
69
+ classes: 'text-drag',
70
+ stroke: outlineStroke, scroll: this.scroller.scrollState, timeOff: 1000
71
+ };
72
+ }
73
+ this.outlineRect.box = SvgHelpers.boxPoints(x , y + this.outlineBox.height, this.outlineBox.width, this.outlineBox.height),
74
+ SvgHelpers.outlineRect(this.outlineRect);
75
+ }
76
+ unrender() {
77
+ this.textGroup.elements.forEach((el: ElementLike) => {
78
+ RemoveElementLike(el);
79
+ });
80
+ this.textGroup.elements = [];
81
+ this.textObject.unrender();
82
+ }
83
+ scrolledClientBox(x: number, y: number) {
84
+ return { x: x + this.scroller.scrollState.x, y: y + this.scroller.scrollState.y, width: 1, height: 1 };
85
+ }
86
+ checkBounds() {
87
+ if (this.outlineBox.y < this.outlineBox.height) {
88
+ this.outlineBox.y = this.outlineBox.height;
89
+ }
90
+ if (this.outlineBox.x < 0) {
91
+ this.outlineBox.x = 0;
92
+ }
93
+ if (this.outlineBox.x > this.page.box.x + this.page.box.width - this.outlineBox.width) {
94
+ this.outlineBox.x = this.page.box.x + this.page.box.width - this.outlineBox.width;
95
+ }
96
+ if (this.outlineBox.y > this.page.box.y + this.page.box.height) {
97
+ this.outlineBox.y = this.page.box.y + this.page.box.height;
98
+ }
99
+ }
100
+ startDrag(e: any) {
101
+ const evBox = this.scrolledClientBox(e.clientX, e.clientY);
102
+ const svgMouseBox = this.pageMap.clientToSvg(evBox);
103
+ svgMouseBox.y -= this.outlineBox.height;
104
+ if (this.debug.mask & layoutDebug.values['dragDebug']) {
105
+ this.debug.updateDragDebug(svgMouseBox, this.outlineBox, 'start');
106
+ }
107
+ if (!SvgHelpers.doesBox1ContainBox2(this.outlineBox, svgMouseBox)) {
108
+ return;
109
+ }
110
+ this.dragging = true;
111
+ this.outlineBox = svgMouseBox;
112
+ this.dragOriginBox = { ...svgMouseBox };
113
+ const currentBox = this.textObject.getLogicalBox();
114
+ this.outlineBox.width = currentBox.width;
115
+ this.outlineBox.height = currentBox.height;
116
+ this.unrender();
117
+ this.checkBounds();
118
+ this._outlineBox();
119
+ }
120
+
121
+ mouseMove(e: any) {
122
+ if (!this.dragging) {
123
+ return;
124
+ }
125
+ const evBox = this.scrolledClientBox(e.clientX, e.clientY);
126
+ const svgMouseBox = this.pageMap.clientToSvg(evBox);
127
+ svgMouseBox.y -= this.outlineBox.height;
128
+ if (this.snapEnabled) {
129
+ // Snap to a grid that looks like 10 real screen pixels regardless of zoom: divide only
130
+ // by svgScale (renderScale), not zoomScale, so the grid stays fixed in model space
131
+ // instead of changing spacing as the user zooms (specs/023-text-drag-controls/research.md §2).
132
+ const gridStep = 10 / this.pageMap.renderScale;
133
+ svgMouseBox.x = Math.round(svgMouseBox.x / gridStep) * gridStep;
134
+ svgMouseBox.y = Math.round(svgMouseBox.y / gridStep) * gridStep;
135
+ }
136
+ if (this.lockHorizontal) {
137
+ svgMouseBox.x = this.dragOriginBox.x;
138
+ }
139
+ if (this.lockVertical) {
140
+ svgMouseBox.y = this.dragOriginBox.y;
141
+ }
142
+ this.outlineBox = SvgHelpers.smoBox(svgMouseBox);
143
+ const currentBox = this.textObject.getLogicalBox();
144
+ this.outlineBox.width = currentBox.width;
145
+ this.outlineBox.height = currentBox.height;
146
+ this.checkBounds();
147
+
148
+ this.textObject.offsetStartX(this.outlineBox.x - currentBox.x);
149
+ this.textObject.offsetStartY(this.outlineBox.y - currentBox.y);
150
+ this.textObject.render();
151
+ if (this.debug.mask & layoutDebug.values['dragDebug']) {
152
+ this.debug.updateDragDebug(svgMouseBox, this.outlineBox, 'drag');
153
+ }
154
+ if (this.outlineRect) {
155
+ SvgHelpers.eraseOutline(this.outlineRect);
156
+ this.outlineRect = null;
157
+ }
158
+ this._outlineBox();
159
+ }
160
+
161
+ endDrag() {
162
+ // this.textObject.render();
163
+ const newBox = this.textObject.getLogicalBox();
164
+ const curBox = this.textGroup.logicalBox ?? SvgBox.default;
165
+ if (this.debug.mask & layoutDebug.values['dragDebug']) {
166
+ this.debug.updateDragDebug(curBox, newBox, 'end');
167
+ }
168
+ this.textGroup.offsetX(newBox.x - curBox.x);
169
+ this.textGroup.offsetY(newBox.y - curBox.y + this.outlineBox.height);
170
+ this.dragging = false;
171
+ if (this.outlineRect) {
172
+ SvgHelpers.eraseOutline(this.outlineRect);
173
+ this.outlineRect = null;
174
+ }
175
+ }
176
+ }
177
+
178
+ export interface SuiAnnotationDragSessionParams {
179
+ context: SvgPageMap;
180
+ scroller: SuiScroller;
181
+ annotation: SmoLyric;
182
+ debug: layoutDebug;
183
+ }
184
+ /**
185
+ * Drags a single note annotation (`SmoLyric` with `parser === annotation`) directly on the
186
+ * score. Unlike `SuiDragSession` (which drags a `SmoTextGroup` and must build a temporary
187
+ * `SuiTextBlock` clone to have something to render), an annotation is always exactly one
188
+ * already-rendered VexFlow glyph, so the live preview here just re-points that same DOM
189
+ * element's `transform` attribute -- the same attribute/convention `VxSystem._updateAnnotationOffsets`
190
+ * already writes on every full render pass -- with no clone and no re-render mid-drag.
191
+ * @category SuiRender
192
+ */
193
+ export class SuiAnnotationDragSession {
194
+ pageMap: SvgPageMap;
195
+ page: SvgPage;
196
+ scroller: SuiScroller;
197
+ annotation: SmoLyric;
198
+ debug: layoutDebug;
199
+ dragging: boolean = false;
200
+ originBox: SvgBox;
201
+ startTranslateX: number = 0;
202
+ startTranslateY: number = 0;
203
+ startMouse: { x: number, y: number } = { x: 0, y: 0 };
204
+ currentDx: number = 0;
205
+ currentDy: number = 0;
206
+ outlineRect: OutlineInfo | null = null;
207
+ constructor(params: SuiAnnotationDragSessionParams) {
208
+ this.annotation = params.annotation;
209
+ this.pageMap = params.context;
210
+ this.scroller = params.scroller;
211
+ this.debug = params.debug;
212
+ this.page = this.pageMap.getRendererFromModifier(this.annotation);
213
+ this.originBox = SvgHelpers.smoBox(this.annotation.logicalBox ?? SvgBox.default);
214
+ }
215
+ scrolledClientBox(x: number, y: number) {
216
+ return { x: x + this.scroller.scrollState.x, y: y + this.scroller.scrollState.y, width: 1, height: 1 };
217
+ }
218
+ _outlineBox(box: SvgBox) {
219
+ const outlineStroke = SuiTextStrokes['text-drag'];
220
+ const x = box.x - this.page.box.x;
221
+ const y = box.y - this.page.box.y;
222
+ if (!this.outlineRect) {
223
+ this.outlineRect = {
224
+ context: this.page,
225
+ box: SvgHelpers.boxPoints(x, y, box.width, box.height),
226
+ classes: 'text-drag',
227
+ stroke: outlineStroke, scroll: this.scroller.scrollState, timeOff: 1000
228
+ };
229
+ }
230
+ this.outlineRect.box = SvgHelpers.boxPoints(x, y, box.width, box.height);
231
+ SvgHelpers.outlineRect(this.outlineRect);
232
+ }
233
+ _eraseOutline() {
234
+ if (this.outlineRect) {
235
+ SvgHelpers.eraseOutline(this.outlineRect);
236
+ this.outlineRect = null;
237
+ }
238
+ }
239
+ // Clamp a candidate (dx, dy) so the annotation's origin box, shifted by it, stays within the page.
240
+ _clampedDelta(dx: number, dy: number): { dx: number, dy: number } {
241
+ const pageBox = this.page.box;
242
+ let x = this.originBox.x + dx;
243
+ let y = this.originBox.y + dy;
244
+ x = Math.max(pageBox.x, Math.min(x, pageBox.x + pageBox.width - this.originBox.width));
245
+ y = Math.max(pageBox.y, Math.min(y, pageBox.y + pageBox.height - this.originBox.height));
246
+ return { dx: x - this.originBox.x, dy: y - this.originBox.y };
247
+ }
248
+ startDrag(e: any) {
249
+ const evBox = this.scrolledClientBox(e.clientX, e.clientY);
250
+ const svgMouseBox = this.pageMap.clientToSvg(evBox);
251
+ if (this.debug.mask & layoutDebug.values['dragDebug']) {
252
+ this.debug.updateDragDebug(svgMouseBox, this.originBox, 'start');
253
+ }
254
+ if (!SvgHelpers.doesBox1ContainBox2(this.originBox, svgMouseBox)) {
255
+ return;
256
+ }
257
+ this.dragging = true;
258
+ this.startMouse = { x: svgMouseBox.x, y: svgMouseBox.y };
259
+ this.startTranslateX = this.annotation.translateX;
260
+ this.startTranslateY = this.annotation.translateY;
261
+ this.currentDx = 0;
262
+ this.currentDy = 0;
263
+ this._outlineBox(this.originBox);
264
+ }
265
+ mouseMove(e: any) {
266
+ if (!this.dragging) {
267
+ return;
268
+ }
269
+ const evBox = this.scrolledClientBox(e.clientX, e.clientY);
270
+ const svgMouseBox = this.pageMap.clientToSvg(evBox);
271
+ const rawDx = svgMouseBox.x - this.startMouse.x;
272
+ const rawDy = svgMouseBox.y - this.startMouse.y;
273
+ const { dx, dy } = this._clampedDelta(rawDx, rawDy);
274
+ this.currentDx = dx;
275
+ this.currentDy = dy;
276
+ const dom = this.page.svg.getElementById('vf-' + this.annotation.attrs.id);
277
+ if (dom) {
278
+ const liveX = this.startTranslateX + dx;
279
+ const liveY = this.startTranslateY - dy;
280
+ dom.setAttributeNS('', 'transform', 'translate(' + liveX + ' ' + (-1 * liveY) + ')');
281
+ }
282
+ const newBox = SvgHelpers.boxPoints(this.originBox.x + dx, this.originBox.y + dy, this.originBox.width, this.originBox.height);
283
+ this._eraseOutline();
284
+ this._outlineBox(newBox);
285
+ if (this.debug.mask & layoutDebug.values['dragDebug']) {
286
+ this.debug.updateDragDebug(svgMouseBox, newBox, 'drag');
287
+ }
288
+ }
289
+ endDrag() {
290
+ if (!this.dragging) {
291
+ return;
292
+ }
293
+ this.annotation.translateX = this.startTranslateX + this.currentDx;
294
+ this.annotation.translateY = this.startTranslateY - this.currentDy;
295
+ this.dragging = false;
296
+ if (this.debug.mask & layoutDebug.values['dragDebug']) {
297
+ const newBox = SvgHelpers.boxPoints(this.originBox.x + this.currentDx, this.originBox.y + this.currentDy,
298
+ this.originBox.width, this.originBox.height);
299
+ this.debug.updateDragDebug(this.originBox, newBox, 'end');
300
+ }
301
+ this._eraseOutline();
302
+ }
303
+ unrender() {
304
+ if (this.dragging) {
305
+ this.endDrag();
306
+ }
307
+ this._eraseOutline();
308
+ }
309
+ }
310
+