Skip to content

Commit

Permalink
Split code into plugin & webview, convert webview to TS and build wit…
Browse files Browse the repository at this point in the history
…h webpack
  • Loading branch information
roman-r-m committed Feb 6, 2021
1 parent 67d19f7 commit d2f4d2d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "joplin-plugin-resource-search",
"version": "1.0.0",
"scripts": {
"dist": "webpack --joplin-plugin-config buildMain && webpack --joplin-plugin-config buildExtraScripts && webpack --joplin-plugin-config createArchive",
"dist": "webpack --joplin-plugin-config buildMain && webpack --joplin-plugin-config webview && webpack --joplin-plugin-config createArchive",
"prepare": "npm run dist"
},
"license": "MIT",
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions src/index.ts → src/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import joplin from 'api';
import JoplinData from 'api/JoplinData';
import { MenuItemLocation, ViewHandle } from 'api/types';
import { pdfToText} from './index/pdf';
import { MenuItemLocation } from 'api/types';
import { pdfToText} from '../index/pdf';
import { addToIndex, initDb, query } from './database';
import { Database } from 'sqlite3';

Expand Down Expand Up @@ -84,9 +84,9 @@ joplin.plugins.register({

await joplin.commands.register({
name: 'searchAttachments',
label: 'Search in Attachments',
label: 'Search in attachments',
execute: async () => {
joplin.views.dialogs.open(resourceSearch);
await joplin.views.dialogs.open(resourceSearch);
},
})
await joplin.views.menuItems.create('Search in attachments', 'searchAttachments', MenuItemLocation.Edit);
Expand Down
21 changes: 0 additions & 21 deletions src/settings.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/resource-search-view.js → src/webview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ function debounce(func, timeout = 300) {
timer = setTimeout(() => { func.apply(this, args); }, timeout);
};
}

declare const webviewApi: any;

const getResources = debounce(async query => {
const results = await webviewApi.postMessage({
type: 'search',
Expand Down Expand Up @@ -42,7 +45,7 @@ const getResources = debounce(async query => {
}
});

const queryInput = document.getElementById('query-input');
const queryInput = document.getElementById('query-input') as HTMLInputElement;
queryInput.addEventListener('input', e => {
e.preventDefault();
console.log(JSON.stringify(e));
Expand Down
File renamed without changes.
36 changes: 35 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const baseConfig = {
};

const pluginConfig = Object.assign({}, baseConfig, {
entry: './src/index.ts',
entry: './src/plugin/index.ts',
resolve: {
alias: {
api: path.resolve(__dirname, 'api'),
Expand All @@ -158,6 +158,7 @@ const pluginConfig = Object.assign({}, baseConfig, {
// already copied into /dist so we don't copy them.
'**/*.ts',
'**/*.tsx',
'**/webview/**',
],
},
},
Expand Down Expand Up @@ -227,6 +228,37 @@ function buildExtraScriptConfigs(userConfig) {
return output;
}

const webviewConfig = Object.assign({}, baseConfig, {
entry: './src/webview/index.ts',
target: 'web',
output: {
filename: 'resource-search-view.js',
path: distDir,
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
plugins: [
new CopyPlugin({
patterns: [
{
from: '**/*',
context: path.resolve(__dirname, 'src/webview'),
to: path.resolve(__dirname, 'dist'),
globOptions: {
ignore: [
// All TypeScript files are compiled to JS and
// already copied into /dist so we don't copy them.
'**/*.ts',
'**/*.tsx',
],
},
},
],
}),
]
});

function main(processArgv) {
const yargs = require('yargs/yargs');
const argv = yargs(processArgv).argv;
Expand Down Expand Up @@ -256,6 +288,8 @@ function main(processArgv) {
// exist and output in the publish dir. Then the plugin will delete this
// temporary file before packaging the plugin.
createArchive: [createArchiveConfig],

webview: [webviewConfig],
};

// If we are running the first config step, we clean up and create the build
Expand Down

0 comments on commit d2f4d2d

Please sign in to comment.