forked from codex-team/editor.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaste.js
More file actions
225 lines (176 loc) · 5.29 KB
/
paste.js
File metadata and controls
225 lines (176 loc) · 5.29 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Paste plugin.
*
* Listens on paste event and pastes content from:
* - Instagram
* - Twitter
* - VK
* - Facebook
* - Image
* - External Link
*
*/
/**
* @protected
*
* Main tool settings.
*/
var pasteTool = {
};
/**
* Make elements to insert or switch
*
* @uses Core codex.draw module
*/
pasteTool.ui = {
/**
* Upload image by URL
*
* @uses codex Image tool
* @param filename
* @returns {Element}
*/
uploadedImage : function(filename) {
var data = {
background: false,
border: false,
isStretch: false,
file: {
url: "upload/redactor_images/" + filename,
bigUrl: "upload/redactor_images/" + filename,
width: null,
height: null,
additionalData: "null"
},
caption: '',
cover: null
};
/** Using Image plugin make method */
var image = codex.tools.image.make(data);
return image;
}
};
/**
*
* Callbacks
*/
pasteTool.callbacks = {
/**
* Saves data
* @param event
*/
pasted : function(event) {
var clipBoardData = event.clipboardData || window.clipboardData,
content = clipBoardData.getData('Text');
pasteTool.callbacks.analize(content);
},
/**
* Analizes pated string and calls necessary method
*/
analize : function(string) {
var regexTemplates = {
image : /(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*\.(?:jpe?g|gif|png))(?:\?([^#]*))?(?:#(.*))?/i,
instagram : new RegExp("http?.+instagram.com\/p?."),
twitter : new RegExp("http?.+twitter.com?.+\/"),
facebook : /https?.+facebook.+\/\d+\?/,
vk : /https?.+vk?.com\/feed\?w=wall\d+_\d+/,
},
image = regexTemplates.image.test(string),
instagram = regexTemplates.instagram.exec(string),
twitter = regexTemplates.twitter.exec(string),
facebook = regexTemplates.facebook.test(string),
vk = regexTemplates.vk.test(string);
if (image) {
pasteTool.callbacks.uploadImage(string);
} else if (instagram) {
pasteTool.callbacks.instagramMedia(instagram);
} else if (twitter) {
pasteTool.callbacks.twitterMedia(twitter);
} else if (facebook) {
pasteTool.callbacks.facebookMedia(string);
} else if (vk) {
pasteTool.callbacks.vkMedia(string);
}
},
/**
* Direct upload
* @param url
*/
uploadImage : function(path) {
var ajaxUrl = location.protocol + '//' + location.hostname + ':32769',
file,
image,
current = codex.content.currentNode,
beforeSend,
success_callback;
/** When image is uploaded to redactors folder */
success_callback = function(data) {
console.log(data);
return;
var file = JSON.parse(data);
image = pasteTool.ui.uploadedImage(file.filename);
codex.content.switchBlock(current, image, 'image');
};
/** Before sending XMLHTTP request */
beforeSend = function() {
var content = current.querySelector('.ce-block__content');
content.classList.add('ce-plugin-image__loader');
};
/** Preparing data for XMLHTTP */
var data = {
url: '/club/fetchImage',
type: "POST",
data : {
url: path
},
beforeSend : beforeSend,
success : success_callback
};
codex.core.ajax(data);
},
/**
* callback for instagram url's
* Using instagram Embed Widgete to render
* @uses Instagram tool
* @param url
*/
instagramMedia : function(url) {
var fullUrl = url.input,
data;
data = {
instagram_url: fullUrl
};
codex.tools.instagram.make(data, true);
},
/**
* callback for tweets
* Using Twittter Widget to render
* @uses Twitter tool
* @param url
*/
twitterMedia : function(url) {
var fullUrl = url.input,
tweetId,
arr,
data;
arr = fullUrl.split('/');
tweetId = arr.pop();
/** Example */
data = {
media:true,
conversation:false,
user:{
profile_image_url:"http:\/\/pbs.twimg.com\/profile_images\/1817165982\/nikita-likhachev-512_normal.jpg",
profile_image_url_https:"https:\/\/pbs.twimg.com\/profile_images\/1817165982\/nikita-likhachev-512_normal.jpg",
screen_name:"Niketas",
name:"Никита Лихачёв"
},
id: tweetId,
text:"ВНИМАНИЕ ЧИТАТЬ ВСЕМ НЕ ДАЙ БОГ ПРОПУСТИТЕ НУ ИЛИ ХОТЯ БЫ КЛИКНИ И ПОДОЖДИ 15 СЕКУНД https:\/\/t.co\/iWyOHf4xr2",
created_at:"Tue Jun 28 14:09:12 +0000 2016",
status_url:"https:\/\/twitter.com\/Niketas\/status\/747793978511101953",
caption:"Caption"
};
codex.tools.twitter.make(data);
}
};