@websline/cms-view-utils 0.25.0 → 1.0.0-alpha.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,222 +0,0 @@
1
- // Handler for mouse follower behavior inside the iframe
2
- class IframeDragHandler {
3
- constructor() {
4
- this.mouseFollowerWidth = 48;
5
- this.mouseFollowerHeight = 48;
6
-
7
- this.handleMouseMove = (event) => {
8
- this.lastMouseEvent = event;
9
- this.updateMouseFollowerPosition(event);
10
- this.checkMouseLeaveFrame();
11
- };
12
-
13
- this.handleScroll = () => {
14
- if (!this.lastMouseEvent) return;
15
- this.updateMouseFollowerPosition(this.lastMouseEvent);
16
- };
17
-
18
- window.addEventListener("message", (event) => {
19
- const { owner, type } = event.data;
20
- if (owner !== "dragging") return;
21
-
22
- if (type === "startDragging") {
23
- this.enableDragging();
24
- }
25
-
26
- if (type === "stopDragging") {
27
- this.disableDragging();
28
- }
29
-
30
- if (type === "enterIframeWhileDragging") {
31
- this.showMouseFollower();
32
- }
33
-
34
- if (type === "leaveIframeWhileDragging") {
35
- this.hideMouseFollower();
36
- }
37
- });
38
- }
39
-
40
- enableDragging() {
41
- this.addStopDraggingHandler();
42
- this.addMouseFollowerToDOM();
43
- this.addMouseFollowerHandler();
44
- this.highlightDropzone();
45
- this.addDropListeners();
46
- this.addDraggingClass();
47
- window.addEventListener("scroll", this.handleScroll, true);
48
- }
49
-
50
- disableDragging() {
51
- this.removeStopDraggingHandler();
52
- this.removeMouseFollowerFromDOM();
53
- this.removeMouseFollowerHandler();
54
- this.unhighlightDropzone();
55
- this.removeDropListeners();
56
- this.removeDraggingClass();
57
- window.removeEventListener("scroll", this.handleScroll, true);
58
- this.lastMouseEvent = null;
59
- }
60
-
61
- addStopDraggingHandler() {
62
- window.addEventListener("mouseup", this.stopDragging);
63
- }
64
-
65
- removeStopDraggingHandler() {
66
- window.removeEventListener("mouseup", this.stopDragging);
67
- }
68
-
69
- stopDragging = () => {
70
- this.sendParentMessage({
71
- owner: "iframe",
72
- type: "stopDragging",
73
- });
74
- this.removeStopDraggingHandler();
75
- };
76
-
77
- addMouseFollowerToDOM() {
78
- if (this.mouseFollowerInDOM()) return;
79
-
80
- const div = document.createElement("div");
81
- div.className = "mouse-follower";
82
-
83
- Object.assign(div.style, {
84
- position: "absolute",
85
- zIndex: "9999",
86
- pointerEvents: "none",
87
- left: `-${this.mouseFollowerWidth}px`,
88
- top: `-${this.mouseFollowerHeight}px`,
89
- width: this.mouseFollowerWidth + "px",
90
- height: this.mouseFollowerHeight + "px",
91
- background: "url(/drag-component.svg) no-repeat center center",
92
- });
93
-
94
- document.body.appendChild(div);
95
- }
96
-
97
- addMouseFollowerHandler() {
98
- window.addEventListener("mousemove", this.handleMouseMove);
99
- }
100
-
101
- removeMouseFollowerHandler() {
102
- window.removeEventListener("mousemove", this.handleMouseMove);
103
- }
104
-
105
- updateMouseFollowerPosition(event) {
106
- const div = this.getMouseFollower();
107
- if (div) {
108
- div.style.left = `${event.clientX + window.scrollX - this.mouseFollowerWidth / 2}px`;
109
- div.style.top = `${event.clientY + window.scrollY - this.mouseFollowerHeight / 2}px`;
110
- }
111
- }
112
-
113
- checkMouseLeaveFrame() {
114
- const follower = this.getMouseFollower();
115
- if (!follower) return;
116
-
117
- const rect = follower.getBoundingClientRect();
118
- const margin = 1;
119
-
120
- const outOfBounds =
121
- rect.left < -margin ||
122
- rect.top < -margin ||
123
- rect.right > window.innerWidth + margin ||
124
- rect.bottom > window.innerHeight + margin;
125
-
126
- if (outOfBounds && !follower.dataset._wasOutside) {
127
- follower.dataset._wasOutside = "true";
128
- this.hideMouseFollower();
129
- this.sendParentMessage({ owner: "iframe", type: "leaveIframe" });
130
- } else if (!outOfBounds && follower.dataset._wasOutside === "true") {
131
- delete follower.dataset._wasOutside;
132
- }
133
- }
134
-
135
- hideMouseFollower() {
136
- const div = this.getMouseFollower();
137
- if (div) {
138
- div.style.display = "none";
139
- }
140
- }
141
-
142
- showMouseFollower() {
143
- const div = this.getMouseFollower();
144
- if (div) {
145
- div.style.display = "block";
146
- }
147
- }
148
-
149
- removeMouseFollowerFromDOM() {
150
- const div = this.getMouseFollower();
151
- if (div) {
152
- div.remove();
153
- }
154
- }
155
-
156
- mouseFollowerInDOM() {
157
- return !!this.getMouseFollower();
158
- }
159
-
160
- getMouseFollower() {
161
- return document.querySelector(".mouse-follower");
162
- }
163
-
164
- sendParentMessage(message) {
165
- window.parent.postMessage(message, "*");
166
- }
167
-
168
- addDropListeners() {
169
- const dropzones = document.querySelectorAll("[data-drop-zone]");
170
-
171
- dropzones.forEach((dropzone) => {
172
- dropzone.addEventListener("mouseup", this.commitDrop);
173
- });
174
- }
175
-
176
- removeDropListeners() {
177
- const dropzones = document.querySelectorAll("[data-drop-zone]");
178
-
179
- dropzones.forEach((dropzone) => {
180
- dropzone.removeEventListener("mouseup", this.commitDrop);
181
- });
182
- }
183
-
184
- commitDrop = (event) => {
185
- event.preventDefault();
186
- const dropPosition = parseInt(
187
- event.target.getAttribute("data-drop-zone-index") || "0"
188
- );
189
-
190
- this.sendParentMessage({
191
- owner: "iframe",
192
- position: dropPosition,
193
- type: "commitDrop",
194
- });
195
- };
196
-
197
- highlightDropzone() {
198
- const dropzones = document.querySelectorAll("[data-drop-zone]");
199
-
200
- dropzones.forEach((dropzone) => {
201
- dropzone.classList.add("highlight");
202
- });
203
- }
204
-
205
- unhighlightDropzone() {
206
- const dropzones = document.querySelectorAll("[data-drop-zone]");
207
-
208
- dropzones.forEach((dropzone) => {
209
- dropzone.classList.remove("highlight");
210
- });
211
- }
212
-
213
- addDraggingClass() {
214
- document.body.classList.add("cms-editor-is-dragging");
215
- }
216
-
217
- removeDraggingClass() {
218
- document.body.classList.remove("cms-editor-is-dragging");
219
- }
220
- }
221
-
222
- export default IframeDragHandler;
@@ -1,2 +0,0 @@
1
- export * from "./iframeDragHandler";
2
- export * from "./dragDropUtils";