forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
148 lines (126 loc) · 3.81 KB
/
index.js
File metadata and controls
148 lines (126 loc) · 3.81 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
136
137
138
139
140
141
142
143
144
145
146
147
148
import { filter } from 'fuzzaldrin'
import 'codemirror/addon/edit/closebrackets'
import 'codemirror/addon/edit/closetag'
import 'codemirror/addon/selection/active-line'
import 'codemirror/mode/meta'
import codeMirror from 'codemirror/lib/codemirror'
import loadmode from './loadmode'
import overlayMode from './overlayMode'
import multiplexMode from './mltiplexMode'
import languages from './modes'
import 'codemirror/lib/codemirror.css'
import './index.css'
import 'codemirror/theme/railscasts.css'
loadmode(codeMirror)
overlayMode(codeMirror)
multiplexMode(codeMirror)
window.CodeMirror = codeMirror
const modes = codeMirror.modeInfo
codeMirror.modeURL = './codemirror/mode/%N/%N.js'
const getModeFromName = name => {
let result = null
const lang = languages.filter(lang => lang.name === name)[0]
if (lang) {
const { name, mode, mime } = lang
const matched = modes.filter(m => {
if (m.mime) {
if (Array.isArray(m.mime) && m.mime.indexOf(mime) > -1 && m.mode === mode) {
return true
} else if (typeof m.mime === 'string' && m.mime === mime && m.mode === mode) {
return true
}
}
if (Array.isArray(m.mimes) && m.mimes.indexOf(mime) > -1 && m.mode === mode) {
return true
}
return false
})
if (matched.length && typeof matched[0] === 'object') {
result = {
name,
mode: matched[0]
}
}
}
return result
}
export const search = text => {
const matchedLangs = filter(languages, text, { key: 'name' })
return matchedLangs
.map(({ name }) => getModeFromName(name))
.filter(lang => !!lang)
}
/**
* set cursor at the end of last line.
*/
export const setCursorAtLastLine = cm => {
const lastLine = cm.lastLine()
const lineHandle = cm.getLineHandle(lastLine)
cm.focus()
cm.setCursor(lastLine, lineHandle.text.length)
}
// if cursor at firstLine return true
export const isCursorAtFirstLine = cm => {
const cursor = cm.getCursor()
const { line, ch, outside } = cursor
return line === 0 && ch === 0 && outside
}
export const isCursorAtLastLine = cm => {
const lastLine = cm.lastLine()
const cursor = cm.getCursor()
const { line, outside, sticky } = cursor
return line === lastLine && (outside || !sticky)
}
export const isCursorAtBegin = cm => {
const cursor = cm.getCursor()
const { line, ch, hitSide } = cursor
return line === 0 && ch === 0 && !!hitSide
}
export const onlyHaveOneLine = cm => {
return cm.lineCount() === 1
}
export const isCursorAtEnd = cm => {
const lastLine = cm.lastLine()
const lastLineHandle = cm.getLineHandle(lastLine)
const cursor = cm.getCursor()
const { line, ch, hitSide } = cursor
return line === lastLine && ch === lastLineHandle.text.length && !!hitSide
}
export const getBeginPosition = () => {
return {
anchor: { line: 0, ch: 0 },
head: { line: 0, ch: 0 }
}
}
export const getEndPosition = cm => {
const lastLine = cm.lastLine()
const lastLineHandle = cm.getLineHandle(lastLine)
const line = lastLine
const ch = lastLineHandle.text.length
return { anchor: { line, ch }, head: { line, ch } }
}
export const setCursorAtFirstLine = cm => {
cm.focus()
cm.setCursor(0, 0)
}
export const setMode = (doc, text) => {
const m = getModeFromName(text)
if (!m) {
const errMsg = !text
? 'You\'d better provided a language mode when you create code block'
: `${text} is not a valid language mode!`
return Promise.reject(errMsg) // eslint-disable-line prefer-promise-reject-errors
}
const { mode, mime } = m.mode
return new Promise(resolve => {
codeMirror.requireMode(mode, () => {
doc.setOption('mode', mime || mode)
codeMirror.autoLoadMode(doc, mode)
resolve(m)
})
})
}
export const setTextDirection = (cm, textDirection) => {
cm.setOption('direction', textDirection)
}
export default codeMirror