-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathEditing.ts
214 lines (190 loc) · 5.37 KB
/
Editing.ts
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
import { LitElement } from 'lit';
import { property, state } from 'lit/decorators.js';
import {
AttributeValue,
Edit,
EditEvent,
Insert,
isComplex,
isInsert,
isNamespaced,
isRemove,
isUpdate,
LitElementConstructor,
OpenEvent,
Remove,
Update,
} from '../foundation.js';
function localAttributeName(attribute: string): string {
return attribute.includes(':') ? attribute.split(':', 2)[1] : attribute;
}
function handleInsert({
parent,
node,
reference,
}: Insert): Insert | Remove | [] {
try {
const { parentNode, nextSibling } = node;
parent.insertBefore(node, reference);
if (parentNode)
return {
node,
parent: parentNode,
reference: nextSibling,
};
return { node };
} catch (e) {
// do nothing if insert doesn't work on these nodes
return [];
}
}
function handleUpdate({ element, attributes }: Update): Update {
const oldAttributes = { ...attributes };
Object.entries(attributes)
.reverse()
.forEach(([name, value]) => {
let oldAttribute: AttributeValue;
if (isNamespaced(value!))
oldAttribute = {
value: element.getAttributeNS(
value.namespaceURI,
localAttributeName(name)
),
namespaceURI: value.namespaceURI,
};
else
oldAttribute = element.getAttributeNode(name)?.namespaceURI
? {
value: element.getAttribute(name),
namespaceURI: element.getAttributeNode(name)!.namespaceURI!,
}
: element.getAttribute(name);
oldAttributes[name] = oldAttribute;
});
for (const entry of Object.entries(attributes)) {
try {
const [attribute, value] = entry as [string, AttributeValue];
if (isNamespaced(value)) {
if (value.value === null)
element.removeAttributeNS(
value.namespaceURI,
localAttributeName(attribute)
);
else element.setAttributeNS(value.namespaceURI, attribute, value.value);
} else if (value === null) element.removeAttribute(attribute);
else element.setAttribute(attribute, value);
} catch (e) {
// do nothing if update doesn't work on this attribute
delete oldAttributes[entry[0]];
}
}
return {
element,
attributes: oldAttributes,
};
}
function handleRemove({ node }: Remove): Insert | [] {
const { parentNode: parent, nextSibling: reference } = node;
node.parentNode?.removeChild(node);
if (parent)
return {
node,
parent,
reference,
};
return [];
}
function handleEdit(edit: Edit): Edit {
if (isInsert(edit)) return handleInsert(edit);
if (isUpdate(edit)) return handleUpdate(edit);
if (isRemove(edit)) return handleRemove(edit);
if (isComplex(edit)) return edit.map(handleEdit).reverse();
return [];
}
export type LogEntry = { undo: Edit; redo: Edit };
export interface EditingMixin {
doc: XMLDocument;
history: LogEntry[];
editCount: number;
last: number;
canUndo: boolean;
canRedo: boolean;
docs: Record<string, XMLDocument>;
docName: string;
handleOpenDoc(evt: OpenEvent): void;
handleEditEvent(evt: EditEvent): void;
undo(n?: number): void;
redo(n?: number): void;
}
type ReturnConstructor = new (...args: any[]) => LitElement & EditingMixin;
/** A mixin for editing a set of [[docs]] using [[EditEvent]]s */
export function Editing<TBase extends LitElementConstructor>(
Base: TBase
): TBase & ReturnConstructor {
class EditingElement extends Base {
@state()
/** The `XMLDocument` currently being edited */
get doc(): XMLDocument {
return this.docs[this.docName];
}
@state()
history: LogEntry[] = [];
@state()
editCount: number = 0;
@state()
get last(): number {
return this.editCount - 1;
}
@state()
get canUndo(): boolean {
return this.last >= 0;
}
@state()
get canRedo(): boolean {
return this.editCount < this.history.length;
}
/**
* The set of `XMLDocument`s currently loaded
*
* @prop {Record} docs - Record of loaded XML documents
*/
@state()
docs: Record<string, XMLDocument> = {};
/**
* The name of the [[`doc`]] currently being edited
*
* @prop {String} docName - name of the document that is currently being edited
*/
@property({ type: String, reflect: true }) docName = '';
handleOpenDoc({ detail: { docName, doc } }: OpenEvent) {
this.docName = docName;
this.docs[this.docName] = doc;
}
handleEditEvent(event: EditEvent) {
const edit = event.detail;
this.history.splice(this.editCount);
this.history.push({ undo: handleEdit(edit), redo: edit });
this.editCount += 1;
}
/** Undo the last `n` [[Edit]]s committed */
undo(n = 1) {
if (!this.canUndo || n < 1) return;
handleEdit(this.history[this.last!].undo);
this.editCount -= 1;
if (n > 1) this.undo(n - 1);
}
/** Redo the last `n` [[Edit]]s that have been undone */
redo(n = 1) {
if (!this.canRedo || n < 1) return;
handleEdit(this.history[this.editCount].redo);
this.editCount += 1;
if (n > 1) this.redo(n - 1);
}
constructor(...args: any[]) {
super(...args);
this.addEventListener('oscd-open', this.handleOpenDoc);
this.addEventListener('oscd-edit', event => this.handleEditEvent(event));
}
}
return EditingElement;
}