mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-09 16:54:21 +01:00
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import {Model} from "../layout/Model";
|
|
import {Tab} from "../layout/Tab";
|
|
import {Protyle} from "../protyle";
|
|
import {genSearch} from "./util";
|
|
|
|
export class Search extends Model {
|
|
private element: HTMLElement;
|
|
public config: ISearchOption;
|
|
public edit: Protyle;
|
|
|
|
constructor(options: { tab: Tab, config: ISearchOption }) {
|
|
super({
|
|
id: options.tab.id,
|
|
});
|
|
this.element = options.tab.panelElement as HTMLElement;
|
|
this.config = options.config;
|
|
this.edit = genSearch(this.config, this.element);
|
|
}
|
|
|
|
public updateSearch(text: string, replace: boolean) {
|
|
const inputElement = this.element.querySelector(".b3-text-field") as HTMLInputElement;
|
|
if (text === "") {
|
|
inputElement.select();
|
|
return;
|
|
}
|
|
const oldText = inputElement.value;
|
|
if (oldText === text) {
|
|
return;
|
|
}
|
|
if (!replace) {
|
|
if (oldText.indexOf(text) > -1) {
|
|
text = oldText.replace(text + " ", "").replace(" " + text, "");
|
|
} else if (oldText !== "") {
|
|
text = oldText + " " + text;
|
|
}
|
|
}
|
|
inputElement.value = text;
|
|
inputElement.select();
|
|
inputElement.dispatchEvent(new CustomEvent("input"));
|
|
}
|
|
}
|