/
footnote-pm.js
130 lines (126 loc) · 4.86 KB
/
footnote-pm.js
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
window.pm = {
ProseMirror: require("../../prosemirror/src/edit/main").ProseMirror,
Pos: require("../../prosemirror/src/model").Pos,
Node: require("../../prosemirror/src/model").Node,
fromDOM: require("../../prosemirror/src/parse/dom").fromDOM,
fromHTML: require("../../prosemirror/src/parse/dom").fromHTML,
fidusSchema: require("./footnote-pm-schema").fidusSchema,
fidusFnSchema: require("./footnote-pm-schema").fidusFnSchema,
MenuBar: require("../../prosemirror/src/menu/menubar").MenuBar
};
var where = document.getElementById('editor'),
doc = pm.fromDOM(pm.fidusSchema, where),
editor, fnEditor,
lastFootnotes = [],
findFootnotes = node => {
var footnotes = [];
if (node.type.name === 'footnote') {
footnotes.push(node);
}
if (node.content && node.content.content) {
node.content.content.forEach(function(node) {
footnotes = footnotes.concat(findFootnotes(node));
})
}
return footnotes;
},
getNodePos = (currentNode, searchedNode, searchedNumber = 0, path = [], fromOffset = 0, toOffset = 0, counter = {
hits: 0
}) => {
var index = 0,
foundPos = false;
if (currentNode === searchedNode) {
if (searchedNumber === counter.hits) {
return {
from: new pm.Pos(path, fromOffset),
to: new pm.Pos(path, toOffset)
}
}
counter.hits++;
}
currentNode.forEach(function(childNode, fromOffset, toOffset) {
var childPos;
if (childNode.isInline) {
childPos = getNodePos(childNode, searchedNode, searchedNumber, path, fromOffset, toOffset, counter);
} else {
childPos = getNodePos(childNode, searchedNode, searchedNumber, path.concat(index), fromOffset, toOffset, counter);
}
if (childPos !== false) {
foundPos = childPos;
}
index++;
})
if (foundPos !== false) {
return foundPos;
}
return false;
},
sameArrayContents = (arrayOne, arrayTwo) => {
if (arrayOne.length != arrayTwo.length) {
return false;
}
return arrayOne.every(function(element, index) {
return element === arrayTwo[index];
})
},
renderFootnotes = function() {
var currentFootnotes = findFootnotes(editor.doc),
footnotesHTML = '';
if (sameArrayContents(currentFootnotes, lastFootnotes)) {
return true;
}
console.log('redrawing footnotes');
currentFootnotes.forEach(footnote => {
footnotesHTML += "<div class='footnote-container'>" + footnote.attrs.fnContents + "</div>"
})
fnEditor.setContent(pm.fromHTML(pm.fidusFnSchema, footnotesHTML));
lastFootnotes = currentFootnotes;
},
updateFootnotes = function() {
let currentFootnotesElement = fnEditor.getContent('dom'),
footnotes;
footnotes = [].slice.call(currentFootnotesElement.querySelectorAll('.footnote-container'));
footnotes.forEach((footnote, index) => {
if (footnote.innerHTML != lastFootnotes[index].attrs.fnContents) {
let oldFootnote = lastFootnotes[index];
console.log('detected change:' + footnote.innerHTML);
let replacement = oldFootnote.type.create({
fnContents: footnote.innerHTML
}, null, footnote.styles);
// The editor.doc may sometimes contain the same node several times.
// This happens after copying, for example. We therefore need to check
// how many times the same footnote node shows up before the current
// footnote.
let previousInstances = 0,
i;
for (i = 0; i < index; i++) {
if (lastFootnotes[i] === lastFootnotes[index]) {
previousInstances++;
}
}
let nodePos = getNodePos(editor.doc, oldFootnote, previousInstances);
lastFootnotes[index] = replacement;
editor.tr.replaceWith(nodePos.from, nodePos.to, replacement).apply();
}
});
},
makeEditor = function(where, schema) {
return new pm.ProseMirror({
place: where,
menuBar: {
float: true
},
schema: schema
})
};
where.innerHTML = '';
editor = makeEditor(where, pm.fidusSchema);
fnEditor = makeEditor(document.getElementById('footnote-editor'), pm.fidusFnSchema);
editor.setContent(doc);
renderFootnotes();
editor.on('change', function() {
renderFootnotes();
});
fnEditor.on('change', function() {
updateFootnotes();
});