Skip to content

Commit

Permalink
feat: 第一次提交, 实现了 logseq 显示 d2 svg
Browse files Browse the repository at this point in the history
  • Loading branch information
b-yp committed Feb 24, 2023
0 parents commit 77964ab
Show file tree
Hide file tree
Showing 8 changed files with 1,903 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.parcel-cache/

node_modules/
dist/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Logseq d2 plugin
Empty file added index.css
Empty file.
15 changes: 15 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./index.css">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script src="./index.ts" type="module"></script>
</body>
</html>
109 changes: 109 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import '@logseq/libs'

/**
* main entry
*/
async function main() {
logseq.Editor.registerSlashCommand('d2', async () => {

await logseq.Editor.insertAtEditingCursor(`{{renderer :d2_render}}`)

const currentBlock = await logseq.Editor.getCurrentBlock()

if (!currentBlock) {
return
}

await logseq.Editor.insertBlock(
currentBlock.uuid,
'```d2\n```'
)
})

logseq.provideStyle(`
.d2-wrapper {
position: relative;
display: flex;
}
.d2-wrapper > .edit-button {
position: absolute;
top: 10px;
right: 10px;
}
`)

logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => {

const [type] = payload.arguments
if (type !== ':d2_render') {
return
}

// TODO: 如果只是块折叠,直接 return

const blockDetail = await logseq.Editor.getBlock(payload.uuid, { includeChildren: true })
console.log('blockDetail', blockDetail)
const content = blockDetail?.children?.[0]?.content
const contentUuid = blockDetail?.children?.[0]?.uuid

if (!content) {
return
}

const d2Data = content.slice(6, -4)

logseq.provideUI({
key: 'd2',
slot,
template: `加载中...`,
})

/**
* 当开始加载图标的时候折叠代码块
* 为什么不能在接口请求完成之后折叠
* 因为折叠也会触发加载
*/
// logseq.Editor.setBlockCollapsed(payload.uuid, true)

// TODO: 后期优化,当两次请求数据相同时不重新加载
const response = await fetch('https://d2api.fly.dev/getSvg', {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8;'
},
body: JSON.stringify({ diagramCode: d2Data }),
})

const svgCode = await response.text()

logseq.provideModel({
edit() {
if (!contentUuid) {
return
}

console.log('uuid', contentUuid)
logseq.Editor.editBlock(contentUuid)
// logseq.Editor.setBlockCollapsed(payload.uuid, false)
}
})

logseq.provideUI({
key: 'd2',
slot,
template: `
<div class="d2-wrapper">
<button
class="edit-button"
data-on-click="edit"
>编辑</button>
${svgCode}
</div>
`,
})
})
}

// bootstrap
logseq.ready(main).catch(console.error)
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "logseq-d2",
"version": "0.0.1",
"description": "Logseq d2 plugin",
"main": "dist/index.html",
"author": "b-yp",
"license": "MIT",
"targets": {
"main": false
},
"default": "dist/index.html",
"scripts": {
"dev": "parcel ./index.html --public-url ./",
"build": "parcel build --public-url . --no-source-maps index.html"
},
"devDependencies": {
"@logseq/libs": "^0.0.14",
"parcel": "^2.0.0"
},
"logseq": {
"id": "logseq-juzi-qwer",
"icon": "./logo.png"
}
}
Loading

0 comments on commit 77964ab

Please sign in to comment.