@websline/cms-view-utils 0.25.0 → 0.26.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 +1 -1
- package/src/components/Block.astro +2 -2
- package/src/components/PageRenderer.astro +25 -17
- package/src/data/pageData.js +5 -3
- package/src/index.js +0 -1
- package/src/styles/drag-drop-blocks.css +0 -64
- package/src/utils/dragDropUtils.js +0 -67
- package/src/utils/iframeDragHandler.js +0 -222
- package/src/utils/index.js +0 -2
package/package.json
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
const editToken = Astro.url.searchParams.get("edit_token") || null;
|
|
3
3
|
const isPreviewMode = editToken !== null && editToken !== "";
|
|
4
4
|
|
|
5
|
-
const { class: classes, style } = Astro.props;
|
|
5
|
+
const { class: classes, style, ...rest } = Astro.props;
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
{
|
|
9
9
|
isPreviewMode ? (
|
|
10
|
-
<div class={classes} data-edit-block {style}>
|
|
10
|
+
<div class={classes} data-edit-block {style} {...rest}>
|
|
11
11
|
<div data-edit-block-menu>
|
|
12
12
|
<div data-edit-block-item data-edit-block-item-edit>
|
|
13
13
|
Bearbeiten...
|
|
@@ -6,6 +6,28 @@ const editToken = Astro.url.searchParams.get("edit_token") || null;
|
|
|
6
6
|
const isPreviewMode = editToken !== null && editToken !== "";
|
|
7
7
|
|
|
8
8
|
await fetchPageData({ editToken, locals: Astro.locals, locale, slug });
|
|
9
|
+
|
|
10
|
+
const CMS_URL = import.meta.env.PUBLIC_CMS_URL?.replace(/\/+$/, "");
|
|
11
|
+
|
|
12
|
+
const styleURL = `${CMS_URL}/api/static/styles/drag-drop-blocks.css`;
|
|
13
|
+
const scriptDragHandlerURL = `${CMS_URL}/api/static/scripts/iframeDragHandler.js`;
|
|
14
|
+
const scriptDragDropUtilsURL = `${CMS_URL}/api/static/scripts/dragDropUtils.js`;
|
|
15
|
+
|
|
16
|
+
const previewScript = `
|
|
17
|
+
import IframeDragHandler from "${scriptDragHandlerURL}";
|
|
18
|
+
import {
|
|
19
|
+
disableLinks,
|
|
20
|
+
enableEditBlocks,
|
|
21
|
+
insertDropZones,
|
|
22
|
+
} from "${scriptDragDropUtilsURL}";
|
|
23
|
+
|
|
24
|
+
new IframeDragHandler();
|
|
25
|
+
insertDropZones();
|
|
26
|
+
disableLinks();
|
|
27
|
+
enableEditBlocks();
|
|
28
|
+
|
|
29
|
+
window.CMS_URL = "${CMS_URL}";
|
|
30
|
+
`;
|
|
9
31
|
---
|
|
10
32
|
|
|
11
33
|
<slot />
|
|
@@ -17,23 +39,9 @@ await fetchPageData({ editToken, locals: Astro.locals, locale, slug });
|
|
|
17
39
|
href="https://fonts.googleapis.com/css2?family=Ancizar+Sans:ital,wght@0,100..1000;1,100..1000&display=swap"
|
|
18
40
|
rel="stylesheet"
|
|
19
41
|
/>
|
|
20
|
-
<link
|
|
21
|
-
rel="stylesheet"
|
|
22
|
-
href="https://unpkg.com/@websline/cms-view-utils@latest/src/styles/drag-drop-blocks.css"
|
|
23
|
-
/>
|
|
42
|
+
<link rel="stylesheet" href={styleURL} />
|
|
24
43
|
</>
|
|
25
44
|
)
|
|
26
45
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
import {
|
|
30
|
-
disableLinks,
|
|
31
|
-
enableEditBlocks,
|
|
32
|
-
insertDropZones,
|
|
33
|
-
} from "https://unpkg.com/@websline/cms-view-utils@latest/src/utils/dragDropUtils.js";
|
|
34
|
-
|
|
35
|
-
new IframeDragHandler();
|
|
36
|
-
insertDropZones();
|
|
37
|
-
disableLinks();
|
|
38
|
-
enableEditBlocks();
|
|
39
|
-
</script>}
|
|
46
|
+
|
|
47
|
+
{isPreviewMode && <script type="module" set:html={previewScript} />}
|
package/src/data/pageData.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
const fetchPageData = async ({ editToken, locals, locale, slug }) => {
|
|
2
|
-
const
|
|
3
|
-
if (!
|
|
2
|
+
const CMS_URL = import.meta.env.PUBLIC_CMS_URL?.replace(/\/+$/, ""); // Remove trailing slashes
|
|
3
|
+
if (!CMS_URL) throw new Error("PUBLIC_CMS_URL ist nicht gesetzt.");
|
|
4
|
+
|
|
5
|
+
const API_URL = CMS_URL + "/api/public";
|
|
4
6
|
|
|
5
7
|
const key = `__pageData__${locale}__${slug}`;
|
|
6
8
|
|
|
7
9
|
if (!locals[key]) {
|
|
8
|
-
let url = `${
|
|
10
|
+
let url = `${API_URL}/${locale}/pages/${slug}`;
|
|
9
11
|
|
|
10
12
|
if (editToken) {
|
|
11
13
|
url += `?edit_token=${editToken}`;
|
package/src/index.js
CHANGED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
[data-drop-zone] {
|
|
2
|
-
height: 0px;
|
|
3
|
-
margin-left: auto;
|
|
4
|
-
margin-right: auto;
|
|
5
|
-
max-width: min(1440px, calc(100% - 48px));
|
|
6
|
-
transition: all 0.2s ease-in-out;
|
|
7
|
-
width: 100%;
|
|
8
|
-
}
|
|
9
|
-
[data-drop-zone].highlight {
|
|
10
|
-
border: 1.5px dashed var(--cms-drop-zone-border, #1168af);
|
|
11
|
-
height: 60px;
|
|
12
|
-
margin-top: 24px;
|
|
13
|
-
margin-bottom: 24px;
|
|
14
|
-
}
|
|
15
|
-
[data-drop-zone].highlight:hover {
|
|
16
|
-
background-color: var(--cms-drop-zone-background, rgba(17, 104, 175, 0.5));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
[data-edit-block] {
|
|
20
|
-
min-height: 40px;
|
|
21
|
-
position: relative;
|
|
22
|
-
user-select: none;
|
|
23
|
-
}
|
|
24
|
-
[data-edit-block-hover] {
|
|
25
|
-
&:after {
|
|
26
|
-
background: var(--cms-edit-block-background, #5aaff48c);
|
|
27
|
-
border-radius: 8px;
|
|
28
|
-
bottom: 0;
|
|
29
|
-
content: "";
|
|
30
|
-
display: block;
|
|
31
|
-
left: 0;
|
|
32
|
-
pointer-events: none;
|
|
33
|
-
position: absolute;
|
|
34
|
-
right: 0;
|
|
35
|
-
top: 0;
|
|
36
|
-
z-index: 1;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
[data-edit-block-hover] > [data-edit-block-menu] {
|
|
40
|
-
display: flex;
|
|
41
|
-
}
|
|
42
|
-
[data-edit-block-menu] {
|
|
43
|
-
background: var(--cms-edit-block-menu-bg, #1168af);
|
|
44
|
-
border-radius: 4px;
|
|
45
|
-
display: none;
|
|
46
|
-
left: 0;
|
|
47
|
-
overflow: hidden;
|
|
48
|
-
position: absolute;
|
|
49
|
-
top: -5px;
|
|
50
|
-
transform: translate(0, -100%);
|
|
51
|
-
}
|
|
52
|
-
[data-edit-block-item] {
|
|
53
|
-
color: var(--cms-edit-block-menu-color, #fff);
|
|
54
|
-
cursor: pointer;
|
|
55
|
-
font-family: "Ancizar Sans", sans-serif;
|
|
56
|
-
font-size: 14px;
|
|
57
|
-
font-weight: 400;
|
|
58
|
-
padding: 8px;
|
|
59
|
-
|
|
60
|
-
&:hover {
|
|
61
|
-
background: var(--cms-edit-block-menu-bg-hover, #0d5d9e);
|
|
62
|
-
color: var(--cms-edit-block-menu-color-hover, #fff);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
const insertDropZones = (blockAreaSelector = "[data-block-area]") => {
|
|
2
|
-
const blockArea = document.querySelector(blockAreaSelector);
|
|
3
|
-
if (!blockArea) return;
|
|
4
|
-
|
|
5
|
-
const children = Array.from(blockArea.children);
|
|
6
|
-
let lastIndex = 0;
|
|
7
|
-
|
|
8
|
-
children.forEach((child) => {
|
|
9
|
-
const dropZone = document.createElement("div");
|
|
10
|
-
dropZone.setAttribute("data-drop-zone", "");
|
|
11
|
-
dropZone.setAttribute("data-drop-zone-index", lastIndex.toString());
|
|
12
|
-
blockArea.insertBefore(dropZone, child);
|
|
13
|
-
|
|
14
|
-
lastIndex++;
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
const finalDropZone = document.createElement("div");
|
|
18
|
-
finalDropZone.setAttribute("data-drop-zone", "");
|
|
19
|
-
finalDropZone.setAttribute("data-drop-zone-index", lastIndex.toString());
|
|
20
|
-
blockArea.appendChild(finalDropZone);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const disableLinks = () => {
|
|
24
|
-
const hrefs = document.querySelectorAll("a");
|
|
25
|
-
|
|
26
|
-
hrefs.forEach((a) => {
|
|
27
|
-
a.addEventListener("click", (e) => {
|
|
28
|
-
alert("Vorschau-Modus: Links sind deaktiviert.");
|
|
29
|
-
e.preventDefault();
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const enableEditBlocks = () => {
|
|
35
|
-
const editBlocks = document.querySelectorAll("[data-edit-block]");
|
|
36
|
-
let activeBlock = null;
|
|
37
|
-
let timeoutId;
|
|
38
|
-
|
|
39
|
-
editBlocks.forEach((el) => {
|
|
40
|
-
el.addEventListener("mouseover", () => {
|
|
41
|
-
if (document.body.classList.contains("cms-editor-is-dragging")) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
clearTimeout(timeoutId);
|
|
46
|
-
|
|
47
|
-
if (activeBlock && activeBlock !== el) {
|
|
48
|
-
activeBlock.removeAttribute("data-edit-block-hover");
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
activeBlock = el;
|
|
52
|
-
el.setAttribute("data-edit-block-hover", "");
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
el.addEventListener("mouseout", () => {
|
|
56
|
-
timeoutId = setTimeout(() => {
|
|
57
|
-
el.removeAttribute("data-edit-block-hover");
|
|
58
|
-
|
|
59
|
-
if (activeBlock === el) {
|
|
60
|
-
activeBlock = null;
|
|
61
|
-
}
|
|
62
|
-
}, 150);
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export { disableLinks, enableEditBlocks, insertDropZones };
|
|
@@ -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;
|
package/src/utils/index.js
DELETED