Skip to content

Commit

Permalink
fix: Wrong graph path when working with multi-graph.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjaychen1e committed Sep 18, 2022
1 parent 3dad10e commit de79b93
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logseq-plugin-image-uploader",
"version": "0.0.11",
"version": "0.0.12",
"main": "dist/index.html",
"scripts": {
"dev": "vite",
Expand Down
94 changes: 51 additions & 43 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,69 @@ import "@logseq/libs";
import { checkUpdates } from "./CheckUpdatesUtils";
import { checkAndUploadBlock } from "./ImageUploadUtils";

async function main() {
setTimeout(() => {
checkUpdates();
}, 5000);

async function getGraphPath() {
const graphInfo = await logseq.App.getCurrentGraph();
const graphPath = graphInfo?.path;
if (!graphPath) {
const errorMsg = "Failed to get graph root path.";
console.error('Error:', errorMsg);
console.error("Error:", errorMsg);
logseq.App.showMsg(errorMsg, "error");
return
} else {
// Adding a context menu item to upload images in the block.
logseq.Editor.registerBlockContextMenuItem("Upload image", async (e) => {
const block = await logseq.Editor.getBlock(e.uuid);
if (block && block.content) {
return;
}
return graphPath;
}

async function main() {
setTimeout(() => {
checkUpdates();
}, 5000);

// Adding a context menu item to upload images in the block.
logseq.Editor.registerBlockContextMenuItem("Upload image", async (e) => {
const block = await logseq.Editor.getBlock(e.uuid);
if (block && block.content) {
const graphPath = await getGraphPath();
if (graphPath) {
checkAndUploadBlock(block, graphPath);
}
})
}
});

// Adding pasting event listener to upload images in the block.
// Note: Paste event won't trigger when pastring an image, so we're using the keydown event instead.
parent.document.addEventListener("keydown", async (e) => {
let autoUploading = logseq.settings?.autoUploading ?? true;
if (!autoUploading) {
return
}

if (e.ctrlKey || e.metaKey) {
if (e.code === "KeyV") {
let currentBlock = await logseq.Editor.getCurrentBlock();
if (currentBlock) {
let uuid = currentBlock.uuid;
let checkLater = function () {
setTimeout(async () => {
let isEditing = await logseq.Editor.checkEditing();
if (typeof isEditing === "string" && isEditing === uuid) {
// logseq.App.showMsg("Still editing, check it later.", "warning");
checkLater();
} else {
let block = await logseq.Editor.getBlock(uuid);
if (block && block.content) {
// logseq.App.showMsg(block.content);
// Adding pasting event listener to upload images in the block.
// Note: Paste event won't trigger when pastring an image, so we're using the keydown event instead.
parent.document.addEventListener("keydown", async (e) => {
let autoUploading = logseq.settings?.autoUploading ?? true;
if (!autoUploading) {
return;
}

if (e.ctrlKey || e.metaKey) {
if (e.code === "KeyV") {
let currentBlock = await logseq.Editor.getCurrentBlock();
if (currentBlock) {
let uuid = currentBlock.uuid;
let checkLater = function () {
setTimeout(async () => {
let isEditing = await logseq.Editor.checkEditing();
if (typeof isEditing === "string" && isEditing === uuid) {
// logseq.App.showMsg("Still editing, check it later.", "warning");
checkLater();
} else {
let block = await logseq.Editor.getBlock(uuid);
if (block && block.content) {
const graphPath = await getGraphPath();
if (graphPath) {
checkAndUploadBlock(block, graphPath);
}
}
}, 1000);
}
checkLater();
}
}
}, 1000);
};
checkLater();
}
}
});
}
}
});
}

logseq.ready(main).catch(console.error);
logseq.ready(main).catch(console.error);

0 comments on commit de79b93

Please sign in to comment.