forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainerCtrl.js
More file actions
135 lines (115 loc) · 3.7 KB
/
containerCtrl.js
File metadata and controls
135 lines (115 loc) · 3.7 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
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
131
132
133
134
135
const FUNCTION_TYPE_LANG = {
multiplemath: 'latex',
flowchart: 'yaml',
mermaid: 'yaml',
sequence: 'yaml',
plantuml: 'yaml',
'vega-lite': 'yaml',
html: 'markup'
}
const containerCtrl = ContentState => {
ContentState.prototype.createContainerBlock = function (functionType, value = '', style = undefined) {
const figureBlock = this.createBlock('figure', {
functionType
})
if (functionType === 'multiplemath') {
if (style === undefined) {
figureBlock.mathStyle = this.isGitlabCompatibilityEnabled ? 'gitlab' : ''
}
figureBlock.mathStyle = style
}
const { preBlock, preview } = this.createPreAndPreview(functionType, value)
this.appendChild(figureBlock, preBlock)
this.appendChild(figureBlock, preview)
return figureBlock
}
ContentState.prototype.createPreAndPreview = function (functionType, value = '') {
const lang = FUNCTION_TYPE_LANG[functionType]
const preBlock = this.createBlock('pre', {
functionType,
lang
})
const codeBlock = this.createBlock('code', {
lang
})
this.appendChild(preBlock, codeBlock)
if (typeof value === 'string' && value) {
value = value.replace(/^\s+/, '')
const codeContent = this.createBlock('span', {
text: value,
lang,
functionType: 'codeContent'
})
this.appendChild(codeBlock, codeContent)
} else {
const emptyCodeContent = this.createBlock('span', {
functionType: 'codeContent',
lang
})
this.appendChild(codeBlock, emptyCodeContent)
}
const preview = this.createBlock('div', {
editable: false,
functionType
})
return { preBlock, preview }
}
ContentState.prototype.initContainerBlock = function (functionType, block, style = undefined) { // p block
block.type = 'figure'
block.functionType = functionType
block.children = []
if (functionType === 'multiplemath') {
if (style === undefined) {
block.mathStyle = this.isGitlabCompatibilityEnabled ? 'gitlab' : ''
}
block.mathStyle = style
}
const { preBlock, preview } = this.createPreAndPreview(functionType)
this.appendChild(block, preBlock)
this.appendChild(block, preview)
return preBlock.children[0].children[0]
}
ContentState.prototype.handleContainerBlockClick = function (figureEle) {
const { id } = figureEle
const mathBlock = this.getBlock(id)
const preBlock = mathBlock.children[0]
const firstLine = preBlock.children[0].children[0]
const { key } = firstLine
const offset = 0
this.cursor = {
start: { key, offset },
end: { key, offset }
}
this.partialRender()
}
ContentState.prototype.updateMathBlock = function (block) {
const functionType = 'multiplemath'
const { type } = block
// TODO(GitLab): Allow "functionType" 'languageInput' to convert an existing
// code block into math block.
if (type === 'span' && block.functionType === 'paragraphContent') {
const isMathBlock = !!block.text.match(/^`{3,}math\s*/)
if (isMathBlock) {
const result = this.initContainerBlock(functionType, block, 'gitlab')
if (result) {
// Set cursor at the first line
const { key } = result
const offset = 0
this.cursor = {
start: { key, offset },
end: { key, offset }
}
// Force render
this.partialRender()
return result
}
}
return false
} else if (type !== 'p') {
return false
}
const { text } = block.children[0]
return text.trim() === '$$' ? this.initContainerBlock(functionType, block, '') : false
}
}
export default containerCtrl