-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcss.js
More file actions
233 lines (188 loc) · 6.06 KB
/
Copy pathcss.js
File metadata and controls
233 lines (188 loc) · 6.06 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import React from 'react';
import ReactDOM from 'react-dom';
import { Mark, mergeAttributes } from '@tiptap/core';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
export const removeDialogs = () => {
const prevDialogs = document.querySelectorAll('.insert-css-dialog');
prevDialogs.forEach((s) => s.remove());
};
const insertDialog = ({ editor, callback, opts, selectedText, parentNode }) => {
const editorDOM = editor.options.element;
const newEl = document.createElement('div');
const { selection } = editor.state;
removeDialogs();
newEl.className = 'insert-css-dialog';
let popoverEl;
const closePopOver = () => {
if (popoverEl) {
popoverEl.remove();
}
};
let firstCallMade = false;
const listener = (e) => {
// this will be triggered right after setting it because
// this toolbar is added on the mousedown event
// so right after mouseup, the click will be triggered
if (firstCallMade) {
const focusIsInModals = newEl.contains(e.target) || (popoverEl && popoverEl.contains(e.target));
const focusIsInEditor = editorDOM.contains(e.target);
if (!(focusIsInModals || focusIsInEditor)) {
handleClose();
}
} else {
firstCallMade = true;
}
};
const handleClose = () => {
callback(undefined, true);
newEl.remove();
closePopOver();
document.body.removeEventListener('click', listener);
};
const handleChange = (name) => {
callback(name, true);
newEl.remove();
closePopOver();
document.body.removeEventListener('click', listener);
};
const parentNodeClass = parentNode?.attrs.class;
const createHTML = (name) => {
let html = `<span class="${name}">${selectedText}</span>`;
if (parentNode) {
let tag = 'span';
if (parentNode?.object === 'inline') {
tag = 'span';
}
if (parentNode?.object === 'block') {
tag = 'div';
}
html = `<${tag} class="${parentNodeClass}">${parentNode.text.slice(
0,
selection.$anchor.textOffset,
)}${html}${parentNode.text.slice(selection.$head.textOffset)}</${tag}>`;
}
return html;
};
const el = (
<div
style={{ background: 'white', height: 500, padding: 20, overflow: 'hidden', display: 'flex', flexFlow: 'column' }}
>
<h2>Please choose a css class</h2>
{parentNodeClass && <div>The current parent has this class {parentNodeClass}</div>}
<List component="nav" style={{ overflow: 'scroll' }}>
{opts.names.map((name, i) => (
<ListItem key={`rule-${i}`} button onClick={() => handleChange(name)}>
<div style={{ marginRight: 20 }}>{name}</div>
<div
dangerouslySetInnerHTML={{
__html: createHTML(name),
}}
/>
</ListItem>
))}
</List>
</div>
);
ReactDOM.render(el, newEl, () => {
const cursorItem = editor.view.nodeDOM(editor.state.selection.from);
const cursorNode = cursorItem?.parentNode;
if (cursorNode) {
const bodyRect = editorDOM.parentElement.parentElement.parentElement.getBoundingClientRect();
const boundRect = cursorNode.getBoundingClientRect();
editorDOM.parentElement.parentElement.parentElement.appendChild(newEl);
newEl.style.maxWidth = '500px';
newEl.style.position = 'absolute';
newEl.style.top = 0;
newEl.style.zIndex = 99999;
const leftValue = `${boundRect.left + Math.abs(bodyRect.left) + cursorNode.offsetWidth + 10}px`;
const rightValue = `${boundRect.x}px`;
newEl.style.left = leftValue;
const leftAlignedWidth = newEl.offsetWidth;
newEl.style.left = 'unset';
newEl.style.right = rightValue;
const rightAlignedWidth = newEl.offsetWidth;
newEl.style.left = 'unset';
newEl.style.right = 'unset';
if (leftAlignedWidth >= rightAlignedWidth) {
newEl.style.left = leftValue;
} else {
newEl.style.right = rightValue;
}
document.body.addEventListener('click', listener);
}
});
};
export const CSSMark = Mark.create({
name: 'cssmark',
addOptions() {
return {
classes: [],
};
},
addAttributes() {
return {
class: {
default: null,
parseHTML: (el) => el.getAttribute('class'),
renderHTML: (attributes) => {
if (!attributes.class) return {};
return { class: attributes.class };
},
},
};
},
parseHTML() {
// Any span with a class that matches one of allowed classes any span that carries a class attribute
// so that pre-existing spans are preserved when loading content
return [
{
tag: 'span[class]',
getAttrs: (el) => {
const cls = el.getAttribute('class') || '';
if (!cls) {
return false;
}
const allowedClasses = (this.options && this.options.classes) || [];
if (allowedClasses.length > 0) {
const match = this.options.classes.find((name) => cls.includes(name));
return match ? { class: match } : false;
}
return { class: cls };
},
},
];
},
renderHTML({ HTMLAttributes }) {
return ['span', mergeAttributes(HTMLAttributes), 0];
},
addCommands() {
return {
setCSSClass:
(className) =>
({ commands }) => {
return commands.setMark(this.name, { class: className });
},
unsetCSSClass:
() =>
({ commands }) => {
return commands.unsetMark(this.name);
},
openCSSClassDialog:
() =>
({ editor }) => {
insertDialog({
editor,
selectedText: editor.state.doc.textBetween(editor.state.selection.from, editor.state.selection.to),
parentNode: editor.state.selection.$from.nodeAfter,
opts: this.options.extraCSSRules,
callback: (className) => {
if (className) {
editor.chain().focus().setCSSClass(className).run();
}
},
});
},
};
},
});