This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
forked from MaxArt2501/share-this
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
152 lines (122 loc) · 4.42 KB
/
core.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
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
import { stylePopover, lifeCycleFactory } from "./popover.js";
import { constrainRange } from "./selection.js";
import { extend, isCallable } from "./utils.js";
import render from "./render.js";
let _undefined;
const eventTypes = [ "selectionchange", "mouseup", "touchend", "touchcancel" ];
export default (opts) => {
const options = (Object.assign || extend)({
document,
selector: "body",
sharers: [],
popoverClass: "share-this-popover",
transformer: raw => raw.trim().replace(/\s+/g, " ")
}, opts || {});
let initialized = false;
let destroyed = false;
let _document = _undefined;
let _window = _undefined;
let popover = _undefined;
let lifeCycle = _undefined;
return {
init() {
if (initialized) return false;
_document = options.document;
_window = _document.defaultView;
if (!_window.getSelection) {
// eslint-disable-next-line no-console
console.warn("share-this: Selection API isn't supported");
return false;
}
eventTypes.forEach(addListener);
_window.addEventListener("resize", resizeHandler);
lifeCycle = lifeCycleFactory(_document);
return initialized = true;
},
destroy() {
if (!initialized || destroyed) return false;
eventTypes.forEach(removeListener);
_window.removeEventListener("resize", resizeHandler);
killPopover();
_document = _undefined;
_window = _undefined;
return destroyed = true;
},
reposition() {
if (popover) {
stylePopover(popover, getConstrainedRange(), options);
}
return !!popover;
},
drawPopover(range) {
drawPopover(constrainRange(range, options.selector));
},
killPopover,
};
function addListener(type) { _document.addEventListener(type, selectionCheck); }
function removeListener(type) { _document.removeEventListener(type, selectionCheck); }
function resizeHandler() {
if (popover) {
stylePopover(popover, getConstrainedRange(), options);
}
}
function selectionCheck({ type }) {
const shouldHavePopover = type === "selectionchange";
if (!popover !== shouldHavePopover) {
// Safari iOS fires selectionchange *before* click, so tapping on a sharer would be prevented
setTimeout(() => {
if (_window) {
const range = getConstrainedRange();
if (range) drawPopover(range);
else killPopover();
}
}, 10);
}
}
function getConstrainedRange() {
const selection = _window.getSelection();
const range = selection.rangeCount && selection.getRangeAt(0);
if (!range) return;
const constrainedRange = constrainRange(range, options.selector);
if (constrainedRange.collapsed || !constrainedRange.getClientRects().length) return;
// eslint-disable-next-line consistent-return
return constrainedRange;
}
function drawPopover(range) {
const toBeOpened = !popover;
const rawText = range.toString();
const text = options.transformer(rawText);
const sharers = options.sharers.filter(sharerCheck.bind(null, text, rawText));
if (!sharers.length) {
if (popover) killPopover();
return;
}
if (toBeOpened) popover = lifeCycle.createPopover();
popover.sharers = sharers;
popover.innerHTML = render(options, sharers, text, rawText);
stylePopover(popover, range, options);
if (!toBeOpened) return;
lifeCycle.attachPopover(popover);
if (isCallable(options.onOpen)) {
options.onOpen(popover, text, rawText);
}
}
function killPopover() {
if (!popover) return;
lifeCycle.removePopover(popover);
popover = _undefined;
if (isCallable(options.onClose)) {
options.onClose();
}
}
function sharerCheck(text, rawText, sharer) {
const active = sharer.active;
if (isCallable(active)) {
return active(text, rawText);
}
if (active !== _undefined) {
return active;
}
return true;
}
};