@websline/cms-view-utils 0.1.0 → 0.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websline/cms-view-utils",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.js"
@@ -11,7 +11,7 @@
11
11
  "sideEffects": false,
12
12
  "peerDependencies": {
13
13
  "astro": "^5.8.0",
14
- "svelte": "^5.33.0"
14
+ "svelte": "^5.33.1"
15
15
  },
16
16
  "scripts": {}
17
17
  }
@@ -5,24 +5,19 @@ const { lang, slug } = Astro.params;
5
5
  const url = new URL(Astro.request.url);
6
6
  const searchParams = url.searchParams;
7
7
 
8
- // const isPreviewMode = searchParams.get("editable") === "true";
8
+ const isPreviewMode = searchParams.get("editable") === "true";
9
9
 
10
10
  await fetchPageData(Astro.locals, lang, slug);
11
11
  ---
12
12
 
13
13
  <slot />
14
14
 
15
- <!--
16
- {isPreviewMode && (
17
-
18
- <style>
19
- .cms-block-dropzones {
20
- padding: 2rem;
21
- border: 2px dashed #999;
22
- min-height: 50px;
23
- }
24
- .cms-block-dropzones.highlight {
25
- border-color: rgb(106, 106, 255);
26
- }
27
- </style>
28
- )} -->
15
+ {
16
+ isPreviewMode && (
17
+ <script type="module">
18
+ import IframeDragHandler from
19
+ "https://unpkg.com/@websline/cms-view-utils@latest/src/utils/iframeDragHandler.js";
20
+ new IframeDragHandler();
21
+ </script>
22
+ )
23
+ }
@@ -5,7 +5,6 @@ export async function fetchPageData(locals, lang, slug) {
5
5
  const key = `__pageData__${lang}__${slug}`;
6
6
 
7
7
  if (!locals[key]) {
8
- console.log("from fetch");
9
8
  const url = `${API_BASE}/${lang}/pages/${slug}`;
10
9
  const res = await fetch(url);
11
10
 
@@ -31,7 +30,5 @@ export function getPageData(locals) {
31
30
  );
32
31
  }
33
32
 
34
- console.log("from cache");
35
-
36
33
  return locals[key];
37
34
  }
@@ -5,10 +5,16 @@ class IframeDragHandler {
5
5
  this.mouseFollowerHeight = 48;
6
6
 
7
7
  this.handleMouseMove = (event) => {
8
+ this.lastMouseEvent = event;
8
9
  this.updateMouseFollowerPosition(event);
9
10
  this.checkMouseLeaveFrame();
10
11
  };
11
12
 
13
+ this.handleScroll = () => {
14
+ if (!this.lastMouseEvent) return;
15
+ this.updateMouseFollowerPosition(this.lastMouseEvent);
16
+ };
17
+
12
18
  window.addEventListener("message", (event) => {
13
19
  const { owner, type } = event.data;
14
20
  if (owner !== "dragging") return;
@@ -31,33 +37,31 @@ class IframeDragHandler {
31
37
  });
32
38
  }
33
39
 
34
- // Enables drag mode inside the iframe
35
40
  enableDragging() {
36
41
  this.addStopDraggingHandler();
37
42
  this.addMouseFollowerToDOM();
38
43
  this.addMouseFollowerHandler();
39
44
  this.highlightDropzone();
45
+ window.addEventListener("scroll", this.handleScroll, true);
40
46
  }
41
47
 
42
- // Disables drag mode
43
48
  disableDragging() {
44
49
  this.removeStopDraggingHandler();
45
50
  this.removeMouseFollowerFromDOM();
46
51
  this.removeMouseFollowerHandler();
47
52
  this.unhighlightDropzone();
53
+ window.removeEventListener("scroll", this.handleScroll, true);
54
+ this.lastMouseEvent = null;
48
55
  }
49
56
 
50
- // Adds a listener for mouseup to stop dragging
51
57
  addStopDraggingHandler() {
52
58
  window.addEventListener("mouseup", this.stopDragging);
53
59
  }
54
60
 
55
- // Removes the mouseup listener
56
61
  removeStopDraggingHandler() {
57
62
  window.removeEventListener("mouseup", this.stopDragging);
58
63
  }
59
64
 
60
- // Stops dragging and notifies the parent
61
65
  stopDragging = () => {
62
66
  this.sendParentMessage({
63
67
  owner: "iframe",
@@ -66,7 +70,6 @@ class IframeDragHandler {
66
70
  this.removeStopDraggingHandler();
67
71
  };
68
72
 
69
- // Adds mouse follower element to the DOM
70
73
  addMouseFollowerToDOM() {
71
74
  if (this.mouseFollowerInDOM()) return;
72
75
 
@@ -87,26 +90,22 @@ class IframeDragHandler {
87
90
  document.body.appendChild(div);
88
91
  }
89
92
 
90
- // Adds the mousemove listener
91
93
  addMouseFollowerHandler() {
92
94
  window.addEventListener("mousemove", this.handleMouseMove);
93
95
  }
94
96
 
95
- // Removes the mousemove listener
96
97
  removeMouseFollowerHandler() {
97
98
  window.removeEventListener("mousemove", this.handleMouseMove);
98
99
  }
99
100
 
100
- // Updates mouse follower position on mouse move
101
101
  updateMouseFollowerPosition(event) {
102
102
  const div = this.getMouseFollower();
103
103
  if (div) {
104
- div.style.left = `${event.clientX - this.mouseFollowerWidth / 2}px`;
105
- div.style.top = `${event.clientY - this.mouseFollowerHeight / 2}px`;
104
+ div.style.left = `${event.clientX + window.scrollX - this.mouseFollowerWidth / 2}px`;
105
+ div.style.top = `${event.clientY + window.scrollY - this.mouseFollowerHeight / 2}px`;
106
106
  }
107
107
  }
108
108
 
109
- // Checks if the mouse follower is outside the viewport
110
109
  checkMouseLeaveFrame() {
111
110
  const follower = this.getMouseFollower();
112
111
  if (!follower) return;
@@ -129,7 +128,6 @@ class IframeDragHandler {
129
128
  }
130
129
  }
131
130
 
132
- // Hides the mouse follower
133
131
  hideMouseFollower() {
134
132
  const div = this.getMouseFollower();
135
133
  if (div) {
@@ -137,7 +135,6 @@ class IframeDragHandler {
137
135
  }
138
136
  }
139
137
 
140
- // Shows the mouse follower
141
138
  showMouseFollower() {
142
139
  const div = this.getMouseFollower();
143
140
  if (div) {
@@ -145,7 +142,6 @@ class IframeDragHandler {
145
142
  }
146
143
  }
147
144
 
148
- // Removes the mouse follower from DOM
149
145
  removeMouseFollowerFromDOM() {
150
146
  const div = this.getMouseFollower();
151
147
  if (div) {
@@ -153,22 +149,18 @@ class IframeDragHandler {
153
149
  }
154
150
  }
155
151
 
156
- // Checks whether the follower is in the DOM
157
152
  mouseFollowerInDOM() {
158
153
  return !!this.getMouseFollower();
159
154
  }
160
155
 
161
- // Gets the follower element
162
156
  getMouseFollower() {
163
157
  return document.querySelector(".mouse-follower");
164
158
  }
165
159
 
166
- // Sends a message to the parent window
167
160
  sendParentMessage(message) {
168
161
  window.parent.postMessage(message, "*");
169
162
  }
170
163
 
171
- // Highlights the dropzone
172
164
  highlightDropzone() {
173
165
  const dropzones = document.querySelectorAll(".cms-block-dropzones");
174
166
 
@@ -177,7 +169,6 @@ class IframeDragHandler {
177
169
  });
178
170
  }
179
171
 
180
- // Removes highlight from dropzone
181
172
  unhighlightDropzone() {
182
173
  const dropzones = document.querySelectorAll(".cms-block-dropzones");
183
174