|
| 1 | +import "@logseq/libs" |
| 2 | + |
| 3 | +async function main() { |
| 4 | + const settings = await generateUserConfig() |
| 5 | + |
| 6 | + logseq.provideStyle(` |
| 7 | + span[data-ref="#red"], |
| 8 | + span[data-ref="#green"], |
| 9 | + span[data-ref="#blue"], |
| 10 | + span[data-ref="$red"], |
| 11 | + span[data-ref="$green"], |
| 12 | + span[data-ref="$blue"] { |
| 13 | + display: none; |
| 14 | + } |
| 15 | + span[data-ref="#red"] + mark { |
| 16 | + background: #ffc7c7; |
| 17 | + } |
| 18 | + span[data-ref="#green"] + mark { |
| 19 | + background: #ccffc1; |
| 20 | + } |
| 21 | + span[data-ref="#blue"] + mark { |
| 22 | + background: #abdfff; |
| 23 | + } |
| 24 | + span[data-ref="$red"] + mark { |
| 25 | + color: #f00; |
| 26 | + background: unset; |
| 27 | + padding: 0; |
| 28 | + border-radius: 0; |
| 29 | + } |
| 30 | + span[data-ref="$green"] + mark { |
| 31 | + color: #0f0; |
| 32 | + background: unset; |
| 33 | + padding: 0; |
| 34 | + border-radius: 0; |
| 35 | + } |
| 36 | + span[data-ref="$blue"] + mark { |
| 37 | + color: #00f; |
| 38 | + background: unset; |
| 39 | + padding: 0; |
| 40 | + border-radius: 0; |
| 41 | + } |
| 42 | + `) |
| 43 | + |
| 44 | + const model = {} |
| 45 | + for (const { key, template } of settings.wrappings) { |
| 46 | + model[key] = () => wrap(template) |
| 47 | + } |
| 48 | + logseq.provideModel(model) |
| 49 | + |
| 50 | + for (const { key, label, binding } of settings.wrappings) { |
| 51 | + if (binding) { |
| 52 | + logseq.App.registerCommandPalette( |
| 53 | + { key, label, keybinding: { binding } }, |
| 54 | + model[key], |
| 55 | + ) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + console.log("#wrap loaded") |
| 60 | +} |
| 61 | + |
| 62 | +async function generateUserConfig() { |
| 63 | + if (!logseq.settings?.wrappings) { |
| 64 | + // Generate the default settings if not any. |
| 65 | + const { preferredLanguage: lang } = await logseq.App.getUserConfigs() |
| 66 | + const defaultSettings = { |
| 67 | + disabled: false, |
| 68 | + toolbar: logseq.settings?.toolbar ?? true, |
| 69 | + wrappings: [ |
| 70 | + { |
| 71 | + key: "wrap-cloze", |
| 72 | + label: lang === "zh-CN" ? "包围成 cloze" : "Wrap with cloze", |
| 73 | + binding: "mod+ctrl+c", |
| 74 | + template: "{{cloze $^}}", |
| 75 | + }, |
| 76 | + { |
| 77 | + key: "wrap-red-hl", |
| 78 | + label: |
| 79 | + lang === "zh-CN" ? "包围成红色高亮" : "Wrap with red highlight", |
| 80 | + binding: "mod+ctrl+r", |
| 81 | + template: "[[#red]]==$^==", |
| 82 | + }, |
| 83 | + { |
| 84 | + key: "wrap-green-hl", |
| 85 | + label: |
| 86 | + lang === "zh-CN" ? "包围成绿色高亮" : "Wrap with green highlight", |
| 87 | + binding: "mod+ctrl+g", |
| 88 | + template: "[[#green]]==$^==", |
| 89 | + }, |
| 90 | + { |
| 91 | + key: "wrap-blue-hl", |
| 92 | + label: |
| 93 | + lang === "zh-CN" ? "包围成蓝色高亮" : "Wrap with blue highlight", |
| 94 | + binding: "mod+ctrl+b", |
| 95 | + template: "[[#blue]]==$^==", |
| 96 | + }, |
| 97 | + { |
| 98 | + key: "wrap-yellow-hl", |
| 99 | + label: |
| 100 | + lang === "zh-CN" ? "包围成黄色高亮" : "Wrap with yellow highlight", |
| 101 | + binding: "mod+ctrl+y", |
| 102 | + template: "[[#yellow]]==$^==", |
| 103 | + }, |
| 104 | + { |
| 105 | + key: "wrap-red-text", |
| 106 | + label: lang === "zh-CN" ? "包围成红色文字" : "Wrap with red text", |
| 107 | + binding: "", |
| 108 | + template: "[[$red]]==$^==", |
| 109 | + }, |
| 110 | + { |
| 111 | + key: "wrap-green-text", |
| 112 | + label: lang === "zh-CN" ? "包围成绿色文字" : "Wrap with green text", |
| 113 | + binding: "", |
| 114 | + template: "[[$green]]==$^==", |
| 115 | + }, |
| 116 | + { |
| 117 | + key: "wrap-blue-text", |
| 118 | + label: lang === "zh-CN" ? "包围成蓝色文字" : "Wrap with blue text", |
| 119 | + binding: "", |
| 120 | + template: "[[$blue]]==$^==", |
| 121 | + }, |
| 122 | + ], |
| 123 | + } |
| 124 | + logseq.updateSettings(defaultSettings) |
| 125 | + return defaultSettings |
| 126 | + } |
| 127 | + return logseq.settings |
| 128 | +} |
| 129 | + |
| 130 | +async function wrap(template) { |
| 131 | + const block = await logseq.Editor.getCurrentBlock() |
| 132 | + const textarea = parent.document.activeElement |
| 133 | + |
| 134 | + if (block == null || textarea == null) return |
| 135 | + |
| 136 | + const start = textarea.selectionStart |
| 137 | + const end = textarea.selectionEnd |
| 138 | + const before = block.content.substring(0, start) |
| 139 | + const selection = textarea.value.substring(start, end) |
| 140 | + const after = block.content.substring(end) |
| 141 | + const [wrapBefore, wrapAfter] = template.split("$^") |
| 142 | + const text = `${before}${wrapBefore}${selection}${wrapAfter ?? ""}${after}` |
| 143 | + await logseq.Editor.updateBlock(block.uuid, text) |
| 144 | + textarea.setSelectionRange(start + wrapBefore.length, end + wrapBefore.length) |
| 145 | +} |
| 146 | + |
| 147 | +logseq.ready(main).catch(console.error) |
0 commit comments