-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
plugin-ui.js
194 lines (169 loc) · 3.81 KB
/
plugin-ui.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
190
191
192
193
194
'use strict';
import React from 'react';
import ReactDOM from 'react-dom';
import Iconify from '@iconify/iconify';
import style from './src/css/style.scss';
import UI from './src/ui';
// Items per page
const itemsPerPage = 55; // 5 rows x 11 icons. Also see dev-ui.js
/**
* Send message to plugin
*
* @param {string} event
* @param {object} [data]
*/
function sendMessage(event, data) {
if (process.env.SEARCH_DEV) {
console.log('Message from UI', event, data);
}
parent.postMessage(
{
pluginMessage: {
event: event,
data: data,
},
},
'*'
);
}
/**
* Load script
*
* @param {string} uri
*/
function loadScript(uri) {
let script = document.createElement('script');
script.setAttribute('async', true);
script.setAttribute('src', uri);
document.head.appendChild(script);
}
/**
* Call done() after test() is successful
*
* @param {function} test
* @param {function} done
*/
function delay(test, done) {
let counter = 0,
timer,
result;
function nextTick() {
result = test();
if (result) {
// Success
window.clearInterval(timer);
done();
return;
}
if (result === null) {
// Stop execution
window.clearInterval(timer);
return;
}
// Test failed
counter++;
if (counter === 10 || counter === 25) {
// It takes too long. Reduce timeout
window.clearInterval(timer);
timer = window.setInterval(nextTick, counter === 10 ? 250 : 1000);
}
}
// Do first test immediately
result = test(counter);
if (result) {
done();
return;
}
if (result === null) {
return;
}
// Create timer
timer = window.setInterval(nextTick, 100);
}
// Show notice that plug-in is in development mode, not ready for publishing
if (process.env.SEARCH_DEV) {
console.log('Running plug-in in development mode!');
}
// Disable Iconify storage
Iconify.setConfig('localStorage', false);
Iconify.setConfig('sessionStorage', false);
if (process.env.ICONIFY_API) {
// Use local API for development. See config.common.js
Iconify.setConfig('defaultAPI', process.env.ICONIFY_API_VALUE);
}
// Load samples
loadScript('https://code.iconify.design/samples.js');
delay(
counter => {
// Do not delay for more than 1 second
return counter > 10 || Iconify.iconExists('mdi:account-check');
},
() => {
let iconifyConfig = {
config: {
itemsPerPage: itemsPerPage,
search: {
limit: itemsPerPage * 2,
},
},
};
if (process.env.SEARCH_API) {
// Use local API for development. See config.common.js
iconifyConfig.config.API = {
URI: process.env.SEARCH_API_VALUE,
};
}
// UI instance
let ui = null;
window.onmessage = event => {
let message = event.data.pluginMessage;
if (typeof message !== 'object' || !message.event) {
return;
}
if (process.env.SEARCH_DEV) {
console.log('Sent message to UI:', message);
}
switch (message.event) {
case 'show':
// Show UI
let params = {
iconify: iconifyConfig,
stored: message.config,
parentNodes: message.parentNodes,
callback: sendMessage,
// prefix: 'mdi', // Uncomment this to limit plug-in to one collection
};
ui = new UI(document.getElementById('container'), params);
if (message.selectedNode) {
ui.showCode(message.selectedNode);
}
break;
case 'selected-nodes':
// Update list of possible parent nodes
ui.setSelectedNodes(message.nodes);
break;
case 'selected-node-data':
ui.showCode(message.selectedNode);
break;
case 'cancel-node-data':
ui.hideCode();
break;
case 'error':
case 'notice':
case 'success':
ui.addNotice(
message.message,
Object.assign(
{
type: message.event,
},
message
)
);
break;
}
};
// Notify plugin that UI has finished loading
sendMessage('loaded', process.env.SEARCH_DEV);
}
);