2019-05-08 15:31:43 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-05-22 17:16:32 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-08 15:31:43 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function renderModel(editor, model) {
|
2019-05-13 17:21:57 +03:00
|
|
|
const lines = model.parts.reduce((lines, part) => {
|
|
|
|
if (part.type === "newline") {
|
|
|
|
lines.push([]);
|
|
|
|
} else {
|
|
|
|
const lastLine = lines[lines.length - 1];
|
|
|
|
lastLine.push(part);
|
2019-05-08 15:31:43 +03:00
|
|
|
}
|
2019-05-13 17:21:57 +03:00
|
|
|
return lines;
|
|
|
|
}, [[]]);
|
2019-05-14 17:49:53 +03:00
|
|
|
// TODO: refactor this code, DRY it
|
2019-05-13 17:21:57 +03:00
|
|
|
lines.forEach((parts, i) => {
|
|
|
|
let lineContainer = editor.childNodes[i];
|
|
|
|
while (lineContainer && (lineContainer.tagName !== "DIV" || !!lineContainer.className)) {
|
|
|
|
editor.removeChild(lineContainer);
|
|
|
|
lineContainer = editor.childNodes[i];
|
2019-05-08 15:31:43 +03:00
|
|
|
}
|
2019-05-13 17:21:57 +03:00
|
|
|
if (!lineContainer) {
|
|
|
|
lineContainer = document.createElement("div");
|
|
|
|
editor.appendChild(lineContainer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parts.length) {
|
|
|
|
parts.forEach((part, j) => {
|
|
|
|
let partNode = lineContainer.childNodes[j];
|
|
|
|
while (partNode && !part.canUpdateDOMNode(partNode)) {
|
|
|
|
lineContainer.removeChild(partNode);
|
|
|
|
partNode = lineContainer.childNodes[j];
|
|
|
|
}
|
|
|
|
if (partNode && part) {
|
|
|
|
part.updateDOMNode(partNode);
|
|
|
|
} else if (part) {
|
|
|
|
lineContainer.appendChild(part.toDOMNode());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let surplusElementCount = Math.max(0, lineContainer.childNodes.length - parts.length);
|
|
|
|
while (surplusElementCount) {
|
|
|
|
lineContainer.removeChild(lineContainer.lastChild);
|
|
|
|
--surplusElementCount;
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-13 19:45:54 +03:00
|
|
|
// empty div needs to have a BR in it to give it height
|
2019-05-13 17:21:57 +03:00
|
|
|
let foundBR = false;
|
|
|
|
let partNode = lineContainer.firstChild;
|
|
|
|
while (partNode) {
|
2019-05-28 11:01:16 +03:00
|
|
|
const nextNode = partNode.nextSibling;
|
2019-05-13 17:21:57 +03:00
|
|
|
if (!foundBR && partNode.tagName === "BR") {
|
|
|
|
foundBR = true;
|
|
|
|
} else {
|
|
|
|
lineContainer.removeChild(partNode);
|
|
|
|
}
|
2019-05-28 11:01:16 +03:00
|
|
|
partNode = nextNode;
|
2019-05-13 17:21:57 +03:00
|
|
|
}
|
|
|
|
if (!foundBR) {
|
|
|
|
lineContainer.appendChild(document.createElement("br"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let surplusElementCount = Math.max(0, editor.childNodes.length - lines.length);
|
|
|
|
while (surplusElementCount) {
|
|
|
|
editor.removeChild(editor.lastChild);
|
|
|
|
--surplusElementCount;
|
|
|
|
}
|
|
|
|
});
|
2019-05-08 15:31:43 +03:00
|
|
|
}
|