-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic-text.js
More file actions
102 lines (89 loc) · 2.88 KB
/
Copy pathdynamic-text.js
File metadata and controls
102 lines (89 loc) · 2.88 KB
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
/*
* a default schema that we use for the editor
*/
import {Inline, Attribute} from 'prosemirror/dist/model';
import DynamicTextPlugin from './markdown-it-dynamic';
import ExecutionEnvironment from 'exenv';
let Dropdown = function(){}
let MenuCommandGroup = function(){}
let elt = function(){};
let InputRule = function(){};
if (ExecutionEnvironment.canUseDOM) {
({Dropdown, MenuCommandGroup} = require('prosemirror/dist/menu/menu'));
({elt} = require('prosemirror/dist/dom'));
({InputRule} = require('prosemirror/dist/inputrules'));
}
export class DynamicText extends Inline {
// Not sure what this is for other than tagging
get attrs() {
return {
type: new Attribute('dynamic')
}
}
}
DynamicText.prototype.serializeDOM = (node) => {
let newNode = elt('span',
{
'dynamic-label': node.attrs.type, // is this passed in by the command?
'class': 'prosemirror-dynamic-label'
},
`<${node.attrs.type}>`);
return newNode;
}
DynamicText.register('parseDOM', 'span', {
rank: 35,
parse: function(dom, state) {
let label = dom.getAttribute('dynamic-label');
if (!label) { return false; }
state.insert(this, {label});
}
})
DynamicText.prototype.serializeMarkdown = (state, node) => {
state.write(`<${node.attrs.type}>`)
};
DynamicText.register('configureMarkdown', 'dynamic', parser => {
return parser.use(DynamicTextPlugin);
})
// this works
DynamicText.register('parseMarkdown', 'dynamic', {parse: function(state, tok) {
state.addNode(this, {type: tok.content});
}})
// TODO(marcos): find out how to get the parse: 'block' behavior for inline nodes
DynamicText.register('parseMarkdown', 'dynamic_open', { parse: function(state, tok) { }});
DynamicText.register('parseMarkdown', 'dynamic_close', { parse: function(state, tok) { }});
export const dynamicMenu = new Dropdown({label: 'Insert dynamic field'}, new MenuCommandGroup('dynamic'))
// this has the effect of modifying subsequent DynamicText modules
export function dynamicTextWithLabels(labels) {
labels.forEach((label, idx) => {
DynamicText.register('command', 'insert'+label, {
run(pm) {
const field = this.create({type: label});
let {from, to, node} = pm.selection
let side = pm.doc.resolve(from).parentOffset ? to : from
pm.tr.insert(side, field).apply(pm.apply.scroll)
pm.setTextSelection(side + 1)
},
menu: {
group: 'dynamic',
rank: (idx + 1) * 10,
display: {
type: 'label',
label
}
}
})
})
let rule = `{(${labels.join('|')})}$`;
DynamicText.register("autoInput", "autoDynamic",
new InputRule(
new RegExp(rule),
'}',
function(pm, match, pos) {
let start = pos - match[0].length;
let field = this.create({type: match[1]});
pm.tr.delete(start, pos).insertInline(start, field).apply();
}
)
)
return DynamicText;
}