-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattributes.js
More file actions
176 lines (151 loc) · 5.02 KB
/
Copy pathattributes.js
File metadata and controls
176 lines (151 loc) · 5.02 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
'use strict';
// This file is safe for both client-side (esbuild bundled) and server-side use.
// It contains NO requires for Node.js or ep_etherpad-lite server modules.
// Server-only export hooks are in attributes-server.js
const createLineAttribute = (config) => {
const {attr, tags, normalize} = config;
const aceAttribsToClasses = (hookName, context) => {
if (context.key === attr) {
return [`${attr}:${context.value}`];
}
};
const aceDomLineProcessLineAttributes = (hookName, context) => {
const cls = context.cls;
const match = new RegExp(`(?:^| )${attr}:([A-Za-z0-9]*)`).exec(cls);
if (match) {
let tag = match[1];
if (normalize) tag = normalize(tag);
if (tags.indexOf(tag) >= 0) {
return [{
preHtml: `<${tag}>`,
postHtml: `</${tag}>`,
processedMarker: true,
}];
}
}
return [];
};
const aceRegisterBlockElements = () => tags;
// Server-side counterpart of aceRegisterBlockElements. Etherpad core's
// import-side content collector keeps its own `_blockElems` set
// separate from the editor's, so plugins that want their custom block
// tags treated as block elements on import (so adjacent ones don't
// merge into a single pad line) need to register them on the server
// side too. A plugin that uses `lineAttribute({tags: [...]})` should
// re-export this hook from its `ep.json` under
// `"ccRegisterBlockElements"`.
const ccRegisterBlockElements = () => tags;
const collectContentPre = (hookName, context, cb) => {
const tname = context.tname;
const lineAttributes = context.state.lineAttributes;
if (tname === 'div' || tname === 'p') {
delete lineAttributes[attr];
}
if (tags.indexOf(tname) >= 0) {
lineAttributes[attr] = tname;
}
if (cb) return cb();
};
const collectContentPost = (hookName, context, cb) => {
const tname = context.tname;
const lineAttributes = context.state.lineAttributes;
if (tags.indexOf(tname) >= 0) {
delete lineAttributes[attr];
}
if (cb) return cb();
};
const aceRegisterLineAttributes = () => [attr];
return {
aceAttribsToClasses,
aceDomLineProcessLineAttributes,
aceRegisterBlockElements,
aceRegisterLineAttributes,
ccRegisterBlockElements,
collectContentPre,
collectContentPost,
};
};
const createInlineAttribute = (config) => {
const {attr, values} = config;
const attrPattern = new RegExp(`(?:^| )${attr}:([A-Za-z0-9]*)`);
const aceAttribsToClasses = (hookName, context) => {
if (context.key.indexOf(`${attr}:`) !== -1) {
const match = attrPattern.exec(context.key);
if (match) return [`${attr}:${match[1]}`];
}
if (context.key === attr) {
return [`${attr}:${context.value}`];
}
};
const aceCreateDomLine = (hookName, context) => {
const cls = context.cls;
const match = attrPattern.exec(cls);
if (!match) return [];
if (values && values.indexOf(match[1]) < 0) return [];
return [{extraOpenTags: '', extraCloseTags: '', cls}];
};
const collectContentPre = (hookName, context) => {
const match = attrPattern.exec(context.cls);
if (match && match[1]) {
context.cc.doAttrib(context.state, `${attr}::${match[1]}`);
}
};
const collectContentPost = () => {};
return {
aceAttribsToClasses,
aceCreateDomLine,
collectContentPre,
collectContentPost,
};
};
const createTagAttribute = (config) => {
const {tags} = config;
const aceAttribClasses = (hookName, attr) => {
for (const tag of tags) {
attr[tag] = `tag:${tag}`;
}
return attr;
};
const aceAttribsToClasses = (hookName, context) => {
if (tags.includes(context.key)) {
return [context.key];
}
};
const aceRegisterBlockElements = () => tags;
// Server-side counterpart of aceRegisterBlockElements. Etherpad core's
// import-side content collector keeps its own `_blockElems` set
// separate from the editor's, so plugins that want their custom block
// tags treated as block elements on import (so adjacent ones don't
// merge into a single pad line) need to register them on the server
// side too. A plugin that uses `lineAttribute({tags: [...]})` should
// re-export this hook from its `ep.json` under
// `"ccRegisterBlockElements"`.
const ccRegisterBlockElements = () => tags;
const collectContentPre = (hookName, context, cb) => {
const tname = context.tname;
if (tags.includes(tname)) {
context.cc.doAttrib(context.state, tname);
}
if (cb) return cb();
};
const collectContentPost = (hookName, context, cb) => {
if (cb) return cb();
};
return {
aceAttribClasses,
aceAttribsToClasses,
aceRegisterBlockElements,
ccRegisterBlockElements,
collectContentPre,
collectContentPost,
};
};
module.exports = {
lineAttribute: createLineAttribute,
inlineAttribute: createInlineAttribute,
tagAttribute: createTagAttribute,
// Keep old names as aliases for backwards compatibility
createLineAttribute,
createInlineAttribute,
createTagAttribute,
};