-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathimage.ts
More file actions
166 lines (136 loc) · 5.68 KB
/
image.ts
File metadata and controls
166 lines (136 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Node as ProsemirrorNode } from "prosemirror-model";
import { EditorView, NodeView } from "prosemirror-view";
import { escapeHTML, generateRandomId } from "../../shared/utils";
//NOTE relies on Stacks classes. Should we separate out so the view is more agnostic?
type getPosParam = boolean | (() => number);
export class ImageView implements NodeView {
dom: Node | null;
img: HTMLImageElement;
popover: HTMLElement;
form: HTMLFormElement;
id: string;
selectionActive: boolean;
constructor(node: ProsemirrorNode, view: EditorView, getPos: getPosParam) {
this.id = generateRandomId();
this.img = this.createImage(node);
this.form = this.createForm();
this.form.addEventListener("submit", (event) =>
this.handleChangedImageAttributes(event, getPos, view)
);
this.popover = this.createPopover();
this.dom = document.createElement("span");
this.dom.appendChild(this.img);
this.dom.appendChild(this.popover);
this.dom.addEventListener("s-popover:hide", (event: Event) =>
this.preventClose(event)
);
}
selectNode(): void {
this.img.classList.add("bs-ring");
}
deselectNode(): void {
this.img.classList.remove("bs-ring");
this.selectionActive = false;
// tell Stacks to hide the popover
this.img.dispatchEvent(new Event("image-popover-hide"));
}
stopEvent(event: Event): boolean {
// prevent elements from within the tooltip to bubble up to the outer editor
// as this would make the editor view close the tooltip view right away
return this.popover.contains(event.target as Element);
}
ignoreMutation(): boolean {
return true;
}
destroy(): void {
this.img.remove();
this.popover.remove();
this.dom = null;
this.form.remove();
}
private createImage(node: ProsemirrorNode): HTMLImageElement {
const img = document.createElement("img");
img.setAttribute("aria-controls", `img-popover-${this.id}`);
img.setAttribute("data-controller", "s-popover");
img.setAttribute(
"data-action",
"image-popover-show->s-popover#show image-popover-hide->s-popover#hide"
);
img.src = node.attrs.src as string;
if (node.attrs.alt) img.alt = node.attrs.alt as string;
if (node.attrs.title) img.title = node.attrs.title as string;
img.onclick = () => this.handleClick();
return img;
}
private createForm(): HTMLFormElement {
const form = document.createElement("form");
form.className = "d-flex fd-column";
form.innerHTML = escapeHTML`
<label class="flex--item s-label mb4" for="img-src-${this.id}">Image source</label>
<div class="d-flex ps-relative mb12">
<input class="flex--item s-input" type="text" name="src" id="img-src-${this.id}" value="${this.img.src}" placeholder="https://example.com/image.png"/>
</div>
<label class="flex--item s-label mb4" for="img-alt-${this.id}">Image description</label>
<div class="d-flex ps-relative mb12">
<input class="flex--item s-input" type="text" name="alt" id="img-alt-${this.id}" value="${this.img.alt}" placeholder="A description for the image"/>
</div>
<label class="flex--item s-label mb4" for="img-title-${this.id}">Title</label>
<div class="d-flex ps-relative mb12">
<input class="flex--item s-input" type="text" name="title" id="img-title-${this.id}" value="${this.img.title}" placeholder="A title shown on hover"/>
</div>
<button class="s-btn s-btn__primary" type="submit" aria-pressed="false">Apply</button>
`;
return form;
}
private createPopover(): HTMLDivElement {
const popover = document.createElement("div");
popover.className = "s-popover ws-normal wb-normal js-img-popover";
popover.id = `img-popover-${this.id}`;
// TODO added `ws-normal` to fix FF only bug. Will file bug against Stacks and revisit
popover.innerHTML = `<div class="s-popover--arrow ws-normal"></div>`;
popover.append(this.form);
return popover;
}
private handleChangedImageAttributes(
event: Event,
getPos: getPosParam,
view: EditorView
) {
event.preventDefault();
if (typeof getPos !== "function") return;
const findInput = (selector: string): HTMLInputElement =>
this.form.querySelector(selector);
const src = findInput(`#img-src-${this.id}`);
const alt = findInput(`#img-alt-${this.id}`);
const title = findInput(`#img-title-${this.id}`);
view.dispatch(
view.state.tr.setNodeMarkup(getPos(), null, {
src: src.value,
alt: alt.value,
title: title.value,
})
);
view.focus();
}
// don't let Stacks close the popover if the image
// element is still selected in the editor
private preventClose(event: Event) {
if (this.selectionActive) {
event.preventDefault();
}
}
/**
* We want to trigger Stacks' showing and hiding of popovers whenever an image is clicked
*/
private handleClick(): void {
this.selectionActive = true;
// tell Stacks to hide the popover
this.img.dispatchEvent(new Event("image-popover-show"));
const inputFields = this.form.querySelectorAll("input");
if (inputFields.length > 0) {
inputFields[0].focus({
preventScroll: true,
});
}
}
}