Skip to content

Commit 4d26d64

Browse files
author
Petro Salema
committed
Implement MutationTrees.insert
1 parent 693d220 commit 4d26d64

3 files changed

Lines changed: 42 additions & 37 deletions

File tree

sandbox/index.html

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,35 @@ <h1>Aloha Editor</h1>
152152

153153
var editable = document.querySelector('.aloha-editable');
154154
var boundaries = [
155+
/*
155156
aloha.boundaries.create(editable.querySelector('h6').firstChild, 2),
156157
aloha.boundaries.create(document.querySelector('b').firstChild, 0),
158+
*/
157159
aloha.boundaries.create(document.querySelector('b').firstChild, 1),
160+
/*
158161
aloha.boundaries.create(document.querySelector('b').firstChild, 2),
159162
aloha.boundaries.create(document.querySelector('b').firstChild, 3),
160163
aloha.boundaries.create(document.querySelector('b'), 1),
161164
aloha.boundaries.create(document.querySelector('b'), 1),
162165
aloha.boundaries.fromNode(document.querySelector('b')),
163166
aloha.boundaries.create(document.querySelector('b').firstChild, 2)
167+
*/
164168
];
165169
var limit = editable; //.querySelector('b').parentNode.parentNode;
166170
var splitBoundary = aloha.boundaries.create(limit.querySelector('b').firstChild, 1);
167171
var path = aloha.paths.fromBoundary(limit, splitBoundary);
168172
var tree = aloha.MutationTrees.create(limit, boundaries);
169173
var result = aloha.MutationTrees.split(tree, path)
170-
var updated = aloha.MutationTrees.update(result[0]);
171-
console.log(updated.domNode(), aloha.markers.hint(aloha.paths.toBoundary(updated.domNode(), result[1])));
174+
tree = result[0];
175+
path = result[1];
176+
tree = aloha.MutationTrees.insert(tree, path, [
177+
document.createTextNode('apple'),
178+
document.createTextNode('orange')
179+
]);
180+
//tree.updateDom();
181+
var updated = aloha.MutationTrees.update(tree);
182+
183+
//console.log(updated.domNode(), aloha.markers.hint(aloha.paths.toBoundary(updated.domNode(), result[1])));
172184
}
173185
if ('undefined' === typeof aloha) {
174186
setVersion('src');

src/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ define([
554554

555555
exports['MutationTrees'] = new Object();
556556
exports['MutationTrees'].split = MutationTrees.split;
557+
exports['MutationTrees'].insert = MutationTrees.insert;
557558
exports['MutationTrees'].create = MutationTrees.create;
558559
exports['MutationTrees'].update = MutationTrees.update;
559560

src/mutation-trees.js

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -167,44 +167,45 @@ define([
167167
return index;
168168
}
169169

170-
function insertPath(record, offset) {
171-
var records;
170+
function insertRecords(record, offset, records) {
172171
if (isTextRecord(record)) {
173-
records = spliceContent(offset, 0, record, Marker());
174-
return FragmentedText(record, records);
172+
return spliceContent(offset, 0, record, records);
175173
}
176174
if (isFragmentedText(record)) {
177175
var offsets = fragmentedOffset(record, offset);
178176
offset = offsets[0];
179177
if (2 === offsets.length) {
180178
var text = record.children()[offset];
181-
var fragments = spliceContent(offsets[1], 0, text, Marker());
182-
records = spliceContent(offset, 1, record, fragments);
183-
return record.children(records);
179+
var fragments = spliceContent(offsets[1], 0, text, records);
180+
return record.children(spliceContent(offset, 1, record, fragments));
184181
}
185182
} else {
186183
offset = normalizeOffset(record, offset);
187184
}
188-
records = spliceContent(offset, 0, record, Marker());
189-
return record.children(records);
185+
return record.children(spliceContent(offset, 0, record, records));
190186
}
191187

192-
function insertPathInTree(record, path) {
188+
function insertRec(record, path, insertAt) {
193189
var trail = path.slice(1);
194190
var offset = path[0];
195191
// Because we are at end of the trail: <a/>|<b/> or <#text "foo|bar">
196192
if (0 === trail.length) {
197-
return insertPath(record, offset);
193+
return insertAt(record, offset);
198194
}
199195
offset = normalizeOffset(record, offset);
200196
var unchanged = record.children()[offset];
201-
var changed = insertPathInTree(unchanged, trail);
197+
var changed = insertRec(unchanged, trail, insertAt);
202198
var children = spliceContent(offset, 1, record, changed);
203199
return record.children(children);
204200
}
205201

206-
function insertPathsInTree(record, paths) {
207-
return paths.reduce(insertPathInTree, record);
202+
function insertPath(record, offset) {
203+
var records = insertRecords(record, offset, Marker());
204+
return isTextRecord(record) ? FragmentedText(record, records) : record;
205+
}
206+
207+
function insertPathInTree(record, path) {
208+
return insertRec(record, path, insertPath);
208209
}
209210

210211
function boundaryIndexInFragment(fragments, offset) {
@@ -229,6 +230,7 @@ define([
229230
var text = fragments.reduce(function (strings, string) {
230231
return strings.concat(string.text());
231232
}, []).join('');
233+
console.log(text);
232234
//var doc = record.original.domNode().ownerDocument;
233235
var doc = document;
234236
return (record.original && record.original.text() === text)
@@ -315,7 +317,7 @@ define([
315317
var clipped = paths.map(Fn.partial(clipCommonRoot, root)).filter(function (arr) {
316318
return arr.length > 0;
317319
});
318-
return insertPathsInTree(Boromir(element), clipped);
320+
return clipped.reduce(insertPathInTree, Boromir(element));
319321
}
320322

321323
/**
@@ -337,26 +339,16 @@ define([
337339
];
338340
}
339341

340-
function insert(tree, content) {
341-
/*
342-
var node = Boromir(
343-
('string' === typeof content)
344-
? Boundaries.document(boundary).createTextNode(content)
345-
: content
346-
);
347-
var container = Boundaries.container(boundary);
348-
var editable = Dom.editingHost(container);
349-
var path = Paths.fromBoundary(editable, boundary);
350-
var unpreserved = boundaries.map(Fn.partial(Paths.fromBoundary, editable));
351-
var unchanged = Boromir(container);
352-
var offset = Boundaries.offset(boundary);
353-
var children = unchanged.children();
354-
unchanged.children(
355-
children.slice(0, offset).concat(node).concat(children.slice(offset))
356-
).updateDom();
357-
var preserved = adjustForInsertBefore(path, unpreserved);
358-
return preserved.map(Fn.partial(Paths.toBoundary, editable));
359-
*/
342+
function insert(tree, path, content) {
343+
if (0 === content.length) {
344+
return tree;
345+
}
346+
var records = content.map(function (item) {
347+
return item instanceof Boromir ? item : Boromir(item);
348+
});
349+
return insertRec(tree, path, function (record, offset) {
350+
return insertRecords(record, offset, records);
351+
});
360352
}
361353

362354
return {

0 commit comments

Comments
 (0)