external-completion-3/core
from /programming-notes/external-completion-3/core
external-completion-3のメインコード
dependencies
scrapbox-dom-accessor
external-completion-3/completionObserver-2
scrapbox-suggest-container-3
読み込みに時間がかかるときにメッセージを出すscript
async-singleton
code:script.js
import {scrapboxDOM} from '../scrapbox-dom-accessor/script.js';
import {CompletionObserver} from '../external-completion-3%2FcompletionObserver-2/script.js';
import {suggestContainer} from '../scrapbox-suggest-container-3/script.js';
import {loading} from '../読み込みに時間がかかるときにメッセージを出すscript/script.js';
import {asyncSingleton} from '../async-singleton/script.js';
export class ExternalCompletion {
constructor() {
this._suggestBox = suggestContainer();
scrapboxDOM.editor.append(this._suggestBox);
this._settingIds = [];
this._observer = new CompletionObserver();
}
push(...settings) {
const oncompletion = asyncSingleton((...params) => this._oncompletion(...params));
for (const {test, display, onchange} of settings) {
this._settingIds.push(this._observer.register({
test: (...params) => test(...params),
oncompletionstart: (...params) => oncompletion(onchange(...params), display(...params)),
oncompletionupdate: (...params) => oncompletion(onchange(...params), display(...params)),
oncompletionend: () => this._suggestBox.clear(),
}));
}
}
get completing() { return this._observer.completing; }
selectPrev() { this._suggestBox.selectPrevious({wrap: true}); }
selectNext() { this._suggestBox.selectNext({wrap: true}); }
start() {
this._observer.start();
}
end() {
this._observer.end();
this._suggestBox.hide();
}
confirm({mode} = {}) {
(this._suggestBox.selectedItem ?? this._suggestBox.firstItem).click({mode});
}
async _oncompletion(result, target) {
const pair = await loading(result, {
// 時間がかかるようであればSearching表示をする
onstart: () => {
const image = /paper-dark-dark|default-dark/
.test(document.documentElement.dataset.projectTheme) ?
'https://img.icons8.com/ios/180/FFFFFF/loading.png' :
'https://img.icons8.com/ios/180/loading.png';
this._suggestBox.pushFirst({title: Searching "${target}"..., image,});
},
onend: () => this._suggestBox.pop(0),
delay: 1000,
});
if (!pair) return;
const {items, position} = pair;
// 補完が終了していたら何もしない
if (!this.completing) {
this.end();
return;
}
// 新しいアイテムに差し替えてwindowを開く
if (items) this._suggestBox.replace(...items);
// ここで表示位置を計算させ直す?
this._suggestBox.position(position);
this._suggestBox.show();
}
}
function _log(msg, ...objects) {
const title = 'external-completion-3';
console.log([main.js@${title}] , msg, ...objects);
}
#2021-04-08 16:56:53