3 Star 17 Fork 3

toolkit/typora_plugin

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
read_only.js 2.25 KB
一键复制 编辑 原始数据 按行查看 历史
obgnail 提交于 2023-11-30 16:31 +08:00 . Refactor plugin.js
class readOnlyPlugin extends BasePlugin {
styleTemplate = () => true
hotkey = () => [{hotkey: this.config.HOTKEY, callback: this.call}]
process = () => {
const write = document.getElementById("write");
write.addEventListener("compositionstart", ev => (File.isLocked) && this.stop(ev), true);
write.addEventListener("keydown", ev => {
if (File.isLocked && (ev.key === "Enter" || ev.key === "Backspace" || ev.key === "Delete" || ev.key === ' ')) {
this.stop(ev);
}
}, true);
if (this.config.READ_ONLY_DEFAULT) {
this.utils.loopDetector(() => File && File.lock, this.call);
}
const setCheckbox = disabled => {
write.querySelectorAll(`input[type="checkbox"]`).forEach(input => {
if (disabled) {
input.setAttribute("disabled", "true");
} else {
input.removeAttribute("disabled");
}
});
}
const setInput = disabled => {
if (disabled) {
[
"#plugin-search-multi-input input", "#plugin-commander-form input", "#plugin-toolbar-input input",
"#plugin-multi-highlighter-input input", "#typora-quick-open-input input"
].forEach(selector => {
const input = document.querySelector(selector);
input && input.removeAttribute("readonly");
})
}
}
this.utils.decorate(() => File, "freshLock", null, () => {
setCheckbox(File.isLocked);
setInput(File.isLocked);
})
}
stop = ev => {
File.lock();
document.activeElement.blur();
ev.preventDefault();
ev.stopPropagation();
}
call = () => {
const span = document.getElementById("footer-word-count-label");
if (File.isLocked) {
File.unlock();
span.setAttribute("data-value", "");
} else {
File.lock();
document.activeElement.blur();
span.setAttribute("data-value", this.config.SHOW_TEXT + String.fromCharCode(160).repeat(3));
}
}
}
module.exports = {
plugin: readOnlyPlugin,
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/toolkit/typora_plugin.git
git@gitee.com:toolkit/typora_plugin.git
toolkit
typora_plugin
typora_plugin
master

搜索帮助