-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): load .kmx keyboard from blob
This change adds the web side of loading a .kmx keyboard from a blob. It also replaces the `CoreProcessor` class with `CoreFactory` that allows to directly use the methods defined in WASM without having to add another wrapper for each method.
- Loading branch information
1 parent
6bef09f
commit a072171
Showing
8 changed files
with
82 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Keyman is copyright (C) SIL International. MIT License. | ||
|
||
import { type MainModule } from './import/core/keymancore.js'; | ||
|
||
// Unfortunately embind has an open issue with enums and typescript where it | ||
// only generates a type for the enum, but not the values in a usable way. | ||
// So we have to re-define the enum here. | ||
// See https://github.com/emscripten-core/emscripten/issues/18585 | ||
// NOTE: Keep in sync with core/include/keyman/keyman_core_api.h#L311 | ||
export enum KM_CORE_STATUS { | ||
OK = 0, | ||
NO_MEM = 1, | ||
IO_ERROR = 2, | ||
INVALID_ARGUMENT = 3, | ||
KEY_ERROR = 4, | ||
INSUFFICENT_BUFFER = 5, | ||
INVALID_UTF = 6, | ||
INVALID_KEYBOARD = 7, | ||
NOT_IMPLEMENTED = 8, | ||
OS_ERROR = 0x80000000 | ||
} | ||
|
||
export class CoreFactory { | ||
public static async createCoreProcessor(baseurl: string): Promise<MainModule> { | ||
try { | ||
const module = await import(baseurl + '/km-core.js'); | ||
const createCoreProcessor = module.default; | ||
return await createCoreProcessor({ | ||
locateFile: function (path: string, scriptDirectory: string) { | ||
return baseurl + '/' + path; | ||
} | ||
}); | ||
} catch (e: any) { | ||
console.log('got execption in CoreFactory.createCoreProcessor', e); | ||
return null; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './core-processor.js'; | ||
export { CoreFactory, KM_CORE_STATUS } from './core-factory.js'; | ||
import { type MainModule, type km_core_keyboard, type CoreKeyboardReturn } from './import/core/keymancore.js'; | ||
export { MainModule, km_core_keyboard, CoreKeyboardReturn }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
import { assert } from 'chai'; | ||
import { CoreProcessor } from 'keyman/engine/core-processor'; | ||
import { CoreFactory, KM_CORE_STATUS } from 'keyman/engine/core-processor'; | ||
|
||
const coreurl = '/web/build/engine/core-processor/obj/import/core'; | ||
const coreurl = '/build/engine/core-processor/obj/import/core'; | ||
|
||
// Test the CoreProcessor interface. | ||
describe('CoreProcessor', function () { | ||
async function loadKeyboardBlob(uri: string) { | ||
const response = await fetch(uri); | ||
if (!response.ok) { | ||
throw new Error(`HTTP ${response.status} ${response.statusText}`); | ||
} | ||
|
||
const buffer = await response.arrayBuffer(); | ||
return new Uint8Array(buffer); | ||
} | ||
|
||
it('can initialize without errors', async function () { | ||
const kp = new CoreProcessor(); | ||
assert.isTrue(await kp.init(coreurl)); | ||
assert.isNotNull(await CoreFactory.createCoreProcessor(coreurl)); | ||
}); | ||
|
||
it('can call temp function', async function () { | ||
const kp = new CoreProcessor(); | ||
await kp.init(coreurl); | ||
const a = kp.tmp_wasm_attributes(); | ||
const km_core = await CoreFactory.createCoreProcessor(coreurl); | ||
const a = km_core.tmp_wasm_attributes(); | ||
assert.isNotNull(a); | ||
assert.isNumber(a.max_context); | ||
console.dir(a); | ||
}); | ||
|
||
it('can load a keyboard from blob', async function () { | ||
const km_core = await CoreFactory.createCoreProcessor(coreurl); | ||
const blob = await loadKeyboardBlob('/common/test/resources/keyboards/test_8568_deadkeys.kmx') | ||
const result = km_core.keyboard_load_from_blob('test', blob); | ||
assert.equal(result.status, KM_CORE_STATUS.OK); | ||
assert.isNotNull(result.object); | ||
result.delete(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters