Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CodeMirror 6 #10

Merged
merged 6 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions GENERATOR_DOC.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
# generator-joplin
# Plugin development

Scaffolds out a new Joplin plugin
This documentation describes how to create a plugin, and how to work with the plugin builder framework and API.

## Installation

First, install [Yeoman](http://yeoman.io) and generator-joplin using [npm](https://www.npmjs.com/) (we assume you have pre-installed [node.js](https://nodejs.org/)).

```bash
npm install -g yo
npm install -g yo@4.3.1
npm install -g generator-joplin
```

Then generate your new project:

```bash
yo joplin
yo --node-package-manager npm joplin
```

## Development

To test the generator for development purposes, follow the instructions there: https://yeoman.io/authoring/#running-the-generator
This is a template to create a new Joplin plugin.

## Structure

The main two files you will want to look at are:
Expand All @@ -39,6 +34,10 @@ To build the plugin, simply run `npm run dist`.

The project is setup to use TypeScript, although you can change the configuration to use plain JavaScript.

## Updating the manifest version number

You can run `npm run updateVersion` to bump the patch part of the version number, so for example 1.0.3 will become 1.0.4. This script will update both the package.json and manifest.json version numbers so as to keep them in sync.

## Publishing the plugin

To publish the plugin, add it to npmjs.com by running `npm publish`. Later on, a script will pick up your plugin and add it automatically to the Joplin plugin repository as long as the package satisfies these conditions:
Expand Down Expand Up @@ -67,6 +66,13 @@ By default, the compiler (webpack) is going to compile `src/index.ts` only (as w

To get such an external script file to compile, you need to add it to the `extraScripts` array in `plugin.config.json`. The path you add should be relative to /src. For example, if you have a file in "/src/webviews/index.ts", the path should be set to "webviews/index.ts". Once compiled, the file will always be named with a .js extension. So you will get "webviews/index.js" in the plugin package, and that's the path you should use to reference the file.

## More information

- [Joplin Plugin API](https://joplinapp.org/api/references/plugin_api/classes/joplin.html)
- [Joplin Data API](https://joplinapp.org/help/api/references/rest_api)
- [Joplin Plugin Manifest](https://joplinapp.org/api/references/plugin_manifest/)
- Ask for help on the [forum](https://discourse.joplinapp.org/) or our [Discord channel](https://discord.gg/VSj7AFHvpq)

## License

MIT © Laurent Cozic
40 changes: 23 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@
"name": "joplin-plugin-inline-tags",
"version": "1.2.1",
"scripts": {
"dist": "webpack --joplin-plugin-config buildMain && webpack --joplin-plugin-config buildExtraScripts && webpack --joplin-plugin-config createArchive",
"dist": "webpack --env joplin-plugin-config=buildMain && webpack --env joplin-plugin-config=buildExtraScripts && webpack --env joplin-plugin-config=createArchive",
"prepare": "npm run dist",
"update": "npm install -g generator-joplin && yo joplin --update"
"update": "npm install -g generator-joplin && yo joplin --node-package-manager npm --update --force",
"updateVersion": "webpack --env joplin-plugin-config=updateVersion"
},
"license": "MIT",
"keywords": [
"joplin-plugin"
],
"devDependencies": {
"@types/codemirror": "0.0.106",
"@types/node": "^14.0.14",
"@types/node": "^18.7.13",
"chalk": "^4.1.0",
"codemirror": "^5.59.1",
"copy-webpack-plugin": "^6.1.0",
"fs-extra": "^9.0.1",
"glob": "^7.1.6",
"i": "^0.3.6",
"npm": "^6.14.11",
Comment on lines -21 to -22
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i and npm weren't in the original template and seem to be unused. As such, I have removed them here.

Please let me know if they should be added back.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see in git log they were there from the very beginning, not sure why.

"on-build-webpack": "^0.1.0",
"tar": "^6.0.5",
"ts-loader": "^7.0.5",
"typescript": "^3.9.3",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"yargs": "^16.2.0"
}
}
"copy-webpack-plugin": "^11.0.0",
"fs-extra": "^10.1.0",
"glob": "^8.0.3",
"tar": "^6.1.11",
"ts-loader": "^9.3.1",
"typescript": "^4.8.2",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"yargs": "^16.2.0",
"@joplin/lib": "~2.9",
"@codemirror/autocomplete": "6.12.0",
"@codemirror/view": "6.24.1",
"@codemirror/state": "6.4.1",
"@codemirror/language": "6.10.1"
},
"files": [
"publish"
]
}
4 changes: 2 additions & 2 deletions plugin.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extraScripts": [
"InlineTagsPlugin.ts"
"contentScript/index.ts"
]
}
}
113 changes: 0 additions & 113 deletions src/InlineTagsPlugin.ts

This file was deleted.

96 changes: 96 additions & 0 deletions src/contentScript/codeMirror5Plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { PluginContext } from './types';


const codeMirror5Plugin = (context: PluginContext, CodeMirror: any) => {
const buildHints = async (prefix: string) =>{
const {tags, keepText } = await context.postMessage({
command: 'getTags',
});
let hints = [];
for (let i = 0; i < tags.length; i++) {
const tag = tags[i];
if (prefix === '#' || tag.title.startsWith(prefix)) {
hints.push({
text: '#' + tag.title,
displayText: tag.title,
hint: async (cm, data, completion) => {
context.postMessage({
command: 'setTag',
tag: tag,
});
if (!!keepText) {
cm.replaceRange(tag.title, completion.from || data.from, cm.getCursor(), "complete");
} else {
const from = completion.from || data.from;
from.ch -= 1;
cm.replaceRange('', from, cm.getCursor(), "complete");
}
}
});
}
}
if (prefix !== '#') {
const tagExists = tags.findIndex(t => t.title === prefix) > 0;
if (!tagExists) {
hints.push({
text: `Create new tag: ${prefix}`,
hint: (cm, data, completion) => {
context.postMessage({
command: 'newTag',
name: prefix,
});
if (!keepText) {
const from = completion.from || data.from;
from.ch -= 1;
cm.replaceRange('', from, cm.getCursor(), "complete");
}
}
});
}
}
return hints;
}
CodeMirror.defineOption('inlineTags', false, function(cm, value, prev) {
if (!value) return;
cm.on('inputRead', async function (cm1, change) {
if (!cm1.state.completionActive && change.text[0] === '#') {
const content = await cm1.getValue().split('\n');
let count=0;
for(let i=0;i<content.length;i++){
if(content[i].startsWith('```')){
count++;
if(i==change.from.line){
return;
}
}
if(i==change.from.line && count%2){
return;
}
}
const start = {line: change.from.line, ch: change.from.ch + 1};
const hint = function(cm, callback) {
const cursor = cm.getCursor();
const token = cm.getRange(start, cursor);

buildHints(token).then(hints => {
callback({
list: hints,
from: {line: change.from.line, ch: change.from.ch + 1},
to: {line: change.to.line, ch: change.to.ch + 1},
});
});
};

setTimeout(function () {
CodeMirror.showHint(cm, hint, {
completeSingle: false,
closeOnUnfocus: true,
async: true,
});
}, 10);
}
});
});
};

export default codeMirror5Plugin;
Loading