Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions page/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,45 @@ export function redrawPreeditUnderline() {
export function hasPreedit() {
return !!preedit
}

export function deleteSurroundingText(offset: number, size: number) {
const input = getInputElement()
if (!input) {
return
}

const text = input.value
const jsCursor = input.selectionStart!
const chars = [...text]

let charCursor = 0
let currentJsIndex = 0
for (const char of chars) {
if (currentJsIndex >= jsCursor) {
break
}
currentJsIndex += char.length
charCursor++
}

const startCharIndex = charCursor + offset
const endCharIndex = startCharIndex + size

if (startCharIndex < 0 || endCharIndex > chars.length || startCharIndex >= endCharIndex) {
return
}

let startJsIndex = 0
for (let i = 0; i < startCharIndex; i++) {
startJsIndex += chars[i].length
}
let endJsIndex = startJsIndex
for (let i = startCharIndex; i < endCharIndex; i++) {
endJsIndex += chars[i].length
}

input.setRangeText('', startJsIndex, endJsIndex, 'end')
input.dispatchEvent(new Event('change'))
input.dispatchEvent(new Event('input'))
onTextChange(input.value)
}
3 changes: 2 additions & 1 deletion page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Input } from './focus'
import UZIP from 'uzip'
import { activateMenuAction, getMenuActions } from './action'
import { cli } from './cli'
import { commit, placePanel, setPreedit } from './client'
import { commit, deleteSurroundingText, placePanel, setPreedit } from './client'
import { getAddons, getConfig, setConfig } from './config'
import { OPTIONS, SERVICE_WORKER, WEB, WEB_WORKER } from './constant'
import { hasTouch, isFirefox } from './context'
Expand Down Expand Up @@ -72,6 +72,7 @@ globalThis.fcitx = Object.assign((...args: any[]) => {
invoke: (name: string, args: string) => fcitx[name](...JSON.parse(args)),
setPreedit,
commit,
deleteSurroundingText,
sendEventToKeyboard,
getLanguageName,
setCurrentInputMethod,
Expand Down
5 changes: 5 additions & 0 deletions wasmfrontend/wasmfrontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ WasmInputContext::WasmInputContext(WasmFrontend *frontend,

WasmInputContext::~WasmInputContext() { destroy(); }

void WasmInputContext::deleteSurroundingTextImpl(int offset,
unsigned int size) {
EM_ASM(fcitx.deleteSurroundingText($0, $1), offset, size);
}

void WasmInputContext::commitStringImpl(const std::string &text) {
EM_ASM(fcitx.commit(UTF8ToString($0)), text.c_str());
}
Expand Down
2 changes: 1 addition & 1 deletion wasmfrontend/wasmfrontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class WasmInputContext : public InputContext {

const char *frontend() const override { return "wasm"; }
void commitStringImpl(const std::string &text) override;
void deleteSurroundingTextImpl(int offset, unsigned int size) override {}
void deleteSurroundingTextImpl(int offset, unsigned int size) override;
void forwardKeyImpl(const ForwardKeyEvent &key) override {}
void updatePreeditImpl() override;

Expand Down
Loading