forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp.js
More file actions
162 lines (145 loc) · 3.86 KB
/
help.js
File metadata and controls
162 lines (145 loc) · 3.86 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { getUniqueId, cloneObj } from '../util'
/**
* Default internel markdown document with editor options.
*
* @type {IDocumentState} Internel markdown document
*/
export const defaultFileState = {
// Indicates whether there are unsaved changes.
isSaved: true,
// Full path to the file or empty. If the value is empty the file doesn't exist on disk.
pathname: '',
filename: 'Untitled-1',
markdown: '',
encoding: {
encoding: 'utf8',
isBom: false
},
lineEnding: 'lf', // lf or crlf
trimTrailingNewline: 3,
adjustLineEndingOnSave: false, // convert editor buffer (LF) to CRLF when saving
history: {
stack: [],
index: -1
},
cursor: null,
wordCount: {
paragraph: 0,
word: 0,
character: 0,
all: 0
},
searchMatches: {
index: -1,
matches: [],
value: ''
},
// Per tab notifications
notifications: []
}
export const getOptionsFromState = file => {
const { encoding, lineEnding, adjustLineEndingOnSave, trimTrailingNewline } = file
return { encoding, lineEnding, adjustLineEndingOnSave, trimTrailingNewline }
}
export const getFileStateFromData = data => {
const fileState = JSON.parse(JSON.stringify(defaultFileState))
const {
markdown,
filename,
pathname,
encoding,
lineEnding,
adjustLineEndingOnSave,
trimTrailingNewline
} = data
const id = getUniqueId()
assertLineEnding(adjustLineEndingOnSave, lineEnding)
return Object.assign(fileState, {
id,
markdown,
filename,
pathname,
encoding,
lineEnding,
adjustLineEndingOnSave,
trimTrailingNewline
})
}
export const getBlankFileState = (tabs, defaultEncoding = 'utf8', lineEnding = 'lf', markdown = '') => {
const fileState = cloneObj(defaultFileState, true)
let untitleId = Math.max(...tabs.map(f => {
if (f.pathname === '') {
return +f.filename.split('-')[1]
} else {
return 0
}
}), 0)
const id = getUniqueId()
// We may pass markdown=null as parameter.
if (markdown == null) {
markdown = ''
}
fileState.encoding.encoding = defaultEncoding
return Object.assign(fileState, {
lineEnding,
adjustLineEndingOnSave: lineEnding.toLowerCase() === 'crlf',
id,
filename: `Untitled-${++untitleId}`,
markdown
})
}
export const getSingleFileState = ({ id = getUniqueId(), markdown, filename, pathname, options }) => {
// TODO(refactor:renderer/editor): Replace this function with `createDocumentState`.
const fileState = cloneObj(defaultFileState, true)
const { encoding, lineEnding, adjustLineEndingOnSave, trimTrailingNewline } = options
assertLineEnding(adjustLineEndingOnSave, lineEnding)
return Object.assign(fileState, {
id,
markdown,
filename,
pathname,
encoding,
lineEnding,
adjustLineEndingOnSave,
trimTrailingNewline
})
}
/**
* Creates a internal document from the given document.
*
* @param {IMarkdownDocument} markdownDocument Markdown document
* @param {String} [id] Random identifier
* @returns {IDocumentState} Returns a document state
*/
export const createDocumentState = (markdownDocument, id = getUniqueId()) => {
const docState = cloneObj(defaultFileState, true)
const {
markdown,
filename,
pathname,
encoding,
lineEnding,
adjustLineEndingOnSave,
trimTrailingNewline,
cursor = null
} = markdownDocument
assertLineEnding(adjustLineEndingOnSave, lineEnding)
return Object.assign(docState, {
id,
markdown,
filename,
pathname,
encoding,
lineEnding,
cursor,
adjustLineEndingOnSave,
trimTrailingNewline
})
}
const assertLineEnding = (adjustLineEndingOnSave, lineEnding) => {
lineEnding = lineEnding.toLowerCase()
if ((adjustLineEndingOnSave && lineEnding !== 'crlf') ||
(!adjustLineEndingOnSave && lineEnding === 'crlf')) {
console.error('Assertion failed: Line ending is "CRLF" but document is saved as "LF".')
}
}