Skip to content

Commit 423d5f0

Browse files
committed
chore: initial commit
0 parents  commit 423d5f0

12 files changed

+502
-0
lines changed

.github/workflows/main.yml

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This is a basic workflow to help you get started with Actions
2+
3+
name: Release
4+
5+
env:
6+
PLUGIN_NAME: logseq-plugin-wrap
7+
8+
# Controls when the workflow will run
9+
on:
10+
push:
11+
tags:
12+
- "*" # Push events to matching any tag format, i.e. 1.0, 20.15.10
13+
14+
# Allows you to run this workflow manually from the Actions tab
15+
workflow_dispatch:
16+
17+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
18+
jobs:
19+
release:
20+
# The type of runner that the job will run on
21+
runs-on: ubuntu-latest
22+
23+
# Steps represent a sequence of tasks that will be executed as part of the job
24+
steps:
25+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
26+
- uses: actions/checkout@v2
27+
- uses: actions/setup-node@v2
28+
with:
29+
node-version: "14.x"
30+
31+
- name: Build
32+
id: build
33+
run: |
34+
yarn
35+
yarn build
36+
mkdir ${{ env.PLUGIN_NAME }}
37+
cp README.md package.json icon.png ${{ env.PLUGIN_NAME }}
38+
mv dist ${{ env.PLUGIN_NAME }}
39+
zip -r ${{ env.PLUGIN_NAME }}.zip ${{ env.PLUGIN_NAME }}
40+
ls
41+
echo "::set-output name=tag_name::$(git tag --sort version:refname | tail -n 1)"
42+
43+
- name: Create Release
44+
id: create_release
45+
uses: actions/create-release@v1
46+
env:
47+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48+
VERSION: ${{ github.ref }}
49+
with:
50+
tag_name: ${{ github.ref }}
51+
release_name: ${{ github.ref }}
52+
draft: false
53+
prerelease: false
54+
55+
- name: Upload zip file
56+
id: upload_zip
57+
uses: actions/upload-release-asset@v1
58+
env:
59+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60+
with:
61+
upload_url: ${{ steps.create_release.outputs.upload_url }}
62+
asset_path: ./${{ env.PLUGIN_NAME }}.zip
63+
asset_name: ${{ env.PLUGIN_NAME }}-${{ steps.build.outputs.tag_name }}.zip
64+
asset_content_type: application/zip

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.cache
2+
node_modules
3+
dist
4+
stats.html
5+
yarn-error.log

.prettierrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"arrowParens": "always",
3+
"endOfLine": "lf",
4+
"semi": false,
5+
"singleQuote": false,
6+
"trailingComma": "all"
7+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Seth Yuan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# logseq-plugin-wrap
2+
3+
创建自定义文字包裹行为及快捷键,默认提供了一组实用的配置。
4+
5+
Create your own wrappings with optional key bindings for selected text, a set of useful defaults is also provided.
6+
7+
## 使用展示 (Usage)
8+
9+
![demo](./demo.gif)
10+
11+
## 用户配置 (User configs)
12+
13+
```json
14+
{
15+
"disabled": false,
16+
"toolbar": true,
17+
"wrappings": [
18+
{
19+
"key": "wrap-cloze",
20+
"label": "Wrap with cloze",
21+
"binding": "mod+ctrl+c",
22+
"template": "{{cloze $^}}"
23+
},
24+
{
25+
"key": "wrap-red-hl",
26+
"label": "Wrap with red highlight",
27+
"binding": "mod+ctrl+r",
28+
"template": "[[#red]]==$^=="
29+
},
30+
{
31+
"key": "wrap-green-hl",
32+
"label": "Wrap with green highlight",
33+
"binding": "mod+ctrl+g",
34+
"template": "[[#green]]==$^=="
35+
},
36+
{
37+
"key": "wrap-blue-hl",
38+
"label": "Wrap with blue highlight",
39+
"binding": "mod+ctrl+b",
40+
"template": "[[#blue]]==$^=="
41+
},
42+
{
43+
"key": "wrap-yellow-hl",
44+
"label": "Wrap with yellow highlight",
45+
"binding": "mod+ctrl+y",
46+
"template": "[[#yellow]]==$^=="
47+
},
48+
{
49+
"key": "wrap-red-text",
50+
"label": "Wrap with red text",
51+
"binding": "",
52+
"template": "[[$red]]==$^=="
53+
},
54+
{
55+
"key": "wrap-green-text",
56+
"label": "Wrap with green text",
57+
"binding": "",
58+
"template": "[[$green]]==$^=="
59+
},
60+
{
61+
"key": "wrap-blue-text",
62+
"label": "Wrap with blue text",
63+
"binding": "",
64+
"template": "[[$blue]]==$^=="
65+
}
66+
]
67+
}
68+
```
69+
70+
在 Logseq 的插件页面打开插件的配置后,有以下几项配置可供使用,请参照上方代码块进行设置(各项的默认值以体现在代码块中):
71+
72+
- `wrappings`: 自定义的包裹行为都定义在这里。你可以扩展默认提供的行为,也可以移除或替换你不需要的默认行为。配置方法请参考上面的配置,`key``binding`不能出现重复。`template`是你要包裹的模板,里面的`$^`代表原本被选中的文字。
73+
74+
There are a couple of user settings available when you access the plugin settings from Logseq's plugins page. Please refer to the source block above (Default values are given in the source block).
75+
76+
- `wrappings`: Your custom wrappings are defined here. You can extend default wrappings and/or replace/remove them. Please refer to the above configuration for how to define wrappings, `key` and `binding` should be unique, `template` defines how you want the selected text to be wrapped, `$^` represents the selected text.

icon.png

25.5 KB
Loading

package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "logseq-plugin-wrap",
3+
"version": "1.0.0",
4+
"main": "dist/index.html",
5+
"logseq": {
6+
"id": "_sethyuan-logseq-wrap",
7+
"icon": "./icon.png"
8+
},
9+
"scripts": {
10+
"start": "wmr",
11+
"build": "wmr build",
12+
"serve": "wmr serve"
13+
},
14+
"dependencies": {
15+
"@logseq/libs": "^0.0.1-alpha.34"
16+
},
17+
"devDependencies": {
18+
"wmr": "^3.7.2"
19+
}
20+
}

public/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<title>logseq-plugin-wrap</title>
7+
<meta name="viewport" content="width=device-width,initial-scale=1" />
8+
<link rel="icon" href="data:" />
9+
<link rel="modulepreload" as="script" href="index.js" />
10+
</head>
11+
12+
<body>
13+
<script type="module" src="index.js"></script>
14+
</body>
15+
16+
</html>

public/index.js

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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)

tsconfig.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compileOnSave": false,
3+
"compilerOptions": {
4+
"jsx": "preserve",
5+
"jsxFactory": "preact.h",
6+
"jsxFragmentFactory": "preact.Fragment",
7+
"allowJs": true,
8+
"checkJs": true,
9+
// "strict": true,
10+
"noEmit": true,
11+
"moduleResolution": "node",
12+
"target": "ESNext",
13+
"module": "esnext",
14+
"resolveJsonModule": true,
15+
"allowSyntheticDefaultImports": true,
16+
"downlevelIteration": true
17+
},
18+
"include": ["node_modules/wmr/types.d.ts", "**/*"],
19+
"typeAcquisition": {
20+
"enable": true
21+
}
22+
}

wmr.config.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from "wmr"
2+
3+
// Full list of options: https://wmr.dev/docs/configuration
4+
export default defineConfig({
5+
/* Your configuration here */
6+
alias: {
7+
react: "preact/compat",
8+
"react-dom": "preact/compat",
9+
},
10+
publicPath: "",
11+
})

0 commit comments

Comments
 (0)