Skip to content

Commit

Permalink
plugin: auto add IMs; drop name requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Dec 17, 2024
1 parent c176be4 commit eb4b55e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion page/Fcitx5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface FCITX {
jsKeyToFcitxString: (event: KeyboardEvent) => string
getMenuActions: () => MenuAction[]
activateMenuAction: (id: number) => void
installPlugin: (name: string, buffer: ArrayBuffer) => void
installPlugin: (buffer: ArrayBuffer) => string
getInstalledPlugins: () => string[]
Module: EM_MODULE
}
Expand Down
20 changes: 18 additions & 2 deletions page/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ function reload() {
Module.ccall('reload', 'void', [], [])
}

export function installPlugin(name: string, buffer: ArrayBuffer) {
const textDecoder = new TextDecoder()

function addDefaultIMs(byteArray: Uint8Array) {
const inputMethods = (JSON.parse(textDecoder.decode(byteArray)).input_methods ?? []) as string[]
const currentInputMethods = window.fcitx.getInputMethods().map(im => im.name)
window.fcitx.setInputMethods([...currentInputMethods, ...inputMethods.filter(im => !currentInputMethods.includes(im))])
}

export function installPlugin(buffer: ArrayBuffer) {
// UZIP hangs on empty zip.
if (buffer.byteLength < 1024) {
throw new Error('Invalid plugin')
}
const manifest = UZIP.parse(buffer)
if (!(`plugin/${name}.json` in manifest)) {
const names = Object.keys(manifest).map(path => (path.match(/^plugin\/(\S+)\.json$/) ?? { 1: undefined })[1]).filter(name => name !== undefined)
if (names.length !== 1) {
throw new Error('Invalid plugin')
}
const name = names[0]
const byteArray = manifest[`plugin/${name}.json`]
if (!byteArray) {
throw new Error('Invalid plugin')
}
Object.entries(manifest).forEach(([path, data]) => {
Expand All @@ -25,6 +39,8 @@ export function installPlugin(name: string, buffer: ArrayBuffer) {
}
})
reload()
addDefaultIMs(byteArray)
return name
}

export function getInstalledPlugins() {
Expand Down

0 comments on commit eb4b55e

Please sign in to comment.