poi-plugin-shortcuts 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +29 -0
- package/core.js +42 -0
- package/index.js +6 -0
- package/package.json +17 -0
- package/runtime.js +250 -0
- package/test.js +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 84265cc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# poi-plugin-shortcuts
|
|
2
|
+
|
|
3
|
+
Icon-only favorite plugin shortcuts for poi.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Adds plugin shortcuts between poi's message area and map reminder.
|
|
8
|
+
- Uses each plugin's existing icon; shortcuts contain icons only.
|
|
9
|
+
- Hover a plugin item in the plugin grid to reveal its favorite star.
|
|
10
|
+
- Favorited plugins appear in the order they were added.
|
|
11
|
+
- Unfavoriting and favoriting again moves a plugin to the end.
|
|
12
|
+
- Clicking a shortcut uses poi's existing plugin focus/open handler.
|
|
13
|
+
- Uses Blueprint class names from the installed poi version, supporting poi 11 and 12.
|
|
14
|
+
|
|
15
|
+
Favorites are stored at `plugin.poi-plugin-shortcuts.favorites` in poi's configuration.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Copy this directory to poi's extra plugin directory as `poi-plugin-shortcuts`, then restart poi or reload plugins.
|
|
20
|
+
|
|
21
|
+
## Test
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm test
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## License
|
|
28
|
+
|
|
29
|
+
MIT
|
package/core.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const PLUGIN_ID = 'poi-plugin-shortcuts'
|
|
4
|
+
const FAVORITES_PATH = 'plugin.poi-plugin-shortcuts.favorites'
|
|
5
|
+
|
|
6
|
+
const normalizeFavorites = value => {
|
|
7
|
+
if (!Array.isArray(value)) return []
|
|
8
|
+
const seen = new Set()
|
|
9
|
+
return value.filter(id => {
|
|
10
|
+
if (typeof id !== 'string' || !id || id === PLUGIN_ID || seen.has(id)) return false
|
|
11
|
+
seen.add(id)
|
|
12
|
+
return true
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const toggleFavorite = (favorites, pluginId) => {
|
|
17
|
+
const normalized = normalizeFavorites(favorites)
|
|
18
|
+
if (typeof pluginId !== 'string' || !pluginId || pluginId === PLUGIN_ID) return normalized
|
|
19
|
+
if (normalized.includes(pluginId)) return normalized.filter(id => id !== pluginId)
|
|
20
|
+
return [...normalized, pluginId]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const isListedPlugin = plugin => Boolean(
|
|
24
|
+
plugin && plugin.id !== PLUGIN_ID && plugin.enabled &&
|
|
25
|
+
(plugin.handleClick || plugin.windowURL || plugin.reactClass),
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const getListedPlugins = plugins => (Array.isArray(plugins) ? plugins : []).filter(isListedPlugin)
|
|
29
|
+
|
|
30
|
+
const getFavoritePlugins = (plugins, favorites) => {
|
|
31
|
+
const byId = new Map(getListedPlugins(plugins).map(plugin => [plugin.id, plugin]))
|
|
32
|
+
return normalizeFavorites(favorites).map(id => byId.get(id)).filter(Boolean)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
FAVORITES_PATH,
|
|
37
|
+
PLUGIN_ID,
|
|
38
|
+
getFavoritePlugins,
|
|
39
|
+
getListedPlugins,
|
|
40
|
+
normalizeFavorites,
|
|
41
|
+
toggleFavorite,
|
|
42
|
+
}
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "poi-plugin-shortcuts",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Favorite poi plugins and open them from an icon-only shortcut bar.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test.js"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"poiPlugin": {
|
|
11
|
+
"title": "插件快捷列",
|
|
12
|
+
"description": "收藏插件並在訊息列右側顯示圖示捷徑",
|
|
13
|
+
"icon": "fa/star",
|
|
14
|
+
"priority": 1
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
package/runtime.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const React = require('react')
|
|
4
|
+
const ReactDOM = require('react-dom')
|
|
5
|
+
const Blueprint = require('@blueprintjs/core')
|
|
6
|
+
const remote = require('@electron/remote')
|
|
7
|
+
const ipc = remote.require('./lib/ipc')
|
|
8
|
+
const {
|
|
9
|
+
FAVORITES_PATH,
|
|
10
|
+
getFavoritePlugins,
|
|
11
|
+
getListedPlugins,
|
|
12
|
+
normalizeFavorites,
|
|
13
|
+
toggleFavorite,
|
|
14
|
+
} = require('./core')
|
|
15
|
+
|
|
16
|
+
const STYLE_ID = 'poi-plugin-shortcuts-style'
|
|
17
|
+
const ROOT_CLASS = 'poi-plugin-shortcuts-root'
|
|
18
|
+
const STAR_CLASS = 'poi-plugin-shortcuts-star'
|
|
19
|
+
const HOST_CLASS = 'poi-plugin-shortcuts-star-host'
|
|
20
|
+
const RECONCILE_INTERVAL_MS = 1000
|
|
21
|
+
const BUTTON_CLASS = Blueprint.Classes.BUTTON
|
|
22
|
+
const MINIMAL_CLASS = Blueprint.Classes.MINIMAL
|
|
23
|
+
const MENU_CLASS = Blueprint.Classes.MENU
|
|
24
|
+
const MENU_ITEM_CLASS = Blueprint.Classes.MENU_ITEM
|
|
25
|
+
|
|
26
|
+
let observer = null
|
|
27
|
+
let reconcileTimer = null
|
|
28
|
+
let reconcileScheduled = false
|
|
29
|
+
|
|
30
|
+
const getPlugins = () => getListedPlugins(window.getStore('plugins'))
|
|
31
|
+
const getFavorites = () => normalizeFavorites(window.config.get(FAVORITES_PATH, []))
|
|
32
|
+
|
|
33
|
+
const openPlugin = plugin => {
|
|
34
|
+
const focusPlugin = ipc.access('MainWindow')?.ipcFocusPlugin
|
|
35
|
+
if (typeof focusPlugin === 'function') {
|
|
36
|
+
focusPlugin(plugin.id)
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
if (typeof plugin.handleClick === 'function') {
|
|
40
|
+
plugin.handleClick()
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
const doubleTabbed = window.config.get('poi.tabarea.double', false)
|
|
44
|
+
window.dispatch({
|
|
45
|
+
type: '@@TabSwitch',
|
|
46
|
+
tabInfo: doubleTabbed
|
|
47
|
+
? { activePluginName: plugin.id }
|
|
48
|
+
: { activeMainTab: plugin.id, activePluginName: plugin.id },
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const ShortcutBar = ({ plugins }) => React.createElement(
|
|
53
|
+
React.Fragment,
|
|
54
|
+
null,
|
|
55
|
+
plugins.map(plugin => React.createElement(
|
|
56
|
+
'button',
|
|
57
|
+
{
|
|
58
|
+
'aria-label': plugin.name,
|
|
59
|
+
className: `${BUTTON_CLASS} ${MINIMAL_CLASS} poi-plugin-shortcuts-button`,
|
|
60
|
+
key: plugin.id,
|
|
61
|
+
onClick: () => openPlugin(plugin),
|
|
62
|
+
title: plugin.name,
|
|
63
|
+
type: 'button',
|
|
64
|
+
},
|
|
65
|
+
plugin.displayIcon
|
|
66
|
+
? React.cloneElement(plugin.displayIcon, { 'aria-hidden': true })
|
|
67
|
+
: React.createElement('span', { className: 'fa fa-th-large', 'aria-hidden': true }),
|
|
68
|
+
)),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
const ensureStyle = () => {
|
|
72
|
+
if (document.getElementById(STYLE_ID)) return
|
|
73
|
+
const style = document.createElement('style')
|
|
74
|
+
style.id = STYLE_ID
|
|
75
|
+
style.textContent = `
|
|
76
|
+
poi-plugin-shortcuts.${ROOT_CLASS} {
|
|
77
|
+
align-items: stretch;
|
|
78
|
+
display: flex;
|
|
79
|
+
flex: 0 0 auto;
|
|
80
|
+
height: 30px;
|
|
81
|
+
overflow: hidden;
|
|
82
|
+
width: auto;
|
|
83
|
+
}
|
|
84
|
+
poi-plugin-shortcuts.${ROOT_CLASS} > .poi-plugin-shortcuts-button {
|
|
85
|
+
border-radius: 0;
|
|
86
|
+
flex: 0 0 34px;
|
|
87
|
+
height: 30px;
|
|
88
|
+
min-height: 30px;
|
|
89
|
+
min-width: 34px;
|
|
90
|
+
padding: 0;
|
|
91
|
+
}
|
|
92
|
+
.${MENU_CLASS} .${MENU_ITEM_CLASS}.${HOST_CLASS} {
|
|
93
|
+
padding-right: 34px;
|
|
94
|
+
position: relative;
|
|
95
|
+
}
|
|
96
|
+
.${MENU_CLASS} .${STAR_CLASS} {
|
|
97
|
+
align-items: center;
|
|
98
|
+
color: #ffc107;
|
|
99
|
+
cursor: pointer;
|
|
100
|
+
display: inline-flex;
|
|
101
|
+
font-size: 18px;
|
|
102
|
+
height: 28px;
|
|
103
|
+
justify-content: center;
|
|
104
|
+
opacity: 0;
|
|
105
|
+
pointer-events: none;
|
|
106
|
+
position: absolute;
|
|
107
|
+
right: 3px;
|
|
108
|
+
top: 50%;
|
|
109
|
+
transform: translateY(-50%);
|
|
110
|
+
transition: opacity 100ms ease;
|
|
111
|
+
width: 28px;
|
|
112
|
+
z-index: 3;
|
|
113
|
+
}
|
|
114
|
+
.${MENU_CLASS} .${MENU_ITEM_CLASS}.${HOST_CLASS}:hover > .${STAR_CLASS},
|
|
115
|
+
.${MENU_CLASS} .${MENU_ITEM_CLASS}.${HOST_CLASS}:focus-within > .${STAR_CLASS},
|
|
116
|
+
.${MENU_CLASS} .${STAR_CLASS}:focus {
|
|
117
|
+
opacity: 1;
|
|
118
|
+
pointer-events: auto;
|
|
119
|
+
}
|
|
120
|
+
`
|
|
121
|
+
document.head.appendChild(style)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const saveFavorite = pluginId => {
|
|
125
|
+
window.config.set(FAVORITES_PATH, toggleFavorite(getFavorites(), pluginId))
|
|
126
|
+
reconcile()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const stopMenuActivation = event => {
|
|
130
|
+
event.preventDefault()
|
|
131
|
+
event.stopPropagation()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const createStar = plugin => {
|
|
135
|
+
const star = document.createElement('span')
|
|
136
|
+
star.className = STAR_CLASS
|
|
137
|
+
star.dataset.pluginId = plugin.id
|
|
138
|
+
star.setAttribute('role', 'button')
|
|
139
|
+
star.setAttribute('tabindex', '0')
|
|
140
|
+
star.addEventListener('mousedown', stopMenuActivation)
|
|
141
|
+
star.addEventListener('pointerdown', stopMenuActivation)
|
|
142
|
+
star.addEventListener('click', event => {
|
|
143
|
+
stopMenuActivation(event)
|
|
144
|
+
saveFavorite(plugin.id)
|
|
145
|
+
})
|
|
146
|
+
star.addEventListener('keydown', event => {
|
|
147
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
148
|
+
stopMenuActivation(event)
|
|
149
|
+
saveFavorite(plugin.id)
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
return star
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const updateStar = (star, plugin, favoriteIds) => {
|
|
156
|
+
const favorite = favoriteIds.has(plugin.id)
|
|
157
|
+
const glyph = favorite ? '★' : '☆'
|
|
158
|
+
if (star.textContent !== glyph) star.textContent = glyph
|
|
159
|
+
star.setAttribute('aria-label', favorite ? `取消收藏 ${plugin.name}` : `收藏 ${plugin.name}`)
|
|
160
|
+
star.title = favorite ? '取消收藏' : '加入快捷列'
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const decoratePluginMenus = plugins => {
|
|
164
|
+
const favorites = new Set(getFavorites())
|
|
165
|
+
const pluginsById = new Map(plugins.map(plugin => [plugin.id, plugin]))
|
|
166
|
+
document.querySelectorAll(`.${MENU_CLASS}`).forEach(menu => {
|
|
167
|
+
const items = Array.from(menu.querySelectorAll(`.${MENU_ITEM_CLASS}`))
|
|
168
|
+
.filter(item => item.closest(`.${MENU_CLASS}`) === menu)
|
|
169
|
+
if (!items.some(item => pluginsById.has(item.id))) return
|
|
170
|
+
items.forEach((item, index) => {
|
|
171
|
+
const plugin = pluginsById.get(item.id) || plugins[index]
|
|
172
|
+
if (!plugin) return
|
|
173
|
+
item.classList.add(HOST_CLASS)
|
|
174
|
+
let star = Array.from(item.children).find(child => child.classList?.contains(STAR_CLASS))
|
|
175
|
+
if (!star || star.dataset.pluginId !== plugin.id) {
|
|
176
|
+
if (star) star.remove()
|
|
177
|
+
star = createStar(plugin)
|
|
178
|
+
item.appendChild(star)
|
|
179
|
+
}
|
|
180
|
+
updateStar(star, plugin, favorites)
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const renderShortcutBars = plugins => {
|
|
186
|
+
const favoritePlugins = getFavoritePlugins(plugins, getFavorites())
|
|
187
|
+
document.querySelectorAll('poi-info').forEach(infoBar => {
|
|
188
|
+
const mapReminder = Array.from(infoBar.children)
|
|
189
|
+
.find(child => child.tagName?.toLowerCase() === 'poi-map-reminder')
|
|
190
|
+
if (!mapReminder) return
|
|
191
|
+
let root = Array.from(infoBar.children).find(child => child.classList?.contains(ROOT_CLASS))
|
|
192
|
+
if (!root) {
|
|
193
|
+
root = document.createElement('poi-plugin-shortcuts')
|
|
194
|
+
root.className = ROOT_CLASS
|
|
195
|
+
infoBar.insertBefore(root, mapReminder)
|
|
196
|
+
} else if (root.nextSibling !== mapReminder) {
|
|
197
|
+
infoBar.insertBefore(root, mapReminder)
|
|
198
|
+
}
|
|
199
|
+
ReactDOM.render(React.createElement(ShortcutBar, { plugins: favoritePlugins }), root)
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const reconcile = () => {
|
|
204
|
+
if (!document.head) return
|
|
205
|
+
ensureStyle()
|
|
206
|
+
const plugins = getPlugins()
|
|
207
|
+
decoratePluginMenus(plugins)
|
|
208
|
+
renderShortcutBars(plugins)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const scheduleReconcile = () => {
|
|
212
|
+
if (reconcileScheduled) return
|
|
213
|
+
reconcileScheduled = true
|
|
214
|
+
setTimeout(() => {
|
|
215
|
+
reconcileScheduled = false
|
|
216
|
+
reconcile()
|
|
217
|
+
}, 0)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const handleConfigChange = path => {
|
|
221
|
+
if (path === FAVORITES_PATH || path.startsWith('poi.plugin.windowmode')) scheduleReconcile()
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const mount = () => {
|
|
225
|
+
if (observer) return
|
|
226
|
+
ensureStyle()
|
|
227
|
+
observer = new MutationObserver(scheduleReconcile)
|
|
228
|
+
observer.observe(document.documentElement, { childList: true, subtree: true })
|
|
229
|
+
window.config.addListener('config.set', handleConfigChange)
|
|
230
|
+
reconcileTimer = setInterval(reconcile, RECONCILE_INTERVAL_MS)
|
|
231
|
+
reconcile()
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const unmount = () => {
|
|
235
|
+
if (observer) observer.disconnect()
|
|
236
|
+
observer = null
|
|
237
|
+
if (reconcileTimer) clearInterval(reconcileTimer)
|
|
238
|
+
reconcileTimer = null
|
|
239
|
+
reconcileScheduled = false
|
|
240
|
+
window.config.removeListener('config.set', handleConfigChange)
|
|
241
|
+
document.querySelectorAll(`poi-plugin-shortcuts.${ROOT_CLASS}`).forEach(root => {
|
|
242
|
+
ReactDOM.unmountComponentAtNode(root)
|
|
243
|
+
root.remove()
|
|
244
|
+
})
|
|
245
|
+
document.querySelectorAll(`.${STAR_CLASS}`).forEach(star => star.remove())
|
|
246
|
+
document.querySelectorAll(`.${HOST_CLASS}`).forEach(item => item.classList.remove(HOST_CLASS))
|
|
247
|
+
document.getElementById(STYLE_ID)?.remove()
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
module.exports = { mount, unmount }
|
package/test.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const assert = require('assert')
|
|
4
|
+
const {
|
|
5
|
+
PLUGIN_ID,
|
|
6
|
+
getFavoritePlugins,
|
|
7
|
+
getListedPlugins,
|
|
8
|
+
normalizeFavorites,
|
|
9
|
+
toggleFavorite,
|
|
10
|
+
} = require('./core')
|
|
11
|
+
|
|
12
|
+
const plugins = [
|
|
13
|
+
{ id: 'alpha', enabled: true, reactClass: function Alpha() {} },
|
|
14
|
+
{ id: 'windowed', enabled: true, handleClick: () => {} },
|
|
15
|
+
{ id: 'disabled', enabled: false, reactClass: function Disabled() {} },
|
|
16
|
+
{ id: 'no-view', enabled: true },
|
|
17
|
+
{ id: PLUGIN_ID, enabled: true, reactClass: function Self() {} },
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
assert.deepStrictEqual(normalizeFavorites(null), [])
|
|
21
|
+
assert.deepStrictEqual(normalizeFavorites(['alpha', 'alpha', '', PLUGIN_ID, 3, 'windowed']), ['alpha', 'windowed'])
|
|
22
|
+
assert.deepStrictEqual(toggleFavorite(['alpha'], 'windowed'), ['alpha', 'windowed'])
|
|
23
|
+
assert.deepStrictEqual(toggleFavorite(['alpha', 'windowed'], 'alpha'), ['windowed'])
|
|
24
|
+
assert.deepStrictEqual(getListedPlugins(plugins).map(plugin => plugin.id), ['alpha', 'windowed'])
|
|
25
|
+
assert.deepStrictEqual(
|
|
26
|
+
getFavoritePlugins(plugins, ['windowed', 'disabled', 'missing', 'alpha']).map(plugin => plugin.id),
|
|
27
|
+
['windowed', 'alpha'],
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
console.log('poi-plugin-shortcuts tests passed')
|