-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
601 additions
and
491 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/*! | ||
* Jodit Editor (https://xdsoft.net/jodit/) | ||
* Released under MIT see LICENSE.txt in the project root for license information. | ||
* Copyright (c) 2013-2023 Valeriy Chupurnov. All rights reserved. https://xdsoft.net | ||
*/ | ||
|
||
/** | ||
* @module plugin | ||
*/ | ||
|
||
import type { IDictionary, IJodit, IPlugin, PluginInstance } from 'jodit/types'; | ||
import { isInitable } from 'jodit/core/helpers/checker'; | ||
import { loadStyle } from './load' | ||
|
||
/** | ||
* Init plugin and try init waiting list | ||
* @private | ||
*/ | ||
export function initInstance( | ||
jodit: IJodit, | ||
pluginName: string, | ||
instance: PluginInstance, | ||
doneList: Set<string>, | ||
waitingList: IDictionary<PluginInstance> | ||
): void { | ||
if (init(jodit, pluginName, instance, doneList, waitingList)) { | ||
Object.keys(waitingList).forEach(name => { | ||
const plugin = waitingList[name]; | ||
init(jodit, name, plugin, doneList, waitingList); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Init plugin if it has not dependencies in another case wait requires plugins will be init | ||
* @private | ||
*/ | ||
export function init( | ||
jodit: IJodit, | ||
pluginName: string, | ||
instance: PluginInstance, | ||
doneList: Set<string>, | ||
waitingList: IDictionary<PluginInstance> | ||
): boolean { | ||
const req = (instance as IPlugin).requires; | ||
|
||
if (req?.length && !req.every(name => doneList.has(name))) { | ||
// @ts-ignore | ||
if (!isProd && !isTest && !waitingList[pluginName]) { | ||
console.log('Await plugin: ', pluginName); | ||
} | ||
|
||
waitingList[pluginName] = instance; | ||
return false; | ||
} | ||
|
||
if (isInitable(instance)) { | ||
try { | ||
instance.init(jodit); | ||
} catch (e) { | ||
console.error(e); | ||
|
||
// @ts-ignore | ||
if (!isProd) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
doneList.add(pluginName); | ||
delete waitingList[pluginName]; | ||
|
||
if ((instance as IPlugin).hasStyle) { | ||
loadStyle(jodit, pluginName).catch(e => { | ||
!isProd && console.log(e); | ||
}); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/*! | ||
* Jodit Editor (https://xdsoft.net/jodit/) | ||
* Released under MIT see LICENSE.txt in the project root for license information. | ||
* Copyright (c) 2013-2023 Valeriy Chupurnov. All rights reserved. https://xdsoft.net | ||
*/ | ||
|
||
/** | ||
* @module plugin | ||
*/ | ||
|
||
import type { IExtraPlugin, IJodit, PluginType } from 'jodit/types'; | ||
import { | ||
appendScriptAsync, | ||
appendStyleAsync | ||
} from 'jodit/core/helpers/utils/append-script'; | ||
import { kebabCase } from 'jodit/core/helpers/string/kebab-case'; | ||
import { normalizeName } from 'jodit/core/plugin/helpers/utils'; | ||
|
||
const styles: Set<string> = new Set(); | ||
|
||
/** | ||
* @private | ||
*/ | ||
export async function loadStyle( | ||
jodit: IJodit, | ||
pluginName: string | ||
): Promise<void> { | ||
const url = getFullUrl(jodit, pluginName, false); | ||
|
||
if (styles.has(url)) { | ||
return; | ||
} | ||
|
||
styles.add(url); | ||
|
||
return appendStyleAsync(jodit, url); | ||
} | ||
|
||
/** | ||
* Call full url to the script or style file | ||
* @private | ||
*/ | ||
function getFullUrl(jodit: IJodit, name: string, js: boolean): string { | ||
name = kebabCase(name); | ||
|
||
return ( | ||
jodit.basePath + | ||
'plugins/' + | ||
name + | ||
'/' + | ||
name + | ||
'.' + | ||
(js ? 'js' : 'css') | ||
); | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
export function loadExtras( | ||
items: Map<string, PluginType>, | ||
jodit: IJodit, | ||
extrasList: IExtraPlugin[], | ||
callback: () => void | ||
): void { | ||
try { | ||
const needLoadExtras = extrasList.filter( | ||
extra => !items.has(normalizeName(extra.name)) | ||
); | ||
|
||
if (needLoadExtras.length) { | ||
load(jodit, needLoadExtras, callback); | ||
} | ||
} catch (e) { | ||
// @ts-ignore | ||
if (!isProd) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Download plugins | ||
* @private | ||
*/ | ||
function load( | ||
jodit: IJodit, | ||
pluginList: IExtraPlugin[], | ||
callback: () => void | ||
): void { | ||
pluginList.map(extra => { | ||
const url = extra.url || getFullUrl(jodit, extra.name, true); | ||
|
||
return appendScriptAsync(jodit, url) | ||
.then(callback) | ||
.catch(() => null); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*! | ||
* Jodit Editor (https://xdsoft.net/jodit/) | ||
* Released under MIT see LICENSE.txt in the project root for license information. | ||
* Copyright (c) 2013-2023 Valeriy Chupurnov. All rights reserved. https://xdsoft.net | ||
*/ | ||
|
||
/** | ||
* @module plugin | ||
*/ | ||
|
||
import type { IJodit, Nullable, PluginInstance, PluginType } from 'jodit/types'; | ||
import { isFunction } from 'jodit/core/helpers/checker'; | ||
|
||
/** | ||
* Create instance of plugin | ||
* @private | ||
*/ | ||
export function makeInstance( | ||
jodit: IJodit, | ||
plugin: PluginType | ||
): Nullable<PluginInstance> { | ||
try { | ||
try { | ||
// @ts-ignore | ||
return isFunction(plugin) ? new plugin(jodit) : plugin; | ||
} catch (e) { | ||
if (isFunction(plugin) && !plugin.prototype) { | ||
return (plugin as Function)(jodit); | ||
} | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
// @ts-ignore | ||
if (!isProd) { | ||
throw e; | ||
} | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/*! | ||
* Jodit Editor (https://xdsoft.net/jodit/) | ||
* Released under MIT see LICENSE.txt in the project root for license information. | ||
* Copyright (c) 2013-2023 Valeriy Chupurnov. All rights reserved. https://xdsoft.net | ||
*/ | ||
|
||
/** | ||
* @module plugin | ||
*/ | ||
|
||
import { kebabCase } from 'jodit/core/helpers/string/kebab-case'; | ||
|
||
/** | ||
* @private | ||
*/ | ||
export function normalizeName(name: string): string { | ||
return kebabCase(name).toLowerCase(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/*! | ||
* Jodit Editor (https://xdsoft.net/jodit/) | ||
* Released under MIT see LICENSE.txt in the project root for license information. | ||
* Copyright (c) 2013-2023 Valeriy Chupurnov. All rights reserved. https://xdsoft.net | ||
*/ | ||
|
||
declare module 'jodit/types/events' { | ||
interface IEventEmitter { | ||
/** | ||
* Emitted every time after the plugins have been initialized | ||
* or a deferred plugin has been loaded and also initialized | ||
*/ | ||
on(event: 'updatePlugins', callback: () => void): this; | ||
} | ||
} |
Oops, something went wrong.