Skip to content

Commit 4d1247b

Browse files
Break schema out into its own file; Add top-level attributes (#57)
1 parent a32c113 commit 4d1247b

File tree

3 files changed

+359
-127
lines changed

3 files changed

+359
-127
lines changed

src/collab.js

Lines changed: 2 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -12,132 +12,10 @@
1212
import {
1313
prosemirrorToYXmlFragment, yDocToProsemirror,
1414
} from 'y-prosemirror';
15-
import {
16-
DOMParser, DOMSerializer, Schema,
17-
} from 'prosemirror-model';
18-
import { addListNodes } from 'prosemirror-schema-list';
19-
import {
20-
tableNodes,
21-
} from 'prosemirror-tables';
22-
import { schema as baseSchema } from 'prosemirror-schema-basic';
15+
import { DOMParser, DOMSerializer } from 'prosemirror-model';
2316
import { fromHtml } from 'hast-util-from-html';
2417
import { matches } from 'hast-util-select';
25-
26-
function parseLocDOM(locTag) {
27-
return [{
28-
tag: locTag,
29-
30-
// Do we need to add this to the contentElement function?
31-
// Only parse the content of the node, not the temporary elements
32-
// const deleteThese = dom.querySelectorAll('[loc-temp-dom]');
33-
// deleteThese.forEach((e) => e.remove());
34-
contentElement: (dom) => dom,
35-
}];
36-
}
37-
38-
function addLocNodes(baseNodes) {
39-
if (!baseNodes.content.includes('loc_deleted')) {
40-
baseNodes.content.push('loc_deleted');
41-
baseNodes.content.push({
42-
group: 'block',
43-
content: 'block+',
44-
parseDOM: parseLocDOM('da-loc-deleted'),
45-
toDOM: () => ['da-loc-deleted', { contenteditable: false }, 0],
46-
});
47-
baseNodes.content.push('loc_added');
48-
baseNodes.content.push({
49-
group: 'block',
50-
content: 'block+',
51-
parseDOM: parseLocDOM('da-loc-added'),
52-
toDOM: () => ['da-loc-added', { contenteditable: false }, 0],
53-
});
54-
}
55-
return baseNodes;
56-
}
57-
58-
function addCustomMarks(marks) {
59-
const sup = {
60-
parseDOM: [{ tag: 'sup' }, { clearMark: (m) => m.type.name === 'sup' }],
61-
toDOM() { return ['sup', 0]; },
62-
};
63-
64-
const sub = {
65-
parseDOM: [{ tag: 'sub' }, { clearMark: (m) => m.type.name === 'sub' }],
66-
toDOM() { return ['sub', 0]; },
67-
};
68-
69-
const contextHighlight = { toDOM: () => ['span', { class: 'highlighted-context' }, 0] };
70-
71-
return marks
72-
.addToEnd('sup', sup)
73-
.addToEnd('sub', sub)
74-
.addToEnd('contextHighlightingMark', contextHighlight);
75-
}
76-
77-
function getImageNodeWithHref() {
78-
// due to bug in y-prosemirror, add href to image node
79-
// which will be converted to a wrapping <a> tag
80-
return {
81-
inline: true,
82-
attrs: {
83-
src: { validate: 'string' },
84-
alt: { default: null, validate: 'string|null' },
85-
title: { default: null, validate: 'string|null' },
86-
href: { default: null, validate: 'string|null' },
87-
},
88-
group: 'inline',
89-
draggable: true,
90-
parseDOM: [{
91-
tag: 'img[src]',
92-
getAttrs(dom) {
93-
return {
94-
src: dom.getAttribute('src'),
95-
title: dom.getAttribute('title'),
96-
alt: dom.getAttribute('alt'),
97-
href: dom.getAttribute('href'),
98-
};
99-
},
100-
}],
101-
toDOM(node) {
102-
const {
103-
src,
104-
alt,
105-
title,
106-
href,
107-
} = node.attrs;
108-
return ['img', {
109-
src,
110-
alt,
111-
title,
112-
href,
113-
}];
114-
},
115-
};
116-
}
117-
118-
function getTableNodeSchema() {
119-
const getTableAttrs = (dom) => ({
120-
dataId: dom.getAttribute('dataId') || null,
121-
});
122-
123-
const schema = tableNodes({ tableGroup: 'block', cellContent: 'block+' });
124-
schema.table.attrs = { dataId: { default: null } };
125-
schema.table.parseDOM = [{ tag: 'table', getAttrs: (dom) => getTableAttrs(dom) }];
126-
schema.table.toDOM = (node) => ['table', node.attrs, ['tbody', 0]];
127-
return schema;
128-
}
129-
130-
// Note: until getSchema() is separated in its own module, this function needs to be kept in-sync
131-
// with the getSchema() function in da-live blocks/edit/prose/index.js
132-
function getSchema() {
133-
const { marks, nodes: baseNodes } = baseSchema.spec;
134-
const withLocNodes = addLocNodes(baseNodes);
135-
const withListnodes = addListNodes(withLocNodes, 'block+', 'block');
136-
const withTableNodes = withListnodes.append(getTableNodeSchema());
137-
const nodes = withTableNodes.update('image', getImageNodeWithHref());
138-
const customMarks = addCustomMarks(marks);
139-
return new Schema({ nodes, marks: customMarks });
140-
}
18+
import { getSchema } from './schema.js';
14119

14220
function convertSectionBreak(node) {
14321
if (!node) return;

0 commit comments

Comments
 (0)