@lvce-editor/main-process 1.0.3

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.
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @type {HTMLElement}
3
+ */
4
+ // @ts-ignore
5
+ const $QuickPickItems = document.getElementById('QuickPickItems')
6
+ /**
7
+ * @type {HTMLInputElement}
8
+ */
9
+ // @ts-ignore
10
+ const $QuickPickInput = document.getElementById('QuickPickInput')
11
+
12
+ export { $QuickPickInput, $QuickPickItems }
@@ -0,0 +1,88 @@
1
+ import * as RendererWorker from './QuickPickIpc.js'
2
+ import * as KeyBindings from './QuickPickKeyBindings.js'
3
+
4
+ export const handleBeforeInput = (event) => {
5
+ event.preventDefault()
6
+ const { target, inputType, data } = event
7
+ const { selectionStart, selectionEnd } = target
8
+ RendererWorker.send(
9
+ /* method */ 'QuickPick.handleBeforeInput',
10
+ /* inputType */ inputType,
11
+ /* data */ data,
12
+ /* selectionStart */ selectionStart,
13
+ /* selectionEnd */ selectionEnd,
14
+ )
15
+ }
16
+
17
+ const getNodeIndex = ($Node) => {
18
+ let index = 0
19
+ while (($Node = $Node.previousElementSibling)) {
20
+ index++
21
+ }
22
+ return index
23
+ }
24
+
25
+ const getItem = ($Target) => {
26
+ switch ($Target.className) {
27
+ case 'QuickPickItem':
28
+ return $Target
29
+ default:
30
+ return $Target.parentNode
31
+ }
32
+ }
33
+
34
+ export const handleMouseDown = (event) => {
35
+ event.preventDefault()
36
+ const { target } = event
37
+ const $Item = getItem(target)
38
+ const index = getNodeIndex($Item)
39
+ console.log({ $Item, index })
40
+ RendererWorker.send(/* selectIndex */ 'QuickPick.selectIndex', /* index */ index)
41
+ }
42
+
43
+ const getIdentifier = (event) => {
44
+ let identifier = ''
45
+ if (event.ctrlKey) {
46
+ identifier += 'ctrl+'
47
+ }
48
+ if (event.shiftKey) {
49
+ identifier += 'shift+'
50
+ }
51
+ if (event.altKey) {
52
+ identifier += 'alt+'
53
+ }
54
+ let { key } = event
55
+ if (key === ' ') {
56
+ key = 'Space'
57
+ }
58
+ if (key.length === 1) {
59
+ key = key.toLowerCase()
60
+ }
61
+ identifier += key
62
+ return identifier
63
+ }
64
+
65
+ const matchesIdentifier = (keyBinding, identifier) => {
66
+ return keyBinding.key === identifier
67
+ }
68
+
69
+ const getMatchingKeyBinding = (keyBindings, identifier) => {
70
+ for (const keyBinding of keyBindings) {
71
+ if (matchesIdentifier(keyBinding, identifier)) {
72
+ return keyBinding
73
+ }
74
+ }
75
+ return undefined
76
+ }
77
+
78
+ export const handleKeyDown = (event) => {
79
+ const keyBindings = KeyBindings.getKeyBindings()
80
+ const identifier = getIdentifier(event)
81
+ const matchingKeyBinding = getMatchingKeyBinding(keyBindings, identifier)
82
+ if (!matchingKeyBinding) {
83
+ return
84
+ }
85
+ event.preventDefault()
86
+ const { command } = matchingKeyBinding
87
+ RendererWorker.send(command)
88
+ }
@@ -0,0 +1,54 @@
1
+ import { $QuickPickInput, $QuickPickItems } from './QuickPickElements.js'
2
+ import * as KeyBindings from './QuickPickKeyBindings.js'
3
+
4
+ const activeId = 'QuickPickItemActive'
5
+
6
+ const create$Item = (item) => {
7
+ const $QuickPickItemIcon = document.createElement('div')
8
+ $QuickPickItemIcon.className = 'QuickPickItemIcon'
9
+ const $QuickPickItemLabel = document.createElement('div')
10
+ $QuickPickItemLabel.className = 'Label'
11
+ $QuickPickItemLabel.textContent = item.label
12
+
13
+ const $QuickPickItem = document.createElement('div')
14
+ $QuickPickItem.className = 'QuickPickItem'
15
+ $QuickPickItem.append($QuickPickItemIcon, $QuickPickItemLabel)
16
+
17
+ return $QuickPickItem
18
+ }
19
+ export const setVisiblePicks = (items) => {
20
+ $QuickPickItems.replaceChildren(...items.map(create$Item))
21
+ }
22
+
23
+ export const setValue = (value) => {
24
+ $QuickPickInput.value = value
25
+ }
26
+
27
+ export const setCursorOffset = (cursorOffset) => {
28
+ $QuickPickInput.selectionStart = cursorOffset
29
+ $QuickPickInput.selectionEnd = cursorOffset
30
+ }
31
+
32
+ export const setFocusedIndex = (oldFocusedIndex, newFocusedIndex) => {
33
+ if (oldFocusedIndex >= 0) {
34
+ const $OldItem = $QuickPickItems.children[oldFocusedIndex]
35
+ if ($OldItem) {
36
+ $OldItem.removeAttribute('id')
37
+ }
38
+ }
39
+ if (newFocusedIndex >= 0) {
40
+ const $NewItem = $QuickPickItems.children[newFocusedIndex]
41
+ if ($NewItem) {
42
+ $NewItem.id = activeId
43
+ $QuickPickInput.setAttribute('aria-activedescendant', activeId)
44
+ }
45
+ }
46
+ }
47
+
48
+ export const setItemsHeight = () => {
49
+ // not implemented
50
+ }
51
+
52
+ export const setKeyBindings = (keyBindings) => {
53
+ KeyBindings.setKeyBindings(keyBindings)
54
+ }
@@ -0,0 +1,36 @@
1
+ export const state = {
2
+ /**
3
+ * @type {MessagePort|undefined}
4
+ */
5
+ port: undefined,
6
+ }
7
+
8
+ export const send = (method, ...params) => {
9
+ if (!state.port) {
10
+ return
11
+ }
12
+ state.port.postMessage({
13
+ jsonrpc: '2.0',
14
+ method,
15
+ params,
16
+ })
17
+ }
18
+
19
+ const getPort = (type) => {
20
+ return new Promise((resolve, reject) => {
21
+ const handleMessageFromWindow = (event) => {
22
+ const port = event.ports[0]
23
+ resolve(port)
24
+ }
25
+ // @ts-ignore
26
+ window.addEventListener('message', handleMessageFromWindow, {
27
+ once: true,
28
+ })
29
+ })
30
+ }
31
+
32
+ export const initialize = async () => {
33
+ const port = await getPort('quickpick-browserview')
34
+ state.port = port
35
+ return port
36
+ }
@@ -0,0 +1,13 @@
1
+ export const state = {
2
+ /**
3
+ * @type {any[]}
4
+ */
5
+ keyBindings: [],
6
+ }
7
+
8
+ export const setKeyBindings = (keyBindings) => {
9
+ state.keyBindings = keyBindings
10
+ }
11
+ export const getKeyBindings = () => {
12
+ return state.keyBindings
13
+ }
@@ -0,0 +1,9 @@
1
+ const { ipcRenderer } = require('electron')
2
+
3
+ const handlePort = (event) => {
4
+ const port = event.ports[0]
5
+ // console.log('[preload] got port', port)
6
+ window.postMessage('abc', '*', [port])
7
+ }
8
+
9
+ ipcRenderer.on('port', handlePort)
@@ -0,0 +1,117 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>QuickPick</title>
7
+ <style>
8
+ :root {
9
+ --QuickPickBackground: rgb(40, 46, 47);
10
+ --FocusOutline: #3d5252;
11
+ --ListActiveSelectionBackground: #515f59;
12
+ --ListActiveSelectionForeground: #ffffff;
13
+ --ListHoverBackground: #405c5033;
14
+ --ListHoverForeground: #e0e0e0;
15
+ --ListForeground: rgb(156, 162, 160);
16
+ }
17
+ html,
18
+ body {
19
+ width: 100%;
20
+ overflow: hidden;
21
+ margin: 0;
22
+ cursor: default;
23
+ }
24
+
25
+ body {
26
+ font-family:
27
+ system-ui,
28
+ Ubuntu,
29
+ Droid Sans,
30
+ sans-serif;
31
+ }
32
+
33
+ #QuickPick {
34
+ padding: 0 1px 6px;
35
+ background: var(--QuickPickBackground);
36
+ color: var(--ListForeground);
37
+ box-sizing: border-box;
38
+ contain: content;
39
+ display: flex;
40
+ flex-direction: column;
41
+ row-gap: 4px;
42
+ pointer-events: all;
43
+ box-shadow: rgb(0 0 0 / 15%) 0px 0px 8px 2px;
44
+ }
45
+
46
+ #QuickPickHeader {
47
+ /* width: 100%; */
48
+ display: flex;
49
+ /* margin: 6px 6px 0; */
50
+ padding: 6px 6px 0;
51
+ /* contain: strict; */
52
+ height: 30px;
53
+ contain: strict;
54
+ /* margin-bottom: -2px; */
55
+ }
56
+
57
+ .QuickPickItem {
58
+ display: flex;
59
+ white-space: nowrap;
60
+ align-items: center;
61
+ width: 100%;
62
+ height: 22px;
63
+ font-size: 13px;
64
+ margin: 0;
65
+ padding: 0 6px;
66
+ contain: strict;
67
+ gap: 4px;
68
+ }
69
+
70
+ .QuickPickItem:hover {
71
+ background: var(--ListHoverBackground);
72
+ color: var(--ListHoverForeground);
73
+ }
74
+
75
+ #QuickPickItemActive {
76
+ color: var(--ListActiveSelectionForeground);
77
+ background: var(--ListActiveSelectionBackground);
78
+ }
79
+
80
+ .InputBox {
81
+ width: 100%;
82
+ margin: 0;
83
+ border: none;
84
+ outline: none;
85
+ flex: 1;
86
+ background: var(--InputBoxBackground);
87
+ border: 1px solid rgb(55, 65, 63);
88
+ padding: 4px;
89
+ color: var(--InputBoxForeground);
90
+ font-size: 13px;
91
+ contain: strict;
92
+ }
93
+
94
+ .Label {
95
+ text-overflow: ellipsis;
96
+ overflow: hidden;
97
+ display: inline-block;
98
+ white-space: nowrap;
99
+ }
100
+
101
+ #QuickPickItems {
102
+ color: white;
103
+ display: flex;
104
+ flex-direction: column;
105
+ }
106
+ </style>
107
+ <script type="module" src="quickpick.js"></script>
108
+ </head>
109
+ <body>
110
+ <div id="QuickPick">
111
+ <div id="QuickPickHeader">
112
+ <input type="text" class="InputBox" id="QuickPickInput" spellcheck="false" autofocus />
113
+ </div>
114
+ <div id="QuickPickItems"></div>
115
+ </div>
116
+ </body>
117
+ </html>
@@ -0,0 +1,48 @@
1
+ import * as QuickPickFunctions from './QuickPickFunctions.js'
2
+ import * as QuickPickEvents from './QuickPickEvents.js'
3
+ import * as QuickPickIpc from './QuickPickIpc.js'
4
+ import { $QuickPickInput, $QuickPickItems } from './QuickPickElements.js'
5
+
6
+ const executeCommand = (command) => {
7
+ const _0 = command[0]
8
+ const _1 = command[1]
9
+ const _2 = command[2]
10
+ if (_0 === 'Viewlet.create' || _0 === 'Viewlet.show') {
11
+ return
12
+ }
13
+ const args = command.slice(3)
14
+ const fn = QuickPickFunctions[_2]
15
+ fn(...args)
16
+ }
17
+
18
+ const executeCommands = (commands) => {
19
+ for (const command of commands) {
20
+ executeCommand(command)
21
+ }
22
+ }
23
+
24
+ const getFn = (method) => {
25
+ switch (method) {
26
+ case 'executeCommands':
27
+ return executeCommands
28
+ default:
29
+ throw new Error('method not found')
30
+ }
31
+ }
32
+
33
+ const handleMessage = (event) => {
34
+ const message = event.data
35
+ // console.log({ message })
36
+ const fn = getFn(message.method)
37
+ fn(message.params)
38
+ }
39
+
40
+ const main = async () => {
41
+ $QuickPickInput.addEventListener('beforeinput', QuickPickEvents.handleBeforeInput)
42
+ $QuickPickItems.onmousedown = QuickPickEvents.handleMouseDown
43
+ window.onkeydown = QuickPickEvents.handleKeyDown
44
+ const port = await QuickPickIpc.initialize()
45
+ port.onmessage = handleMessage
46
+ }
47
+
48
+ main()
@@ -0,0 +1,36 @@
1
+ export const state = {
2
+ /**
3
+ * @type {MessagePort|undefined}
4
+ */
5
+ port: undefined,
6
+ }
7
+
8
+ export const send = (method, ...params) => {
9
+ if (!state.port) {
10
+ return
11
+ }
12
+ state.port.postMessage({
13
+ jsonrpc: '2.0',
14
+ method,
15
+ params,
16
+ })
17
+ }
18
+
19
+ const getPort = (type) => {
20
+ return new Promise((resolve, reject) => {
21
+ const handleMessageFromWindow = (event) => {
22
+ const port = event.ports[0]
23
+ resolve(port)
24
+ }
25
+ // @ts-ignore
26
+ window.addEventListener('message', handleMessageFromWindow, {
27
+ once: true,
28
+ })
29
+ })
30
+ }
31
+
32
+ export const initialize = async () => {
33
+ const port = await getPort('quickpick-browserview')
34
+ state.port = port
35
+ return port
36
+ }
@@ -0,0 +1,9 @@
1
+ const { ipcRenderer } = require('electron')
2
+
3
+ const handlePort = (event) => {
4
+ const port = event.ports[0]
5
+ // console.log('[preload] got port', port)
6
+ window.postMessage('abc', '*', [port])
7
+ }
8
+
9
+ ipcRenderer.on('port', handlePort)
@@ -0,0 +1,16 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>Document</title>
8
+ <style>
9
+ body {
10
+ background: white;
11
+ }
12
+ </style>
13
+ <script type="module" src="suggestions.js"></script>
14
+ </head>
15
+ <body></body>
16
+ </html>
@@ -0,0 +1,8 @@
1
+ import * as Ipc from './Ipc.js'
2
+
3
+ const main = async () => {
4
+ const port = await Ipc.initialize()
5
+ console.log({ port })
6
+ }
7
+
8
+ main()