forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipboard.js
More file actions
75 lines (67 loc) · 2.32 KB
/
clipboard.js
File metadata and controls
75 lines (67 loc) · 2.32 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
class Clipboard {
constructor (muya) {
this.muya = muya
this._copyType = 'normal' // `normal` or `copyAsMarkdown` or `copyAsHtml`
this._pasteType = 'normal' // `normal` or `pasteAsPlainText`
this._copyInfo = null
this.listen()
}
listen () {
const { container, eventCenter, contentState } = this.muya
const docPasteHandler = event => {
contentState.docPasteHandler(event)
}
const docCopyCutHandler = event => {
contentState.docCopyHandler(event)
if (event.type === 'cut') {
// when user use `cut` function, the dom has been deleted by default.
// But should update content state manually.
contentState.docCutHandler(event)
}
}
const copyCutHandler = event => {
contentState.copyHandler(event, this._copyType, this._copyInfo)
if (event.type === 'cut') {
// when user use `cut` function, the dom has been deleted by default.
// But should update content state manually.
contentState.cutHandler()
}
this._copyType = 'normal'
}
const pasteHandler = event => {
contentState.pasteHandler(event, this._pasteType)
this._pasteType = 'normal'
this.muya.dispatchChange()
}
eventCenter.attachDOMEvent(document, 'paste', docPasteHandler)
eventCenter.attachDOMEvent(container, 'paste', pasteHandler)
eventCenter.attachDOMEvent(container, 'cut', copyCutHandler)
eventCenter.attachDOMEvent(container, 'copy', copyCutHandler)
eventCenter.attachDOMEvent(document.body, 'cut', docCopyCutHandler)
eventCenter.attachDOMEvent(document.body, 'copy', docCopyCutHandler)
}
// TODO: `document.execCommand` is deprecated!
copyAsMarkdown () {
this._copyType = 'copyAsMarkdown'
document.execCommand('copy')
}
copyAsHtml () {
this._copyType = 'copyAsHtml'
document.execCommand('copy')
}
pasteAsPlainText () {
this._pasteType = 'pasteAsPlainText'
document.execCommand('paste')
}
/**
* Copy the anchor block(table, paragraph, math block etc) with the info
* @param {string|object} type copyBlock or copyCodeContent
* @param {string|object} info is the block key if it's string, or block if it's object
*/
copy (type, info) {
this._copyType = type
this._copyInfo = info
document.execCommand('copy')
}
}
export default Clipboard