Skip to content

Commit

Permalink
feat: 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrijk committed Sep 19, 2021
0 parents commit 600ab64
Show file tree
Hide file tree
Showing 15 changed files with 9,061 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This is a basic workflow to help you get started with Actions

name: Releases

# Controls when the action will run.
on:
push:
branches:
- "master"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
release:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
- uses: mskelton/setup-yarn@v1
- run: yarn
- run: yarn build
- uses: montudor/action-zip@v1
with:
args: zip -qq -r logseq-plugin-block-to-page.zip dist demo README.md package.json LICENSE
- name: Release
run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.parcel-cache
dist
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 2,
"useTabs": false
}
21 changes: 21 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"branches": "master",
"plugins": [
[
"@semantic-release/git",
{
"assets": [
"package.json",
"CHANGELOG.md"
],
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}
],
[
"@semantic-release/github",
{
"assets": "logseq-plugin-block-to-page.zip"
}
]
]
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 hyrijk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Trun block into page

## Slash command

![slash command](./demo/slash-command.gif)

## Block context menu

![block context menu](./demo/context-menu.gif)
Binary file added demo/context-menu.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/slash-command.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/slash.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Split block</title>
</head>
<body>
<script src="index.ts"></script>
</body>
</html>
116 changes: 116 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import "@logseq/libs";
import {
BlockEntity,
BlockIdentity,
IBatchBlock,
} from "@logseq/libs/dist/LSPlugin.user";

async function main(blockId: string) {
const block = await logseq.Editor.getBlock(blockId, {
includeChildren: true,
});
if (block === null) {
return;
}

const pageRegx = /^\[\[(.*)\]\]$/;
const pageName = block.content.replace(pageRegx, "$1");
await createPageIfNotExist(pageName);

const srcBlock = await getLastBlock(pageName);
if (srcBlock) {
// page.format 为空
if (srcBlock.format !== block.format) {
logseq.App.showMsg("page format not same", "warning");
return Promise.reject("page format not same");
}

await removeBlocks(block.children as BlockEntity[]);
if (!pageRegx.test(block.content)) {
await logseq.Editor.updateBlock(block.uuid, `[[${block.content}]]`);
}
await insertBatchBlock(srcBlock.uuid, block.children as BlockEntity[]);

if (srcBlock.content === "") {
// insertBatchBlock before 参数无效
await logseq.Editor.removeBlock(srcBlock.uuid);
}

await logseq.Editor.exitEditingMode();
}
}

logseq
.ready(() => {
logseq.Editor.registerSlashCommand("Turn Into Page", async (e) => {
main(e.uuid);
});
logseq.Editor.registerBlockContextMenuItem("Turn into page", async (e) => {
main(e.uuid);
});
})
.catch(console.error);

async function insertBatchBlock(
srcBlock: BlockIdentity,
blocks: BlockEntity[]
) {
// children: [] 会出错
const batchBlocks = blocks.map((c) => ({
content: c.content,
children: c.children?.length ? (c.children as IBatchBlock[]) : undefined,
}));

debug("insertBatchBlock", srcBlock, batchBlocks);
await logseq.Editor.insertBatchBlock(srcBlock, batchBlocks, {
sibling: true,
});
}

async function createPageIfNotExist(pageName: string) {
let page = await logseq.Editor.getPage(pageName);
if (!page) {
await logseq.Editor.createPage(
pageName,
{},
{
createFirstBlock: true,
redirect: false,
}
);
} else {
debug("page already exist");
const lastBlock = await getLastBlock(pageName);
if (lastBlock === null) {
// 无法往空页面写入 block
await logseq.Editor.deletePage(pageName);
await logseq.Editor.createPage(
pageName,
{},
{
createFirstBlock: true,
redirect: false,
}
);
}
}
}

async function removeBlocks(blocks: BlockEntity[]) {
for (let i = 0; i < blocks.length; i++) {
const child = blocks[i];
await logseq.Editor.removeBlock(child.uuid);
}
}

async function getLastBlock(pageName: string): Promise<null | BlockEntity> {
const blocks = await logseq.Editor.getPageBlocksTree(pageName);
if (blocks.length === 0) {
return null;
}
return blocks[blocks.length - 1];
}

function debug(...args: any) {
console.debug("block-to-page", ...args);
}
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "logseq-plugin-block-to-page",
"version": "0.0.1",
"description": "turn block into page",
"main": "dist/index.html",
"scripts": {
"dev": "parcel ./index.html --public-url ./",
"build": "parcel build --public-url . --no-source-maps index.html"
},
"keywords": [
"logseq"
],
"author": "hyrijk",
"devDependencies": {
"@semantic-release/git": "^9.0.0",
"@semantic-release/github": "^7.2.3",
"parcel": "2.0.0-beta.2",
"semantic-release": "^17.4.7",
"typescript": "^4.4.3"
},
"dependencies": {
"@logseq/libs": "^0.0.1-alpha.27"
},
"logseq": {
"id": "_olkce22ke"
}
}
5 changes: 5 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"strictNullChecks": true
}
}
Loading

0 comments on commit 600ab64

Please sign in to comment.