Skip to content

Commit

Permalink
timer:json and png data not to save until changing
Browse files Browse the repository at this point in the history
  • Loading branch information
calandradas committed Jan 12, 2024
1 parent 4b620f9 commit ae54ade
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Forked from xeden3's joplin-plugin-kity-minder project and made several modifica

* Adjusted part of the UI to make it more in line with operating habits.

* Supports export in JSON, MD, and PNG, and import in JSON and MD.
* Supports export JSON, MD, and PNG, and import JSON and MD.

* i18n now include English, 中文, 日本語, Español, Français and Deutsch.

Expand Down
Binary file modified demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 59 additions & 55 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import joplin from 'api';
import { v4 as uuidv4 } from 'uuid';
import { ContentScriptType, SettingItemType, MenuItem, MenuItemLocation } from 'api/types'
import { ContentScriptType, SettingItemType, MenuItem, MenuItemLocation, DialogResult } from 'api/types'
import { isDiagramResource } from './resources'
import { createDiagramResource, getDiagramResource, updateDiagramResource } from './resources';
import { ToolbarButtonLocation } from 'api/types';
Expand All @@ -9,17 +9,17 @@ import { sep } from 'path'
const fs = joplin.require('fs-extra')

const Config = {
ContentScriptId: 'mindmap-content-script',
ContentScriptId: 'mindmap-content-script',
DiagramsCacheFolder: `${tmpdir}${sep}joplin-mindmap-plugin${sep}`,
}

const CommandsId = {
NewMindmap: 'NewMindmap',
NewMindmap: 'NewMindmap',
}

// 插入markdown语句
function diagramMarkdown(diagramId: string) {
return `![mindmap](:/${diagramId})`
return `![mindmap](:/${diagramId})`
}

// 创建dlg内嵌form
Expand All @@ -34,44 +34,44 @@ function buildDialogHTML(diagramBody: string, language: string): string {
}

function clearDiskCache(): void {
fs.emptyDirSync(Config.DiagramsCacheFolder)
fs.emptyDirSync(Config.DiagramsCacheFolder)
}

joplin.plugins.register({
onStart: async function() {
onStart: async function () {

const app_path = await joplin.plugins.installationDir();

// Clean and create cache folder
clearDiskCache()

// Content Scripts
await joplin.contentScripts.register(
ContentScriptType.MarkdownItPlugin,
Config.ContentScriptId,
'./contentScript/contentScript.js',
)

/**
* Messages handling
*/
await joplin.contentScripts.onMessage(Config.ContentScriptId, async (request: { diagramId: string, action: string }) => {
console.log('contentScripts.onMessage Input:', request)
switch (request.action) {
case 'edit':
// Clean and create cache folder
clearDiskCache()

// Content Scripts
await joplin.contentScripts.register(
ContentScriptType.MarkdownItPlugin,
Config.ContentScriptId,
'./contentScript/contentScript.js',
)

/**
* Messages handling
*/
await joplin.contentScripts.onMessage(Config.ContentScriptId, async (request: { diagramId: string, action: string }) => {
console.log('contentScripts.onMessage Input:', request)
switch (request.action) {
case 'edit':
let diagramResource = await getDiagramResource(request.diagramId);
let data_json = diagramResource.data_json;
data_json = data_json.replace(/\'/g, "\\u0027");
data_json = data_json.replace(/\'/g, "\\u0027");
await open_edit_dlg(data_json, request.diagramId, "edit");
return
case 'check':
return { isValid: await isDiagramResource(request.diagramId) }
default:
return `Invalid action: ${request.action}`
}
})

async function open_edit_dlg(data_json:string, diagramId:string, type:string="addnew"){
return
case 'check':
return { isValid: await isDiagramResource(request.diagramId) }
default:
return `Invalid action: ${request.action}`
}
})

async function open_edit_dlg(data_json: string, diagramId: string, type: string = "addnew") {
let dialogs = joplin.views.dialogs;
let language = await joplin.settings.value('language') as string;
let handle_dlg = await dialogs.create(`myDialog2-${uuidv4()}`);
Expand All @@ -91,12 +91,12 @@ joplin.plugins.register({
if (dialogResult.id === 'ok') {
console.log(dialogResult.formData.main.mindmap_diagram_json);
console.log(dialogResult.formData.main.mindmap_diagram_png);
if(type==="addnew"){
if (type === "addnew") {
let diagramId_new = await createDiagramResource(dialogResult.formData.main.mindmap_diagram_png, dialogResult.formData.main.mindmap_diagram_json)
await joplin.commands.execute('insertText', diagramMarkdown(diagramId_new))
let diagramResource = await getDiagramResource(diagramId_new)
console.log(diagramResource.body);
}else{
} else {
let newDiagramId = await updateDiagramResource(diagramId, dialogResult.formData.main.mindmap_diagram_png, dialogResult.formData.main.mindmap_diagram_json)
let note = await joplin.workspace.selectedNote();
if (note) {
Expand All @@ -108,7 +108,11 @@ joplin.plugins.register({
}
}



//function save_mindmap_data() {
// //alert(dialogResult);
//}

await joplin.settings.registerSettings({
'language': {
value: 'en',
Expand All @@ -128,30 +132,30 @@ joplin.plugins.register({
description: `You can choose the language you need, including English, Chinese, Japanese etc.`
},
});





await joplin.settings.registerSection('settings.kityminder', {
label: 'Kity Minder',
iconName: 'fas fa-brain'
});


// Register command
await joplin.commands.register({
name: CommandsId.NewMindmap,
label: 'New Mindmap',
iconName: 'fas fa-brain',
execute: async () => {
await open_edit_dlg("", null);
},
});

// Register menu
const commandsSubMenu: MenuItem[] = Object.values(CommandsId).map(command => ({ commandName: command }));
await joplin.views.menus.create('menu-kityminder', 'Kityminder Mindmap', commandsSubMenu, MenuItemLocation.Tools);



// Register command
await joplin.commands.register({
name: CommandsId.NewMindmap,
label: 'New Mindmap',
iconName: 'fas fa-brain',
execute: async () => {
await open_edit_dlg("", null);
},
});

// Register menu
const commandsSubMenu: MenuItem[] = Object.values(CommandsId).map(command => ({ commandName: command }));
await joplin.views.menus.create('menu-kityminder', 'Kityminder Mindmap', commandsSubMenu, MenuItemLocation.Tools);

// 通过按键来新增思维导图
await joplin.commands.register({
name: 'addnewMindmap',
Expand Down
22 changes: 15 additions & 7 deletions src/local-kity-minder/diy.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@
// init loading mindmap diagram and the existed mindmap data or create new default mindmap json data
let parent = window.parent.document.getElementById('mindmap_diagram_json');
let data_json, maintopic = _lang_pack[_lang_default]['maintopic'];
let init_data_json = `{"root":{"data":{"id":"cmhllt94xb40","created":1661683403686,"text":"${maintopic}"},"children":[{"data":{"id":"cybyhdvw3qg0","created":1704984272746,"text":"Topic1"},"children":[]},{"data":{"id":"cybyhfxtzd40","created":1704984277217,"text":"Topic2"},"children":[]},{"data":{"id":"cybyhhzf1ew0","created":1704984281667,"text":"Topic3"},"children":[]},{"data":{"id":"cybyhjs9st40","created":1704984285588,"text":"Topic4"},"children":[]}]},"template":"default","theme":"fresh-purple","version":"1.4.33"}`;
if (parent != null && parent.value != "")
data_json = parent.value;
else
data_json = `{"root":{"data":{"id":"cmhllt94xb40","created":1661683403686,"text":"${maintopic}"},"children":[{"data":{"id":"cybyhdvw3qg0","created":1704984272746,"text":"Topic1"},"children":[]},{"data":{"id":"cybyhfxtzd40","created":1704984277217,"text":"Topic2"},"children":[]},{"data":{"id":"cybyhhzf1ew0","created":1704984281667,"text":"Topic3"},"children":[]},{"data":{"id":"cybyhjs9st40","created":1704984285588,"text":"Topic4"},"children":[]}]},"template":"default","theme":"fresh-purple","version":"1.4.33"}`;
data_json = init_data_json;
//loading mindmap data in diagram
editor.minder.importData('json', data_json).then(function (data) {
console.log(data);
Expand Down Expand Up @@ -147,14 +148,21 @@
let opstions = `<option value=\"en\" ${en} >English</option><option value=\"zh_cn\" ${zh_cn} >简体中文</option><option value=\"jp\" ${jp} >日本語</option><option value=\"es\" ${es} >Español</option><option value=\"fr\" ${fr} >Français</option><option value=\"de\" ${de} >Deutsch</option>`;
document.getElementById('selectLang').innerHTML = opstions;

// set a timmer to sync mindmap data to parent diagram to save data per 5s
// set a timmer to sync mindmap data to parent diagram to save data per 1s
setInterval(function () {
editor.minder.exportData('json').then(function (content) {
parent.value = content;
});
editor.minder.exportData('png').then(function (content) {
window.parent.document.getElementById('mindmap_diagram_png').value = content;
editor.minder.exportData('json').then(function (jsoncontent) {
//json and png data not to save until changing
if (jsoncontent != init_data_json && parent.value != jsoncontent)
{
parent.value = jsoncontent;
editor.minder.exportData('png').then(function (pngcontent) {
let parent_png = window.parent.document.getElementById('mindmap_diagram_png');
if (parent_png.value != pngcontent)
parent_png.value = pngcontent;
});
}
});

}, 1000);

// mousewheel scroll zoom in/out
Expand Down

0 comments on commit ae54ade

Please sign in to comment.