mirror of
https://github.com/codex-team/editor.js
synced 2024-06-10 18:03:25 +02:00
New Block Tunes API
This commit is contained in:
parent
bc3341bfa1
commit
0772ef3f1d
10
dist/codex-editor.js
vendored
10
dist/codex-editor.js
vendored
File diff suppressed because one or more lines are too long
52
dist/codex-editor.licenses.txt
vendored
52
dist/codex-editor.licenses.txt
vendored
|
@ -76,6 +76,32 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
regenerator-runtime
|
||||
MIT
|
||||
|
||||
@babel/register
|
||||
MIT
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2014-2018 Sebastian McKenzie <sebmck@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
codex-notifier
|
||||
MIT
|
||||
MIT License
|
||||
|
@ -376,29 +402,3 @@ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
@babel/register
|
||||
MIT
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2014-2018 Sebastian McKenzie <sebmck@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
|
|
@ -4,7 +4,7 @@ Toolbar Module has space for Block settings. Settings divided into:
|
|||
- space for plugin's settings, that is described by «Plugin»'s Developer
|
||||
- space for default settings. This option is also can be implemented and expanded
|
||||
|
||||
They difference between zones is that the first option is specified by plugin
|
||||
The difference between zones is that the first option is specified by plugin
|
||||
and each Block can have different options, when second option is for every Block
|
||||
regardless to the plugin's option.
|
||||
|
||||
|
|
|
@ -279,7 +279,7 @@ export default class Block {
|
|||
/**
|
||||
* Tunes used by Tool
|
||||
*/
|
||||
public tunes: BlockTune[];
|
||||
public tunes: {[name: string]: BlockTune};
|
||||
|
||||
/**
|
||||
* Editor`s API
|
||||
|
@ -305,6 +305,7 @@ export default class Block {
|
|||
toolInstance: BlockTool,
|
||||
toolClass: BlockToolConstructable,
|
||||
settings: ToolConfig,
|
||||
tunes: {[name: string]: BlockTuneConstructable},
|
||||
apiMethods: API,
|
||||
) {
|
||||
this.name = toolName;
|
||||
|
@ -317,7 +318,7 @@ export default class Block {
|
|||
/**
|
||||
* @type {BlockTune[]}
|
||||
*/
|
||||
this.tunes = this.makeTunes();
|
||||
this.tunes = this.makeTunes(tunes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -349,29 +350,42 @@ export default class Block {
|
|||
* Groups Tool's save processing time
|
||||
* @return {Object}
|
||||
*/
|
||||
public async save(): Promise<void|{tool: string, data: BlockToolData, time: number}> {
|
||||
const extractedBlock = await this.tool.save(this.pluginsContent as HTMLElement);
|
||||
public async save(): Promise<void|{tool: string, data: BlockToolData, time: number, settings: object}> {
|
||||
try {
|
||||
/**
|
||||
* Measuring execution time
|
||||
*/
|
||||
const measuringStart = window.performance.now();
|
||||
|
||||
/**
|
||||
* Measuring execution time
|
||||
*/
|
||||
const measuringStart = window.performance.now();
|
||||
let measuringEnd;
|
||||
const data = await this.tool.save(this.pluginsContent as HTMLElement);
|
||||
|
||||
return Promise.resolve(extractedBlock)
|
||||
.then((finishedExtraction) => {
|
||||
/** measure promise execution */
|
||||
measuringEnd = window.performance.now();
|
||||
/**
|
||||
* Save Block's Tunes data
|
||||
*/
|
||||
const settings = await Object.entries(this.tunes).reduce(async (result, [name, tune]) => {
|
||||
const savedData = await this.saveTuneState(tune);
|
||||
|
||||
if (savedData === null) {
|
||||
return result;
|
||||
}
|
||||
|
||||
return {
|
||||
tool: this.name,
|
||||
data: finishedExtraction,
|
||||
time : measuringEnd - measuringStart,
|
||||
...result,
|
||||
[name]: savedData,
|
||||
};
|
||||
})
|
||||
.catch((error) => {
|
||||
_.log(`Saving proccess for ${this.name} tool failed due to the ${error}`, 'log', 'red');
|
||||
});
|
||||
}, {});
|
||||
|
||||
const measuringEnd = window.performance.now();
|
||||
|
||||
return {
|
||||
tool: this.name,
|
||||
data,
|
||||
settings,
|
||||
time: measuringEnd - measuringStart,
|
||||
};
|
||||
} catch (error) {
|
||||
_.log(`Saving proccess for ${this.name} tool failed due to the ${error}`, 'log', 'red');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -402,16 +416,26 @@ export default class Block {
|
|||
* Each block has default tune instance that have states
|
||||
* @return {BlockTune[]}
|
||||
*/
|
||||
public makeTunes(): BlockTune[] {
|
||||
const tunesList = [MoveUpTune, DeleteTune, MoveDownTune];
|
||||
public makeTunes(tunes: {[name: string]: BlockTuneConstructable}): {[name: string]: BlockTune} {
|
||||
const tunesList = {
|
||||
MoveUpTune,
|
||||
DeleteTune,
|
||||
MoveDownTune,
|
||||
...tunes,
|
||||
};
|
||||
|
||||
// Pluck tunes list and return tune instances with passed Editor API and settings
|
||||
return tunesList.map( (tune: BlockTuneConstructable) => {
|
||||
return new tune({
|
||||
return Object.entries(tunesList).reduce((result, [name, Tune]: [string, BlockTuneConstructable]) => {
|
||||
const tune = new Tune({
|
||||
api: this.api,
|
||||
settings: this.settings,
|
||||
settings: this.settings[name],
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
[name]: tune,
|
||||
...result,
|
||||
};
|
||||
}, {});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -421,7 +445,7 @@ export default class Block {
|
|||
public renderTunes(): DocumentFragment {
|
||||
const tunesElement = document.createDocumentFragment();
|
||||
|
||||
this.tunes.forEach( (tune) => {
|
||||
Object.values(this.tunes).forEach( (tune) => {
|
||||
$.append(tunesElement, tune.render());
|
||||
});
|
||||
|
||||
|
@ -449,4 +473,12 @@ export default class Block {
|
|||
wrapper.appendChild(contentNode);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
private async saveTuneState(tune: BlockTune): Promise<object> {
|
||||
if (!tune.save || typeof tune.save !== 'function') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return tune.save();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -165,6 +165,10 @@ export default class Core {
|
|||
if (!this.config.data.blocks || this.config.data.blocks.length === 0) {
|
||||
this.config.data.blocks = [ initialBlockData ];
|
||||
}
|
||||
|
||||
if (_.isEmpty(this.config.tunes)) {
|
||||
this.config.tunes = {} as any;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -153,7 +153,7 @@ export default class BlockManager extends Module {
|
|||
public composeBlock(toolName: string, data: BlockToolData = {}, settings: ToolConfig = {}): Block {
|
||||
const toolInstance = this.Editor.Tools.construct(toolName, data) as BlockTool;
|
||||
const toolClass = this.Editor.Tools.available[toolName] as BlockToolConstructable;
|
||||
const block = new Block(toolName, toolInstance, toolClass, settings, this.Editor.API.methods);
|
||||
const block = new Block(toolName, toolInstance, toolClass, settings, this.config.tunes, this.Editor.API.methods);
|
||||
|
||||
this.bindEvents(block);
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
* @author Codex Team
|
||||
* @version 2.0.0
|
||||
*/
|
||||
import _ from '../utils';
|
||||
import Module from '../__module';
|
||||
import {OutputData} from '../../../types';
|
||||
import Block from '../block';
|
||||
|
@ -67,10 +68,19 @@ export default class Saver extends Module {
|
|||
return;
|
||||
}
|
||||
|
||||
blocks.push({
|
||||
let outputData: {[key: string]: any} = {
|
||||
type: extraction.tool,
|
||||
data: extraction.data,
|
||||
});
|
||||
};
|
||||
|
||||
if (!_.isEmpty(extraction.settings)) {
|
||||
outputData = {
|
||||
settings: extraction.settings,
|
||||
...outputData,
|
||||
};
|
||||
}
|
||||
|
||||
blocks.push(outputData);
|
||||
});
|
||||
|
||||
console.log('Total', totalTime);
|
||||
|
|
2
types/block-tunes/block-tune.d.ts
vendored
2
types/block-tunes/block-tune.d.ts
vendored
|
@ -10,6 +10,8 @@ export interface BlockTune {
|
|||
* @return {HTMLElement}
|
||||
*/
|
||||
render(): HTMLElement;
|
||||
|
||||
save?(): object;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
4
types/configs/editor-config.d.ts
vendored
4
types/configs/editor-config.d.ts
vendored
|
@ -1,5 +1,5 @@
|
|||
import {ToolConstructable, ToolSettings} from '../tools';
|
||||
import {OutputData} from '../index';
|
||||
import {BlockTuneConstructable, OutputData} from '../index';
|
||||
import {SanitizerConfig} from './sanitizer-config';
|
||||
|
||||
export interface EditorConfig {
|
||||
|
@ -36,6 +36,8 @@ export interface EditorConfig {
|
|||
*/
|
||||
tools?: {[toolName: string]: ToolConstructable|ToolSettings};
|
||||
|
||||
tunes?: {[name: string]: BlockTuneConstructable};
|
||||
|
||||
/**
|
||||
* Data to render on Editor start
|
||||
*/
|
||||
|
|
2
types/data-formats/output-data.d.ts
vendored
2
types/data-formats/output-data.d.ts
vendored
|
@ -18,4 +18,6 @@ export interface OutputData {
|
|||
type: string;
|
||||
data: BlockToolData
|
||||
}>;
|
||||
|
||||
settings?: {[tune: string]: object};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue