Skip to content

Commit

Permalink
1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
likemuuxi committed Oct 13, 2024
1 parent b386eb8 commit 90b9655
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-diagrams-net",
"name": "Diagrams.net",
"version": "1.0.6",
"version": "1.0.7",
"minAppVersion": "0.12.0",
"description": "Enable diagrams.net (previously draw.io) type diagrams, with the diagrams.net embedded editor.",
"author": "Jens M Gleditsch, Muuxi",
Expand Down
58 changes: 40 additions & 18 deletions src/diagrams-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,48 @@ export default class DiagramsView extends Modal {
}

const refreshMarkdownViews = async () => {
// Haven't found a way to refresh the hostView.
// Delete the preceding image link through regular matching! and add it back in the modified content
const editor = this.app.workspace.getActiveViewOfType(MarkdownView).editor;
const cursor = editor.getCursor();
const line = editor.getLine(cursor.line);
const match = line.match(/\[\[.*?\]\]/);
if (!match) return;
const modifiedLine = line.replace(/\!\[\[/, '[[');
editor.replaceRange(modifiedLine, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: line.length });
setTimeout(() => {
const finalLine = modifiedLine.replace(/\[\[/, '![[');
editor.replaceRange(finalLine, { line: cursor.line, ch: 0 }, { line: cursor.line, ch: modifiedLine.length });
}, 200);

setTimeout(() => {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view?.getMode() === "preview") {
// 获取处理前滚动条位置百分比
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
let scrollPosition: any;
if (view.getMode() === "preview") {
scrollPosition = view.previewMode.getScroll();
// refresh the previewView.
setTimeout(() => {
view.previewMode.rerender(true);
}, 100);
} else if (view.getMode() === "source") {
const editView = view.currentMode;
if (editView && typeof editView.getScroll === 'function') {
scrollPosition = editView.getScroll();
} else if (view.editor) {
scrollPosition = view.editor.getScrollInfo();
}
}, 200);
// refresh the editView.
const editor = view.editor;
const content = editor.getValue();
// 第一步:移除所有 ![[]] 中的感叹号
const modifiedContent = content.replace(/!\[\[(.+?)\]\]/g, '[[$1]]');
editor.setValue(modifiedContent);

// 第二步:重新添加感叹号到原本是 ![[]] 的链接
setTimeout(() => {
const finalContent = editor.getValue().replace(/\[\[(.+?)\]\]/g, (match, p1) => {
// 检查原始内容中是否存在 ![[p1]]
return content.includes(`![[${p1}]]`) ? `![[${p1}]]` : `[[${p1}]]`;
});
editor.setValue(finalContent);
// 保持光标位置不变
const cursor = editor.getCursor();
editor.setCursor(cursor);
}, 100);
}

// 处理后滚动条回去
setTimeout(() => {
const editView = view.currentMode;
editView.applyScroll(scrollPosition);
}, 500);
}

const insertDiagram = () => {
Expand Down

0 comments on commit 90b9655

Please sign in to comment.