Created
March 25, 2021 16:35
-
-
Save mablin7/b8ea9a8512533a661ab05c15aaf3f95c to your computer and use it in GitHub Desktop.
Joplin quick notes - proof of concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sendCMStyle (ctx) { | |
setTimeout(() => { | |
const cmStyle = document.getElementById('codemirrorStyle').innerHTML | |
ctx.postMessage({ | |
type: 'cmStyle', | |
cmStyle | |
}) | |
}, 10) | |
} | |
module.exports = { | |
default: function(ctx) { | |
return { | |
plugin (CodeMirror) { | |
// sendCMStyle(ctx) | |
console.log('PLUGIN SCRIPT', CodeMirror) | |
CodeMirror.defineInitHook(function(cm, value, prev) { | |
console.warn('plugin script ran') | |
cm.on('inputRead', function (cm1, change) { | |
console.log('CONTENT SCRIPT: on input', change) | |
if (change.text[0] === 'x') { | |
throw new Error('ERROR FROM CONTENT SCRIPT') | |
} | |
}) | |
}); | |
} | |
} | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import joplin from 'api'; | |
import { ContentScriptType } from 'api/types'; | |
window.onload = function () { | |
const linkElem = document.createElement('link') | |
linkElem.href = '../../node_modules/codemirror/lib/codemirror.css' | |
linkElem.rel = 'stylesheet' | |
document.body.appendChild(linkElem) | |
let div = document.createElement('div') | |
div.id = 'root' | |
document.body.appendChild(div) | |
const script = document.createElement('script') | |
script.innerHTML = ` | |
const _settings = { | |
'editor.spellcheckBeta': false, | |
'markdown.plugin.katex': true, | |
'markdown.plugin.emoji': true | |
} | |
const patchedSetting = { | |
default: { | |
value (key) { | |
console.log('Settings accessing key: ', key) | |
return _settings[key] | |
} | |
}, | |
__esModule: true | |
} | |
const Module = require('module') | |
const _req = Module.prototype.require; | |
Module.prototype.require = function (path) { | |
if (path.endsWith('Setting')) return patchedSetting | |
return _req.apply(this, arguments); | |
}; | |
const { render } = require('react-dom'.toLowerCase()) | |
const React = require('react'.toLowerCase()) | |
const Editor = require('../../gui/NoteEditor/NoteBody/CodeMirror/Editor').default | |
const KeymapService = require('@joplin/lib/services/KeymapService').default | |
KeymapService.instance().initialize() | |
function Popup() { | |
const editorProps = { | |
value: 'hello', | |
"searchMarkers": { | |
"keywords": [], | |
"options": { | |
"searchTimestamp": 0, | |
"selectedIndex": 0, | |
"separateWordSearch": false | |
} | |
}, | |
"mode": "joplin-markdown", | |
"codeMirrorTheme": "default", | |
"style": { | |
"display": "flex", | |
"width": "auto", | |
"height": "auto", | |
"flex": 1, | |
"overflowY": "hidden", | |
"paddingTop": 0, | |
"lineHeight": "18px", | |
"fontSize": "13px", | |
"color": "#32373F", | |
"backgroundColor": "#ffffff", | |
"codeMirrorTheme": "default" | |
}, | |
"readOnly": false, | |
"autoMatchBraces": true, | |
"keyMap": "", | |
"plugins": {} | |
} | |
const editor = React.createElement(Editor, editorProps, null) | |
return editor | |
} | |
const popup = React.createElement(Popup, null, null) | |
render(popup, document.getElementById('root')) | |
` | |
document.body.appendChild(script) | |
} | |
function addCMStyle(style) { | |
const styleElem = document.createElement('style') | |
styleElem.innerHTML = style | |
document.head.appendChild(styleElem) | |
} | |
// let windowProxy | |
joplin.plugins.register({ | |
onStart: async function() { | |
await joplin.contentScripts.register( | |
ContentScriptType.CodeMirrorPlugin, | |
'testPlugin', | |
'./testPlugin.js' | |
) | |
await joplin.contentScripts.onMessage('testPlugin', msg => { | |
const { type } = msg | |
if (type === 'cmStyle') addCMStyle(msg.cmStyle) | |
}); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment