From dd6d589187bfbac0f1059a6659d1d2a09afc0e3f Mon Sep 17 00:00:00 2001 From: Valera Date: Wed, 11 Dec 2019 22:36:23 +0300 Subject: [PATCH] Try create extra plugins --- index.d.ts | 2 ++ make.js | 5 +++++ package.json | 2 +- plugins/emoji/emoji.less | 3 +++ plugins/emoji/emoji.ts | 24 ++++++++++++++++++++++ src/index.ts | 2 +- src/modules/toolbar/icon.ts | 33 ++++++++++++++++++++++-------- src/utils/create-entries.js | 25 +++++++++++++++++++++++ src/utils/svg-loader.js | 15 ++++++++++++++ tsconfig.json | 2 +- webpack.config.js | 40 ++++++++++++++++++++++++++++--------- 11 files changed, 133 insertions(+), 20 deletions(-) create mode 100644 index.d.ts create mode 100644 make.js create mode 100644 plugins/emoji/emoji.less create mode 100644 plugins/emoji/emoji.ts create mode 100644 src/utils/create-entries.js create mode 100644 src/utils/svg-loader.js diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 000000000..af36d5539 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,2 @@ +import { IJodit } from "./src/types/jodit" +declare var Jodit: IJodit; diff --git a/make.js b/make.js new file mode 100644 index 000000000..894aac942 --- /dev/null +++ b/make.js @@ -0,0 +1,5 @@ +module.exports = { + paths: [ + './plugins/emoji/', + ] +}; diff --git a/package.json b/package.json index f62ca1faf..84574ad0c 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "newversion": "npm test && npm version patch --no-git-tag-version && npm run build && npm run newversiongit && npm publish ./", "newversiongit": "git add --all && git commit -m \"New version $npm_package_version. Read more https://github.com/xdan/jodit/releases/tag/$npm_package_version \" && git tag $npm_package_version && git push --tags origin HEAD:master", "start": "node server.js", - "clean": "rm -f build/*", + "clean": "rm -rf build/*", "build": "npm run clean && npm run build-es5 && npm run build-es18 && npm run build-no-uglify-es5 && npm run build-no-uglify-es18", "build-es5": "webpack --progress --mode production --es es5 --uglify true", "build-es18": "webpack --progress --mode production --es es2018 --uglify true", diff --git a/plugins/emoji/emoji.less b/plugins/emoji/emoji.less new file mode 100644 index 000000000..f140b7511 --- /dev/null +++ b/plugins/emoji/emoji.less @@ -0,0 +1,3 @@ +.jodit_emoji { + color: red; +} diff --git a/plugins/emoji/emoji.ts b/plugins/emoji/emoji.ts new file mode 100644 index 000000000..650ee3181 --- /dev/null +++ b/plugins/emoji/emoji.ts @@ -0,0 +1,24 @@ + +// Config.prototype.controls.emoji = { +// icon: 'plugins/emoji/icon.svg', +// tooltip: 'Insert Emoji' +// } as IControlType; + +/** + * Support emoji + */ +Jodit.plugins.emoji = class emoji { + afterInit(editor: any) { + // const buttons = editor.options.buttons; + // + // if (!buttons.includes('emoji')) { + // buttons.push('emoji'); + // } + // + // editor.options.buttons = buttons; + } + + beforeDestruct(editor: any) { + + } +}; diff --git a/src/index.ts b/src/index.ts index 7354c7e38..6be0f9dbb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,7 +38,7 @@ const esFilter = (key: string): boolean => key !== '__esModule'; Object.keys(Icons) .filter(esFilter) .forEach((key: string) => { - ToolbarIcon.icons[key.replace('_', '-')] = (Icons as any)[key]; + ToolbarIcon.setIcon(key.replace('_', '-'), (Icons as any)[key]); }); // Modules diff --git a/src/modules/toolbar/icon.ts b/src/modules/toolbar/icon.ts index d5f24b73a..51cbe11ef 100644 --- a/src/modules/toolbar/icon.ts +++ b/src/modules/toolbar/icon.ts @@ -10,10 +10,20 @@ import { IDictionary } from '../../types'; export class ToolbarIcon { - static icons: IDictionary = {}; + private static icons: IDictionary = {}; + private static get(name: string): string | undefined { + return ToolbarIcon.icons[name] || + ToolbarIcon.icons[name.replace(/-/g, '_')] || + ToolbarIcon.icons[name.toLowerCase()]; + } + + /** + * Check if icon exist in store + * @param name + */ static exists(name: string): boolean { - return ToolbarIcon.icons[name] !== undefined; + return this.get(name) !== undefined; } /** @@ -26,12 +36,19 @@ export class ToolbarIcon { name: string, defaultValue: string = '' ): string { + return this.get(name) || defaultValue; + } - const icon = - ToolbarIcon.icons[name] || - ToolbarIcon.icons[name.replace(/-/g, '_')] || - ToolbarIcon.icons[name.toLowerCase()]; - - return icon || defaultValue; + /** + * Set SVG in store + * + * @param name + * @param value + */ + static setIcon( + name: string, + value: string + ): void { + this.icons[name.replace('_', '-')] = value; } } diff --git a/src/utils/create-entries.js b/src/utils/create-entries.js new file mode 100644 index 000000000..5d3784f21 --- /dev/null +++ b/src/utils/create-entries.js @@ -0,0 +1,25 @@ +const path = require('path'); +const fs = require('fs'); + +function createEntries(directory) { + const rootPath = path.resolve(process.cwd()); + const fullPath = path.resolve(directory); + const files = fs.readdirSync(fullPath); + + return files.reduce((entries, file) => { + let filename = path.join(fullPath, file); + + if (fs.lstatSync(filename).isFile()) { + filename = filename.replace(rootPath, '.'); + entries[filename.replace(/\.ts$/, '')] = filename; + } else { + Object.assign(entries, createEntries(filename)) + } + + return entries; + }, {}) +} + +module.exports = { + createEntries +}; diff --git a/src/utils/svg-loader.js b/src/utils/svg-loader.js new file mode 100644 index 000000000..6a55a5662 --- /dev/null +++ b/src/utils/svg-loader.js @@ -0,0 +1,15 @@ +/* + MIT License http://www.opensource.org/licenses/mit-license.php + Author Tobias Koppers @sokra +*/ +module.exports = function(content) { + this.cacheable && this.cacheable(); + this.value = content; + return "module.exports = " + JSON.stringify( + content + .replace(/[\n\t]+/g, '') + .replace(/[\s]+/g, ' ') + ); +}; + +module.exports.seperable = true; diff --git a/tsconfig.json b/tsconfig.json index 07a194905..5bd099aa8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,5 +25,5 @@ }, "typeRoots": ["./node_modules/@types/"], - "exclude": ["node_modules", "dist"] + "exclude": ["node_modules", "build"] } diff --git a/webpack.config.js b/webpack.config.js index ea8fa4d37..96ecb7134 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -7,6 +7,8 @@ const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'); const MinimizeJSPlugin = require('terser-webpack-plugin'); const pkg = require('./package.json'); +const make = require('./make.js'); +const { createEntries } = require('./src/utils/create-entries.js'); const banner = `/*! ${pkg.name} - ${pkg.description} @@ -31,7 +33,7 @@ module.exports = (env, argv) => { console.warn('ES mode: ' + ES); - const filename = 'jodit' + ((ES === 'es5' || isTest) ? '' : '.' + ES) + (uglify ? '.min' : ''); + const filename = (name) => name + ((ES === 'es5' || isTest) ? '' : '.' + ES) + (uglify ? '.min' : ''); const css_loaders = [ (debug || isTest) ? 'style-loader' : MiniCssExtractPlugin.loader, @@ -68,9 +70,10 @@ module.exports = (env, argv) => { devtool: debug ? 'inline-sourcemap' : false, - entry: debug - ? ['webpack-hot-middleware/client', './src/index'] - : './src/index', + entry: { + jodit: (debug ? ['webpack-hot-middleware/client', './src/index'] : ['./src/index']), + ...make.paths.reduce((entm, file) => ({...entm, ...createEntries(file)}), {}) + }, resolve: { extensions: ['.ts', '.d.ts', '.js', '.json', '.less', '.svg'] @@ -115,9 +118,9 @@ module.exports = (env, argv) => { ] }, - output: { + output: { path: path.join(__dirname, 'build'), - filename: filename + '.js', + filename: filename('[name]') + '.js', publicPath: '/build/', libraryTarget: 'umd' }, @@ -128,6 +131,23 @@ module.exports = (env, argv) => { test: /\.less$/, use: css_loaders }, + { + test: /\.(ts)$/, + loader: 'ts-loader', + options: { + transpileOnly: uglify, + compilerOptions: { + target: ES + } + }, + include: path.resolve('../'), + exclude: [ + path.resolve('src/'), + path.resolve('node_modules/'), + path.resolve('examples/'), + path.resolve('test/'), + ] + }, { test: /\.(ts)$/, use: [ @@ -148,14 +168,16 @@ module.exports = (env, argv) => { } }, exclude: [ - /(node_modules|bower_components)/, + /(node_modules)/, /langs\/[a-z]{2}\.ts/, /langs\/[a-z]{2}_[a-z]{2}\.ts/, ] }, { test: /\.svg$/i, - use: 'raw-loader' + use: { + loader: path.resolve('src/utils/svg-loader.js') + } } ] }, @@ -189,7 +211,7 @@ module.exports = (env, argv) => { case 'production': config.plugins.push( new MiniCssExtractPlugin({ - filename: filename + '.css' + filename: filename('[name]') + '.css' }) );