forked from leopardpan/leopardpan.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.js
189 lines (170 loc) · 6.41 KB
/
embed.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.embed = factory());
}(this, (function () { 'use strict';
var justExtend = extend;
function extend() {
var args = [].slice.call(arguments);
var deep = false;
if (typeof args[0] == 'boolean') {
deep = args.shift();
}
var result = args[0];
if (!result || typeof result != 'object' && typeof result != 'function') {
throw new Error('extendee must be an object');
}
var extenders = args.slice(1);
var len = extenders.length;
for (var i = 0;i < len; i++) {
var extender = extenders[i];
for (var key in extender) {
if (extender.hasOwnProperty(key)) {
var value = extender[key];
if (deep && isCloneable(value)) {
var base = Array.isArray(value) ? [] : {};
result[key] = extend(true, result.hasOwnProperty(key) ? result[key] : base, value);
} else {
result[key] = value;
}
}
}
}
return result;
}
function isCloneable(obj) {
return Array.isArray(obj) || ({}).toString.call(obj) == '[object Object]';
}
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var pReduce = createCommonjsModule(function (module) {
'use strict';
module.exports = (function (iterable, reducer, initVal) { return new Promise(function (resolve, reject) {
var iterator = iterable[Symbol.iterator]();
var i = 0;
var next = function (total) {
var el = iterator.next();
if (el.done) {
resolve(total);
return;
}
Promise.all([total,el.value]).then(function (value) {
next(reducer(value[0], value[1], i++));
}).catch(reject);
};
next(initVal);
}); });
});
var pWaterfall = function (iterable, initVal) { return pReduce(iterable, function (prev, fn) { return fn(prev); }, initVal); };
var isDom = isNode;
function isNode(val) {
return !val || typeof val !== 'object' ? false : typeof window === 'object' && typeof window.Node === 'object' ? val instanceof window.Node : typeof val.nodeType === 'number' && typeof val.nodeName === 'string';
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var isBrowser = (typeof window === "undefined" ? "undefined" : _typeof(window)) === "object" && (typeof document === "undefined" ? "undefined" : _typeof(document)) === 'object' && document.nodeType === 9;
function combineEmbedsText(embeds) {
return embeds.sort(function (a, b) { return a.index - b.index; }).map(function (ref) {
var content = ref.content;
return content;
}).join(" ");
}
function appendEmbedsAtEnd(ref) {
var result = ref.result;
var _embeds = ref._embeds;
return (result + " " + (combineEmbedsText(_embeds)));
}
function isElementPresent(ref) {
var input = ref.input;
var target = ref.target;
return isDom(input) || target && isDom(target);
}
var EmbedJS = function EmbedJS(options) {
var defaultOptions = {
plugins: [],
preset: null,
fetch: isBrowser && (window.fetch || window.unfetch),
inlineEmbed: true,
replaceUrl: false,
_embeds: [],
_services: []
};
var input = options.input;
var plugins = options.plugins; if ( plugins === void 0 ) plugins = [];
var preset = options.preset;
if (!input) {
throw new Error("You need to pass input element or string in the options object.");
}
var inputString = isDom(input) ? input.innerHTML : input;
this.options = justExtend({}, defaultOptions, options, {
result: inputString,
plugins: preset ? plugins.concat(preset) : plugins,
inputString: inputString
});
};
EmbedJS.prototype.text = function text () {
var options = this.resetOptions();
var transformers = options.plugins.map(function (p) { return p.transform; });
return pWaterfall(transformers, options);
};
EmbedJS.prototype.resetOptions = function resetOptions () {
return justExtend({}, this.options, {
_embeds: []
});
};
EmbedJS.prototype.load = function load () {
var this$1 = this;
this.options.plugins.forEach(function (p) { return p.onLoad && p.onLoad(this$1.options); });
};
EmbedJS.prototype.render = function render () {
var this$1 = this;
return new Promise(function ($return, $error) {
var input, target, inlineEmbed, element;
var options;
var assign;
((assign = this$1.options, input = assign.input, target = assign.target, inlineEmbed = assign.inlineEmbed));
if (!isElementPresent(this$1.options)) {
return $error(new Error("You haven't passed the input as an element."));
}
if (isDom(input) && input.classList.contains("ejs-applied")) {
options = this$1.options;
return $If_1.call(this$1);
} else {
return this$1.text().then(function ($await_2) {
try {
options = $await_2;
element = target || input;
element.innerHTML = inlineEmbed ? options.result : appendEmbedsAtEnd(options);
element.classList.add("ejs-applied");
return $If_1.call(this$1);
} catch ($boundEx) {
return $error($boundEx);
}
}, $error);
}
function $If_1() {
this.load();
return $return(options);
}
});
};
EmbedJS.prototype.destroy = function destroy () {
var ref = this.options;
var inputString = ref.inputString;
var input = ref.input;
var target = ref.target;
if (!isElementPresent(this.options)) {
throw new Error("You haven't passed the input as an element.");
}
var element = target || input;
element.innerHTML = inputString;
element.classList.remove("ejs-applied");
return this.options;
};
return EmbedJS;
})));
//# sourceMappingURL=embed.js.map