主要是上传部分,菜单之类的可以自己实现一下
export class picGo extends naive.plugin {
constructor() {
super({name:picGo});
this.初始化();
let 导出按钮设置 = {
按钮提示: "上传到picgo",
按钮图标: "#iconUpload",
按钮文字: "上传到picgo",
回调函数: (event) => {
this.点击上传文档图片(event);
},
};
let 文档按钮设置 = {
块类型: "NodeDocument",
菜单图标: "#iconUpload",
菜单文字: "上传到picgo",
回调函数: () => {
this.id上传文档图片(this.当前文档id);
},
};
let 图片菜单设置 = {
块类型: "image",
菜单图标: "#iconUpload",
菜单文字: "上传到picgo",
回调函数: () => {
this.上传单张图片();
},
};
let 图片菜单设置1 = {
块类型: "image",
菜单图标: "#iconUpload",
菜单文字: "上传到picgo并改变地址",
回调函数: () => {
this.上传单张图片(true);
},
};
this.注册预览按钮(导出按钮设置);
this.注册文档右上角菜单(文档按钮设置);
this.注册图片菜单(图片菜单设置);
this.注册图片菜单(图片菜单设置1);
this.注册文档树菜单(文档按钮设置)
}
async 初始化() {
let {
PicGo,
} = require(`${naive.pathConstructor.pluginsPath()}/picGo/node_modules/picgo`);
this.picGoPath = this.initFolder();
this.picgo = new PicGo(this.picGoPath + "/picgoConfig.json");
let that = this;
this.picgo.on("afterUpload", function (ctx) {
console.log(ctx.output);
ctx.output.forEach((image) => {
console.log(image);
console.log(that);
if (that.previewer) {
that.添加图片(that.previewer, image);
}
});
alert("上传完成"); // [{fileName, width, height, extname, imgUrl}] <- 注意有imgUrl了。
});
}
上传完成回调() {}
添加图片(previewer, image) {
let url = image.imgUrl;
let name = image.fileName;
let imageEls = previewer.querySelectorAll(`img[src='assets/${name}']`);
console.log(imageEls);
if (imageEls[0]) {
imageEls.forEach((el) => {
el.src = url;
});
}
}
async 点击上传文档图片(event) {
event.stopPropagation();
console.log(event.currentTarget.parentElement.parentElement.parentElement);
let id =
event.currentTarget.parentElement.parentElement.previousElementSibling
.firstElementChild.dataset.nodeId;
console.log(id);
this.previewer = event.currentTarget.parentElement.nextElementSibling;
console.log(this.previewer);
let images = await this.kernelApi.获取文档图片附件({ id: id });
console.log(images);
if (images) {
images.forEach((image) => {
if (image.startsWith("assets")) {
let realimage = this.app.workspaceDir + "/data/" + image;
this.picgo.upload([realimage]);
}
});
}
}
async id上传文档图片(id) {
console.log(id);
let images = await this.kernelApi.获取文档图片附件({ id: id });
console.log(images);
if (images) {
images.forEach((image) => {
if (image.startsWith("assets")) {
let realimage = this.app.workspaceDir + "/data/" + image;
this.picgo.upload([realimage]);
}
});
}
}
async 上传单张图片(flag){
let imagePath = (window.siyuan.menus.menu.element.querySelector('[placeholder="图片地址"]')).value+''
if (imagePath.startsWith("assets")) {
let realimage = this.app.workspaceDir + "/data/" + imagePath;
this.picgo.upload([realimage]);
}
if(flag){
this.picgo.on("afterUpload", (ctx)=> {
ctx.output.forEach((image) => {
console.log(image);
(window.siyuan.menus.menu.element.querySelector('[placeholder="图片地址"]')).value=image.imgUrl
let imageEls = document.querySelectorAll(`img[src='${imagePath}']`);
console.log(imageEls)
imageEls.forEach(
el=>el.setAttribute('src',image.imgUrl)
)
});
alert("上传完成"); // [{fileName, width, height, extname, imgUrl}] <- 注意有imgUrl了。
});
}
}
}
export const environments =['APP']
设置界面自己实现一下吧就行了,反正就是改 json
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于