Skip to content

Commit fd80903

Browse files
committed
Work on production version and structure
- this version can build and run production version - code mirror and katex load asynchronously - worked on themes, better solution
1 parent 36cf034 commit fd80903

7 files changed

Lines changed: 87 additions & 74 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "joplin-plugin-katex-input-helper",
33
"version": "2.0.1",
44
"scripts": {
5-
"dist": "webpack --env joplin-plugin-config=buildMain && webpack --env joplin-plugin-config=buildExtraScripts && npm run build-assets && npm run build-docs && webpack --env joplin-plugin-config=createArchive",
5+
"dist": "webpack --env joplin-plugin-config=buildMain && webpack --env joplin-plugin-config=buildExtraScripts && npm run build-assets-prod && npm run build-docs && webpack --env joplin-plugin-config=createArchive",
66
"prepare": "npm run dist",
77
"updateVersion": "webpack --env joplin-plugin-config=updateVersion",
88
"update": "npm install -g generator-joplin && yo joplin --node-package-manager npm --update --force",

src/assets/js/bootLoader.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

2+
/* Could not bring asynchronous version of easyui to work
3+
*/
24
import jquery from './jquery-plugin/jquery.min';
35
import './jquery-easyui/jquery.easyui.min';
46
import './jquery-easyui/datagrid-dnd';
57
import './jquery-easyui/datagrid-filter';
68
import './jquery-easyui/datagrid-cellediting';
7-
import katex from 'katex/dist/katex';
8-
import 'katex/dist/contrib/mhchem';
9-
import CodeMirror from './codemirror/lib/codemirror';
9+
10+
const CodeMirror = (await import('./codemirror/lib/codemirror')).default;
1011

1112
import { Observable } from './patterns/observable';
1213
import { Localizer } from './localization';
@@ -28,6 +29,7 @@ export class BootLoader {
2829

2930
baseLocation = null;
3031
vme = null;
32+
katex = null;
3133

3234
/**
3335
* @abstract Constructor.
@@ -114,6 +116,10 @@ export class BootLoader {
114116
* @async implements the Promise contract
115117
*/
116118
async init1() {
119+
120+
this.katex = await import('katex/dist/katex');
121+
await import('katex/dist/contrib/mhchem');
122+
117123
var counter = 20;
118124
while (!this.presenceCheck(counter) && --counter >= 0) {
119125
await this.setTimeoutAsync(100);
@@ -133,19 +139,26 @@ export class BootLoader {
133139
*/
134140
presenceCheck(cycle) {
135141
var lastChecked = 'Test';
142+
143+
/**
144+
* Checks the classname. Changed to accomodate the minification.
145+
*/
136146
function checkTypeByName(type, name, readableName = name) {
137147
lastChecked = readableName;
138-
if (type === undefined || type === null || typeof type === 'undefined' || !type.prototype) {
148+
if (type === undefined || type === null || (typeof type) === 'undefined' || !type.prototype) {
139149
console.warn(`Undefined type : ${readableName}`);
140150
return false;
141151
}
142152
var detectedName = type.prototype["constructor"]["name"];
143153
var equal = (detectedName === name);
144154
if (!equal) {
145-
console.warn(`Type check failed : ${detectedName} : ${readableName}`);
155+
//console.warn(`Type check failed : ${detectedName} : ${readableName}`);
146156
}
147-
return equal;
157+
return true; // returning test result can lead to problems with minimized versions of code
148158
}
159+
/**
160+
* Checks some type of an object like 'object' or 'function'
161+
*/
149162
function checkOther(type, name, readableName) {
150163
lastChecked = readableName;
151164
var equal = type === name;
@@ -154,10 +167,14 @@ export class BootLoader {
154167
}
155168
return equal;
156169
}
170+
/**
171+
* Checks if katex can execute chemical formula.
172+
* Deactivated, because not reliable.
173+
*/
157174
function mhchemCheck() {
158175
try {
159176
lastChecked = "Mhchem";
160-
katex.renderToString("\\ce{SO4^2- + Ba^2+ -> BaSO4 v} ", { throwOnError: true });
177+
this.katex.renderToString("\\ce{SO4^2- + Ba^2+ -> BaSO4 v} ", { throwOnError: true });
161178
return true;
162179
} catch(e) {
163180
console.warn(`Presence check failed : Mhchem`);
@@ -166,17 +183,17 @@ export class BootLoader {
166183
}
167184

168185
var allLoaded = (
169-
// false &&
170186
checkOther(typeof $, 'function', 'jquery') &&
171187
checkOther(typeof $.messager, 'object', 'easyui') &&
172188
checkOther(typeof $.fn.datagrid, 'function', 'datagrid') &&
173189
checkOther(typeof $.fn.datagrid.defaults, 'object', 'datagrid') &&
174190
checkOther(typeof $.fn.datagrid.defaults.defaultFilterOptions, 'object', 'datagrid-filter') &&
175-
// can we independantly check dnd and cellediting?
176-
checkOther(typeof katex, 'object', 'Katex') &&
177-
checkOther(typeof katex.renderToString, 'function', 'Katex') &&
178-
mhchemCheck() &&
179-
//checkOther(typeof ($.fn.ColorPicker), 'function', 'ColorPicker') &&
191+
// Can we independantly check dnd and cellediting?
192+
checkOther(typeof this.katex, 'object', 'Katex') &&
193+
checkOther(typeof this.katex.renderToString, 'function', 'Katex') &&
194+
// Can we do this check?
195+
//mhchemCheck() &&
196+
180197
checkTypeByName(CodeMirror, 'CodeMirror', 'CodeMirror') &&
181198

182199
checkTypeByName(Observable, 'Observable') &&
@@ -189,6 +206,7 @@ export class BootLoader {
189206
checkTypeByName(CategoriesTree, 'CategoriesTree') &&
190207
checkTypeByName(DynamicPanel, 'DynamicPanel') &&
191208
checkTypeByName(KatexInputHelper, 'KatexInputHelper'));
209+
192210
if (! allLoaded && cycle <= 0) {
193211
throw Error(`${lastChecked} not loaded`);
194212
}

src/assets/js/dialog.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import './dialog.css' assert { type: 'css' };
22

3+
const CodeMirror = (await import('./codemirror/lib/codemirror')).default;
4+
await import('./codemirror/mode/stex/stex'); // manual recommendation
5+
36
import { VKI_init } from './keyboard/keyboard';
4-
import CodeMirror from './codemirror/lib/codemirror';
5-
import './codemirror/mode/stex/stex'; // manual recommendation
67
import { ParametersProxy } from "./parameters";
78
import { Localizer } from './localization';
89
import { Themes } from './themes';
@@ -1441,7 +1442,7 @@ export class KatexInputHelper {
14411442
* This method is only called once in constructor.
14421443
*/
14431444
setBaseLocation() {
1444-
var bundlePath = $("script[src]")
1445+
var bundlePath = $("script[src$='main.js']")
14451446
.last()
14461447
.attr("src")
14471448
.split('/')

src/assets/js/math.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import katex from 'katex/dist/katex';
21
import { Messager } from './helpers';
3-
2+
const katex = await import('katex/dist/katex');
3+
await import('katex/dist/contrib/mhchem');
44

55
/**
66
* Class responsible for Math Formula handling.

src/assets/js/themes.js

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class Themes extends Observable {
99
dir = "";
1010
cssActive = false;
1111
activeTheme = "";
12+
supportedThemes = [ 'aguas', 'gray', 'black', 'bootstrap', 'metro' ];
1213

1314
/**
1415
* @abstract Constructor.
@@ -17,19 +18,6 @@ export class Themes extends Observable {
1718
super();
1819
}
1920

20-
/**
21-
* @abstract Appends attributes of one or more elements to another.
22-
*/
23-
universalLoad(where, what) {
24-
for (var entry of what) {
25-
$(where)
26-
.append(
27-
$(entry.element)
28-
.attr(entry.attributes)
29-
);
30-
}
31-
}
32-
3321
/**
3422
* @abstract Initializes the theme choice.
3523
*
@@ -66,39 +54,25 @@ export class Themes extends Observable {
6654

6755
this.activeTheme = activeTheme;
6856
var colorType = '';
69-
var opts = { with: { type: 'css' } };
70-
this.disableThemeLinks();
7157

72-
// TRYING alternative way
73-
// WORKS and loads all present themes!!
74-
var mainTheme = activeTheme;
75-
if (activeTheme == 'aguas') { mainTheme = 'default'; }
7658
if (activeTheme == 'black') { colorType = 'black'; }
77-
await import(`./jquery-easyui/themes/${mainTheme}/easyui.css`, opts);
78-
await import(`./jquery-easyui-MathEditorExtend/themes/${activeTheme}/easyui.css`, opts);
7959

80-
this.enableThemeLinks(activeTheme);
81-
this.notify(activeTheme, this.dir, colorType);
82-
}
83-
84-
/**
85-
* @abstract Disables link tags to the theme resources.
86-
*
87-
* The hope was this could enforce a new load of themes every time one selects any.
88-
* Yes, but it must be combined with *enableThemeLinks*.
89-
*/
90-
disableThemeLinks() {
91-
$('link[href$="easyui_css.styles.css"]').attr('disabled', true);
92-
}
93-
94-
/**
95-
* @abstract Enables css link tags again after disabling them.
96-
*/
97-
enableThemeLinks(theme) {
98-
$(`link[href$="${theme}_easyui_css.styles.css"]`).removeAttr('disabled');
99-
if (theme == 'aguas') {
100-
$(`link[href$="default_easyui_css.styles.css"]`).removeAttr('disabled'); // ?
60+
for (const theme of this.supportedThemes) {
61+
if (theme === 'aguas') { continue; }
62+
63+
/*
64+
if (theme == activeTheme) {
65+
$(`#${theme}`).removeAttr('disabled');
66+
$(`#${theme}-extend`).removeAttr('disabled');
67+
} else {
68+
$(`#${theme}`).attr('disabled', true);
69+
$(`#${theme}-extend`).attr('disabled', true);
70+
}
71+
*/
72+
$(`#${theme}, #${theme}-extend`).attr('disabled', theme !== activeTheme);
10173
}
74+
75+
this.notify(activeTheme, this.dir, colorType);
10276
}
10377

10478
/**
@@ -121,8 +95,21 @@ export class Themes extends Observable {
12195
.attr('id', 'RTLstyle')
12296
.attr('disabled', true);
12397

124-
this.setRTLstyle(dir);
98+
for (const theme of this.supportedThemes) {
99+
if (theme === 'aguas') { continue; }
100+
101+
await import(`./jquery-easyui/themes/${theme}/easyui.css`, opts);
102+
$('link').last()
103+
.attr('id', theme);
104+
105+
await import(`./jquery-easyui-MathEditorExtend/themes/${theme}/easyui.css`, opts);
106+
$('link').last()
107+
.attr('id', `${theme}-extend`);
108+
}
109+
110+
this.activateStyle(activeTheme);
125111
this.cssActive = true;
112+
this.setRTLstyle(dir);
126113
}
127114

128115
/**
@@ -139,12 +126,14 @@ export class Themes extends Observable {
139126
this.dir = dir;
140127
if (this.cssActive) {
141128
console.info(`Html Dir is: ${dir}`);
142-
var disabled = document.getElementById("RTLstyle").disabled;
143-
var newDisabled = dir !== 'rtl'; // suppress unnecessary changes => measure did not help
144-
if (newDisabled != disabled) {
145-
document.getElementById("RTLstyle").disabled = newDisabled;
146-
console.info(`RTL style set to: ${newDisabled}`);
129+
/*
130+
if (dir === 'rtl') {
131+
$("#RTLstyle").removeAttr('disabled');
132+
} else {
133+
$("#RTLstyle").attr('disabled', true);
147134
}
135+
*/
136+
$("#RTLstyle").attr('disabled', dir !== 'rtl');
148137
}
149138
}
150139
}

webpack.assets.config.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default (env) => {
1818
new CssMinimizerPlugin(),
1919
],
2020
minimize: true,
21-
/*
21+
/**/
2222
splitChunks: {
2323
chunks: 'async',
2424
minSize: 20000,
@@ -28,31 +28,34 @@ export default (env) => {
2828
maxInitialRequests: 30,
2929
enforceSizeThreshold: 50000,
3030
cacheGroups: {
31+
/*
3132
commons: {
3233
name: 'commons',
3334
chunks: 'async',
3435
minChunks: 2,
35-
},
36+
},*/
3637
vendors: {
3738
test: /[\\/]node_modules[\\/]/,
3839
priority: -10,
3940
reuseExistingChunk: true,
40-
filename: 'js/vendors/[name].js', // no action
41+
filename: 'js/vendors/[name].js',
4142
chunks: 'async',
4243
},
44+
// ACTION: API only copied, generates extra entry in index.html
4345
easyui: {
4446
test: /src[\\/]assets[\\/]js[\\/]jquery-easyui/,
4547
priority: -15,
4648
filename: 'js/easyui/[name].js', // no action
47-
chunks: 'all',
49+
chunks: 'async',
4850
},
51+
/*
4952
default: {
5053
minChunks: 2,
5154
priority: -20,
5255
reuseExistingChunk: true,
53-
},
56+
},*/
5457
},
55-
}*/
58+
}
5659
},
5760
cache: false,
5861
context: path.resolve(path.dirname('.'), '.'),
@@ -64,6 +67,7 @@ export default (env) => {
6467
},
6568
plugins: [
6669

70+
//new webpack.optimize.ModuleConcatenationPlugin(),
6771
new TerserPlugin(),
6872
new MiniCssExtractPlugin({
6973
filename: '[name].css',

webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ const pluginConfig = { ...baseConfig, entry: './src/index.ts',
206206
new CopyPlugin({
207207
patterns: [
208208
{
209-
from: '**/*',
209+
from: '*',
210210
context: path.resolve(__dirname, 'src'),
211211
to: path.resolve(__dirname, 'dist'),
212212
globOptions: {
@@ -215,6 +215,7 @@ const pluginConfig = { ...baseConfig, entry: './src/index.ts',
215215
// already copied into /dist so we don't copy them.
216216
'**/*.ts',
217217
'**/*.tsx',
218+
'assets/**/*'
218219
],
219220
},
220221
},

0 commit comments

Comments
 (0)