-
Notifications
You must be signed in to change notification settings - Fork 7
/
share-url.js
52 lines (44 loc) · 1.25 KB
/
share-url.js
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
import uuid from 'uuid'
import {isUrl} from '../util/url'
function testPrevUrl (state) {
const {selection, doc} = state
// Entered into a new block, collapsed selection
if (!selection.empty) return
// Current position (under potential url line)
const currentNode = doc.childBefore(selection.anchor)
if (!currentNode || currentNode.index < 1) return
// Potential url line
const index = currentNode.index - 1
const prevNode = doc.maybeChild(index)
if (!prevNode || prevNode.type.name !== 'paragraph') return
// Test if url
const url = prevNode.textContent.trim()
if (!url || !isUrl(url)) return
// Make share
const id = uuid.v4()
const block =
{ id,
type: 'placeholder',
metadata:
{ status: `Sharing... ${url}`,
percent: 0,
},
}
setTimeout(() => {
// HACK timeout needed for store to do transform
// Transform _should_ be in plugin applyAction
this.options.edStuff.ed.routeChange('PLUGIN_URL', {index, id, block, url})
}, 0)
}
export default {
state: {
init: function () {
this.testPrevUrl = testPrevUrl.bind(this)
},
apply: function (transaction, value, prev, state) {
if (transaction.steps.length) {
this.testPrevUrl(state)
}
},
},
}