Skip to content

Commit

Permalink
Allow set extraPlugin url
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Dec 23, 2019
1 parent 79db159 commit 800411d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 35 deletions.
2 changes: 1 addition & 1 deletion make.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// You can add here your extra plugins
module.exports = {
paths: [
// require('path').resolve(__dirname, './plugins/example/'),
require('path').resolve(__dirname, './plugins/example/'),
]
};
25 changes: 9 additions & 16 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
val
} from './modules/helpers/';
import { ToolbarIcon } from './modules/toolbar/icon';
import { IDictionary, IJodit, IViewOptions } from './types';
import { IExtraPlugin, IDictionary, IJodit, IViewOptions } from './types';
import { IFileBrowserCallBackData } from './types/fileBrowser';
import { Buttons, Controls, IControlType } from './types/toolbar';
import { extend } from './modules/helpers/extend';
Expand Down Expand Up @@ -562,12 +562,12 @@ export class Config implements IViewOptions {
* @example
* ```typescript
* var editor = new Jodit('.editor', {
* extraPlugins: 'emoji'
* extraPlugins: ['emoji']
* });
* ```
* It will try load %SCRIPT_PATH%/plugins/emoji/emoji.js and after load will try init it
*/
extraPlugins: string[] | string = [];
extraPlugins: Array<string | IExtraPlugin> = [];

/**
* Base path for download extra plugins
Expand Down Expand Up @@ -816,8 +816,7 @@ export class Config implements IViewOptions {
}

export const OptionsDefault: any = function(this: any, options: any) {
const
def = Config.defaultOptions,
const def = Config.defaultOptions,
self: any = this;

self.plainOptions = options;
Expand All @@ -832,22 +831,15 @@ export const OptionsDefault: any = function(this: any, options: any) {
}
}

const
defValue = (def as any)[key],
const defValue = (def as any)[key],
isObject = typeof defValue === 'object' && defValue !== null;

if (
isObject &&
!['ownerWindow', 'ownerDocument'].includes(key) &&
!Array.isArray(defValue)
) {
self[key] = extend(
true,
{},
defValue,
(opt as any)[key]
);

self[key] = extend(true, {}, defValue, (opt as any)[key]);
} else {
self[key] = (opt as any)[key];
}
Expand Down Expand Up @@ -1082,12 +1074,13 @@ Config.prototype.controls = {
video: {
popup: (editor: IJodit, current, control, close) => {
const bylink = editor.create.fromHTML(
`<form class="jodit_form">
`<form class="jodit_form">
<div class="jodit jodit_form_group">
<input class="jodit_input" required name="code" placeholder="http://" type="url"/>
<button class="jodit_button" type="submit">${editor.i18n('Insert')}</button>
</div>
</form>`) as HTMLFormElement,
</form>`
) as HTMLFormElement,
bycode = editor.create.fromHTML(
`<form class="jodit_form">
<div class="jodit_form_group">
Expand Down
40 changes: 24 additions & 16 deletions src/modules/PluginSystem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
IExtraPlugin,
IDictionary,
IJodit,
IPlugin,
Expand All @@ -13,7 +14,8 @@ import {
isFunction,
appendScriptAsync,
splitArray,
appendStyleAsync
appendStyleAsync,
isString
} from './helpers/';

/**
Expand Down Expand Up @@ -54,18 +56,18 @@ export class PluginSystem implements IPluginSystem {
* @param jodit
*/
async init(jodit: IJodit): Promise<void> {
const extras = splitArray(jodit.options.extraPlugins).map(s =>
s.toLowerCase()
),
disable = splitArray(jodit.options.disablePlugins).map(s =>
const extrasList: IExtraPlugin[] = jodit.options.extraPlugins.map(s => {
return isString(s) ? { name: s.toLowerCase() } : s;
}),
disableList = splitArray(jodit.options.disablePlugins).map(s =>
s.toLowerCase()
),
doneList: string[] = [],
promiseList: IDictionary<PluginInstance | undefined> = {},
plugins: PluginInstance[] = [],
makeAndInit = (plugin: PluginType, name: string) => {
if (
disable.includes(name) ||
disableList.includes(name) ||
doneList.includes(name) ||
promiseList[name]
) {
Expand All @@ -79,10 +81,10 @@ export class PluginSystem implements IPluginSystem {
plugins.push(instance);
};

if (extras && extras.length) {
if (extrasList && extrasList.length) {
try {
const needLoadExtras = extras.filter(
key => !this.items.has(key)
const needLoadExtras = extrasList.filter(
extra => !this.items.has(extra.name)
);

if (needLoadExtras.length) {
Expand Down Expand Up @@ -194,22 +196,24 @@ export class PluginSystem implements IPluginSystem {
* @param jodit
* @param pluginList
*/
private load(jodit: IJodit, pluginList: string[]): Promise<any> {
private load(jodit: IJodit, pluginList: IExtraPlugin[]): Promise<any> {
const reflect = (p: Promise<any>) =>
p.then(
(v: any) => ({ v, status: 'fulfilled' }),
(e: any) => ({ e, status: 'rejected' })
);

return Promise.all(
pluginList.map(name =>
reflect(
pluginList.map(extra => {
const url = extra.url || PluginSystem.getFullUrl(jodit, name, true);

return reflect(
appendScriptAsync(
PluginSystem.getFullUrl(jodit, name, true),
url,
jodit.ownerDocument
)
)
)
);
})
);
}

Expand All @@ -233,7 +237,11 @@ export class PluginSystem implements IPluginSystem {
* @param name
* @param js
*/
private static getFullUrl(jodit: IJodit, name: string, js: boolean): string {
private static getFullUrl(
jodit: IJodit,
name: string,
js: boolean
): string {
return (
jodit.basePath +
'plugins/' +
Expand Down
15 changes: 13 additions & 2 deletions src/modules/helpers/array/splitArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@
* Copyright (c) 2013-2019 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/

import { isString } from '../checker';

/**
* Split separated elements
* @param a
*/
export const splitArray = (a: any[] | string): any[] =>
typeof a === 'string' ? a.split(/[,\s]+/) : a;
export const splitArray = <N = string>(a: N[] | string): N[] | string[] => {
if (Array.isArray(a)) {
return a;
}

if (isString(a)) {
return a.split(/[,\s]+/);
}

return <N[]>[];
};
1 change: 1 addition & 0 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export { ToolbarButton } from './toolbar/button';
export { Stack } from './Stack';
export { Widget } from './Widget';
export { Uploader } from './Uploader';
export { PluginSystem } from './PluginSystem';
5 changes: 5 additions & 0 deletions src/types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ interface PluginFunction {
export type PluginType = typeof IPlugin | IPlugin | PluginFunction;
export type PluginInstance = IPlugin | object;

export interface IExtraPlugin {
name: string;
url?: string;
}

export interface IPluginSystem {
add(name: string, plugin: PluginType): void;
remove(name: string): void;
Expand Down

0 comments on commit 800411d

Please sign in to comment.