diff --git a/src/classShape.ts b/src/ClassShape.ts similarity index 98% rename from src/classShape.ts rename to src/ClassShape.ts index dfe906d..63f9615 100644 --- a/src/classShape.ts +++ b/src/ClassShape.ts @@ -1,7 +1,7 @@ import { Graph } from '@antv/x6'; -import { utils } from './utils' +import { Utils } from './Utils' -const myUtils = new utils() +const myUtils = new Utils() /** * 註冊 類聲明 中 方法名稱位置 計算函數 diff --git a/src/codeBlockShape.ts b/src/CodeBlockShape.ts similarity index 96% rename from src/codeBlockShape.ts rename to src/CodeBlockShape.ts index 69ff653..9c14e69 100644 --- a/src/codeBlockShape.ts +++ b/src/CodeBlockShape.ts @@ -1,7 +1,7 @@ import hljs from 'highlight.js'; -import { utils, CodeData } from './utils' +import { Utils, CodeData } from './Utils' -const myUtils = new utils() +const myUtils = new Utils() class CodeBlockShape { diff --git a/src/edge.ts b/src/Edge.ts similarity index 97% rename from src/edge.ts rename to src/Edge.ts index 182c681..28c677c 100644 --- a/src/edge.ts +++ b/src/Edge.ts @@ -1,7 +1,7 @@ import { Shape } from '@antv/x6'; -import { CodeData, utils } from './utils' +import { CodeData, Utils } from './Utils' -const myUtils = new utils() +const myUtils = new Utils() class Edge { @@ -104,4 +104,4 @@ class Edge { } -export { Edge } \ No newline at end of file +export { Edge } diff --git a/src/index.ts b/src/Graph.ts similarity index 56% rename from src/index.ts rename to src/Graph.ts index 25713ca..e22838e 100644 --- a/src/index.ts +++ b/src/Graph.ts @@ -1,92 +1,15 @@ import {Cell, Graph, Shape} from '@antv/x6'; -import {CodeBlockShape} from './codeBlockShape'; -import {ClassShape} from './classShape'; -import {CLASS_SHAPE_ID_TAG, CODE_BLOCK_ID_TAG, CodeData, utils} from './utils'; -import {Edge} from './edge' +import {CodeBlockShape} from './CodeBlockShape'; +import {ClassShape} from './ClassShape'; +import {CLASS_SHAPE_ID_TAG, CODE_BLOCK_ID_TAG, CodeData, Utils} from './Utils'; +import {Edge} from './Edge' -let codeBlocks = [` - /** - * listen plugin handler event and handle plugin. - * @class ShenyuWebHandler - * @function onApplicationEvent(final PluginHandlerEvent event) - * @param event sort plugin event - */ - @Override - public void onApplicationEvent(final PluginHandlerEvent event) { - PluginHandlerEventEnum stateEnums = event.getPluginStateEnums(); - PluginData pluginData = (PluginData) event.getSource(); - switch (stateEnums) { - case ENABLED: - onPluginEnabled(pluginData); - break; - case DELETE: - case DISABLED: - // disable or removed plugin. - onPluginRemoved(pluginData); - break; - case SORTED: - // copy a new one, or there will be concurrency problems - onSortedPlugins(); - break; - default: - throw new IllegalStateException("Unexpected value: " + event.getPluginStateEnums()); - } - onSortedPlugins(); - } - `, - ` - /** - * handler error. - * - * @class ShenyuWebHandler - * @function handle(@NonNull final ServerWebExchange exchange, @NonNull final Throwable throwable) - * @call ShenyuPluginLoader @ getInstance() - * - * @param exchange the exchange - * @param throwable the throwable - * @return error result - */ - @Override - @NonNull - public Mono handle(@NonNull final ServerWebExchange exchange, @NonNull final Throwable throwable) { - LOG.error("handle error: {}{}", exchange.getLogPrefix(), formatError(throwable, exchange.getRequest()), throwable); - HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; - if (throwable instanceof ResponseStatusException) { - httpStatus = ((ResponseStatusException) throwable).getStatus(); - } - exchange.getResponse().setStatusCode(httpStatus); - Object error = ShenyuResultWrap.error(exchange, httpStatus.value(), httpStatus.getReasonPhrase(), throwable); - return WebFluxResultUtils.result(exchange, error); - } - `, - ` - /** - * Get plugin loader instance. - * - * @class ShenyuPluginLoader - * @function getInstance() - * - * @return plugin loader instance - */ - public static ShenyuPluginLoader getInstance() { - if (null == pluginLoader) { - synchronized (ShenyuPluginLoader.class) { - if (null == pluginLoader) { - pluginLoader = new ShenyuPluginLoader(); - } - } - } - return pluginLoader; - } - `] - -const myUtils = new utils() +const myUtils = new Utils() const codeBlockShape = new CodeBlockShape() const classShape = new ClassShape() const edge = new Edge() -// window.onload = function () { -export let indexInit = function (codeBlocks: String[]) { +export let initGraph = function (codeBlocks: String[]) { // 創建畫布 const graph = new Graph({ @@ -132,9 +55,6 @@ export let indexInit = function (codeBlocks: String[]) { // 綁定 開啟/關閉 代碼塊圖形 事件 graph.on('toggle:codeBlock', ({e, node}) => { - console.dir(e) - console.dir(node) - // 獲取 代碼塊圖形、開關文字 對象 let codeBlock = graph.getCellById(e.currentTarget.getAttribute('target-code-block')) let toggleText = e.currentTarget.parentNode.childNodes[3].children[0] diff --git a/src/source-code.ts b/src/SourceCodeView.ts similarity index 90% rename from src/source-code.ts rename to src/SourceCodeView.ts index d5dc77a..3dcfce2 100644 --- a/src/source-code.ts +++ b/src/SourceCodeView.ts @@ -1,5 +1,5 @@ import {ItemView, View, WorkspaceLeaf} from "obsidian"; -import {indexInit} from "./index"; +import {initGraph} from "./Graph"; import {SOURCE_CODE_VIEW_TYPE} from "./main"; export default class SourceCodeView extends ItemView { @@ -26,7 +26,7 @@ export default class SourceCodeView extends ItemView { div.id = 'container'; this.containerEl.children[1].appendChild(div); - indexInit(this.codeBlocks); + initGraph(this.codeBlocks); } async onClose() { diff --git a/src/utils.ts b/src/Utils.ts similarity index 98% rename from src/utils.ts rename to src/Utils.ts index d2953b8..ad84d50 100644 --- a/src/utils.ts +++ b/src/Utils.ts @@ -16,7 +16,7 @@ type CodeDataFuncCall = { export const CLASS_SHAPE_ID_TAG: string = "class-"; export const CODE_BLOCK_ID_TAG: string = 'code-block-'; -class utils { +class Utils { /** * 計算字串寬度 @@ -164,4 +164,4 @@ class utils { } -export { utils, CodeData, CodeDataFunc } +export { Utils, CodeData, CodeDataFunc } diff --git a/src/main.ts b/src/main.ts index eff152b..697645b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,26 +1,34 @@ import {Plugin, Workspace, WorkspaceLeaf} from 'obsidian'; -import SourceCodeView from "./source-code"; +import SourceCodeView from "./SourceCodeView"; export const SOURCE_CODE_VIEW_TYPE = 'source-code-view' -export default class MyPlugin extends Plugin { +export default class SourceCodeViewPlugin extends Plugin { - sourceCodeView: SourceCodeView + // Obsidian 中 Markdown 的 代碼塊內容 集合 codeBlocks: string[] = [] + /** + * 載入插件時,註冊相關組件 + */ async onload() { + + // 註冊 Obsidian 的 View this.registerView( SOURCE_CODE_VIEW_TYPE, - (leaf: WorkspaceLeaf) => - (this.sourceCodeView = new SourceCodeView(this.codeBlocks, leaf)) + (leaf: WorkspaceLeaf) => (new SourceCodeView(this.codeBlocks, leaf)) ); + // 註冊 Obsidian 左側的按鈕 this.addRibbonIcon("dice", "Activate view", () => { - this.initCanvas() + this.initView() }); } - initCanvas() { + /** + * 初始化 Obsidian 的 View 頁面 + */ + initView() { // 當 ResourceCodeView 頁面已開啟,則返回 if (this.app.workspace.getLeavesOfType(SOURCE_CODE_VIEW_TYPE).length > 0) {