Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Block Tunes API #1596

Merged
merged 29 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
24dc069
Add internal wrappers for tools classes
gohabereg Mar 14, 2021
224dd3b
FIx lint
gohabereg Mar 14, 2021
83e17bb
Change tools collections to map
gohabereg Mar 15, 2021
1ffcbed
Apply some more refactoring
gohabereg Mar 15, 2021
3d743f3
Make tool instance private field
gohabereg Mar 15, 2021
e56579c
Add some docs
gohabereg Mar 15, 2021
0069a2a
Fix eslint
gohabereg Mar 15, 2021
d407e32
Basic implementation for Block Tunes
gohabereg Mar 15, 2021
a0160ef
Small fix for demo
gohabereg Mar 15, 2021
dde3bd4
Review changes
gohabereg Mar 16, 2021
28bfacf
Merge branch 'next' of github.com:codex-team/editor.js into reafctori…
gohabereg Mar 16, 2021
ca1aef3
Fix
gohabereg Mar 16, 2021
a459ab5
Merge branch 'reafctoring/tools' of github.com:codex-team/editor.js i…
gohabereg Mar 16, 2021
6ecc3ac
Add common tunes and ToolsCollection class
gohabereg Mar 17, 2021
1f9c5f1
Fixes after review
gohabereg Mar 18, 2021
c81b9a0
Merge branch 'reafctoring/tools' of github.com:codex-team/editor.js i…
gohabereg Mar 18, 2021
b8ba38c
Rename tools collections
gohabereg Mar 18, 2021
aea3158
Readonly fix
gohabereg Mar 18, 2021
82f3bfa
Merge branch 'reafctoring/tools' of github.com:codex-team/editor.js i…
gohabereg Mar 18, 2021
a3bb039
Some fixes after review
gohabereg Mar 27, 2021
41274fb
Merge branch 'next' of github.com:codex-team/editor.js into feature/b…
gohabereg Mar 31, 2021
13b64eb
Apply suggestions from code review
gohabereg Apr 2, 2021
4e9a098
Fixes after review
gohabereg Apr 2, 2021
ebc8b3f
Add docs and changelog
gohabereg Apr 2, 2021
cad9691
Update docs/block-tunes.md
gohabereg Apr 2, 2021
66feab7
Apply suggestions from code review
gohabereg Apr 2, 2021
8ce77a8
Update src/components/block/index.ts
gohabereg Apr 4, 2021
94bfd10
[Dev] Tools utils tests (#1602)
gohabereg Apr 4, 2021
5fe699c
Fix test & bump version
gohabereg Apr 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Basic implementation for Block Tunes
  • Loading branch information
gohabereg committed Mar 15, 2021
commit d407e32df79707e5bceb4a0a3312dca91c6996a6
9 changes: 8 additions & 1 deletion example/example-dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@

<script src="./tools/marker/dist/bundle.js"></script><!-- Marker -->
<script src="./tools/inline-code/dist/bundle.js"></script><!-- Inline Code -->
<script src="./tunes/favorite/index.js"></script><!-- Inline Code -->

<!-- Load Editor.js's Core -->
<script src="../dist/editor.js" onload="document.getElementById('hint-core').hidden = true;"></script>
Expand Down Expand Up @@ -113,6 +114,8 @@
* Tools list
*/
tools: {
favorite: FavoriteTune,

/**
* Each Tool is a Plugin. Pass them via 'class' option with necessary settings {@link docs/tools.md}
*/
Expand All @@ -122,7 +125,8 @@
config: {
placeholder: 'Header'
},
shortcut: 'CMD+SHIFT+H'
shortcut: 'CMD+SHIFT+H',
tunes: ['favorite']
},

/**
Expand Down Expand Up @@ -199,6 +203,9 @@
data: {
text: "Editor.js",
level: 2
},
tunes: {
favorite: true
}
},
{
Expand Down
59 changes: 59 additions & 0 deletions example/tunes/favorite/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class FavoriteTune {
static get isTune() {
return true
}

constructor({api, block, data = false}) {
this.state = data;
}

set state(state) {
this._state = state;

if (this.wrapper) {
this.wrapper.style.backgroundColor = this.background;
}

if (this.button) {
this.button.innerHTML = this.state ? '<b>F</b>' : 'F';
}
}

get state() {
return this._state;
}

get background() {
return this.state ? 'yellow' : 'unset';
}

render() {
this.button = document.createElement('div');

this.button.classList.add('ce-settings__button');

this.button.innerHTML = this.state ? '<b>F</b>' : 'F';

this.button.addEventListener('click', () => this.onClick());

return this.button;
}

onClick() {
this.state = !this.state;
}

wrap(pluginsContent) {
this.wrapper = document.createElement('div');

this.wrapper.style.backgroundColor = this.background;

this.wrapper.appendChild(pluginsContent);

return this.wrapper;
}

save() {
return this.state;
}
}
3 changes: 3 additions & 0 deletions src/components/block-tunes/block-tune-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import $ from '../dom';
*
*/
export default class DeleteTune implements BlockTune {

public static readonly isTune = true;

/**
* Property that contains Editor.js API methods
*
Expand Down
3 changes: 3 additions & 0 deletions src/components/block-tunes/block-tune-move-down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { API, BlockTune } from '../../../types';
*
*/
export default class MoveDownTune implements BlockTune {

public static readonly isTune = true;

/**
* Property that contains Editor.js API methods
*
Expand Down
4 changes: 4 additions & 0 deletions src/components/block-tunes/block-tune-move-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { API, BlockTune } from '../../../types';
*
*/
export default class MoveUpTune implements BlockTune {


public static readonly isTune = true;

/**
* Property that contains Editor.js API methods
*
Expand Down
59 changes: 45 additions & 14 deletions src/components/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BlockTool as IBlockTool,
BlockToolConstructable,
BlockToolData,
BlockTune,
BlockTune as IBlockTune,
BlockTuneConstructable,
SanitizerConfig,
ToolConfig,
Expand All @@ -23,16 +23,13 @@ import BlockTool from '../tools/block';
import MoveUpTune from '../block-tunes/block-tune-move-up';
import DeleteTune from '../block-tunes/block-tune-delete';
import MoveDownTune from '../block-tunes/block-tune-move-down';
import BlockTune from '../tools/tune';
import {BlockTuneData} from "../../../types/block-tunes/block-tune-data";

/**
* Interface describes Block class constructor argument
*/
interface BlockConstructorOptions {
/**
* Tool's name
*/
name: string;

/**
* Initial Block data
*/
Expand All @@ -52,6 +49,10 @@ interface BlockConstructorOptions {
* This flag indicates that the Block should be constructed in the read-only mode.
*/
readOnly: boolean;

tunes: BlockTune[];

tunesData: {[name: string]: BlockTuneData};
}

/**
Expand Down Expand Up @@ -145,6 +146,8 @@ export default class Block {
*/
private readonly toolInstance: IBlockTool;

private readonly tunesInstances: Map<string, IBlockTune> = new Map();

/**
* Editor`s API module
*/
Expand Down Expand Up @@ -195,21 +198,21 @@ export default class Block {

/**
* @param {object} options - block constructor options
* @param {string} options.name - Tool name that passed on initialization
* @param {BlockToolData} options.data - Tool's initial data
* @param {BlockToolConstructable} options.Tool — Tool's class
* @param {ToolSettings} options.settings - default tool's config
* @param options.api - Editor API module for pass it to the Block Tunes
* @param {boolean} options.readOnly - Read-Only flag
*/
constructor({
name,
data,
tool,
api,
readOnly,
tunes,
tunesData,
}: BlockConstructorOptions) {
this.name = name;
this.name = tool.name;
this.settings = tool.settings;
this.config = tool.settings.config || {};
this.api = api;
Expand All @@ -220,11 +223,16 @@ export default class Block {
this.tool = tool;
this.toolInstance = tool.instance(data, this.blockAPI, readOnly);

this.holder = this.compose();
/**
* @type {BlockTune[]}
*/
this.tunes = this.makeTunes();
this.tunes = tunes;

tunes.forEach((tune) => {
this.tunesInstances.set(tune.name, tune.instance(tunesData[tune.name], this.blockAPI))
});

this.holder = this.compose();
}

/**
Expand Down Expand Up @@ -526,6 +534,15 @@ export default class Block {
*/
public async save(): Promise<void|SavedData> {
const extractedBlock = await this.toolInstance.save(this.pluginsContent as HTMLElement);
const tunesData: {[name: string]: BlockTuneData} = {}

Array
.from(this.tunesInstances.entries())
.forEach(([name, tune]) => {
if (_.isFunction(tune.save)) {
tunesData[name] = tune.save()
}
});

/**
* Measuring execution time
Expand All @@ -541,6 +558,7 @@ export default class Block {
return {
tool: this.name,
data: finishedExtraction,
tunes: tunesData,
time: measuringEnd - measuringStart,
};
})
Expand Down Expand Up @@ -574,7 +592,7 @@ export default class Block {
*
* @returns {BlockTune[]}
*/
public makeTunes(): BlockTune[] {
public makeTunes(): IBlockTune[] {
const tunesList = [
{
name: 'moveUp',
Expand All @@ -595,6 +613,8 @@ export default class Block {
return new Tune({
api: this.api.getMethodsForTool(name, ToolType.Tune),
settings: this.config,
block: this.blockAPI,
data: {}
});
});
}
Expand All @@ -607,7 +627,7 @@ export default class Block {
public renderTunes(): DocumentFragment {
const tunesElement = document.createDocumentFragment();

this.tunes.forEach((tune) => {
this.tunesInstances.forEach((tune) => {
$.append(tunesElement, tune.render());
});

Expand Down Expand Up @@ -690,7 +710,18 @@ export default class Block {
pluginsContent = this.toolInstance.render();

contentNode.appendChild(pluginsContent);
wrapper.appendChild(contentNode);

let wrappedContentNode: HTMLElement = contentNode;
neSpecc marked this conversation as resolved.
Show resolved Hide resolved

Array
.from(this.tunesInstances.values())
.forEach((tune) => {
if (_.isFunction(tune.wrap)) {
wrappedContentNode = tune.wrap(wrappedContentNode)
}
});

wrapper.appendChild(wrappedContentNode);

return wrapper;
}
Expand Down
14 changes: 12 additions & 2 deletions src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as _ from '../utils';
import Blocks from '../blocks';
import { BlockToolConstructable, BlockToolData, PasteEvent } from '../../../types';
import BlockTool from '../tools/block';
import {BlockTuneData} from '../../../types/block-tunes/block-tune-data';

/**
* @typedef {BlockManager} BlockManager
Expand Down Expand Up @@ -220,15 +221,21 @@ export default class BlockManager extends Module {
*
* @returns {Block}
*/
public composeBlock({ tool: name, data = {} }: {tool: string; data?: BlockToolData}): Block {
public composeBlock({
tool: name,
data = {},
tunes: tunesData = {}
}: {tool: string; data?: BlockToolData, tunes?: {[name: string]: BlockTuneData}}): Block {
const readOnly = this.Editor.ReadOnly.isEnabled;
const tool = this.Editor.Tools.block.get(name);
const tunes = this.Editor.Tools.getTunesForTool(tool);
const block = new Block({
name,
data,
tool,
api: this.Editor.API,
readOnly,
tunes,
tunesData,
});

if (!readOnly) {
Expand Down Expand Up @@ -256,12 +263,14 @@ export default class BlockManager extends Module {
index,
needToFocus = true,
replace = false,
tunes = {},
}: {
tool?: string;
data?: BlockToolData;
index?: number;
needToFocus?: boolean;
replace?: boolean;
tunes?: {[name: string]: BlockTuneData}
} = {}): Block {
let newIndex = index;

Expand All @@ -272,6 +281,7 @@ export default class BlockManager extends Module {
const block = this.composeBlock({
tool,
data,
tunes,
});

this._blocks.insert(newIndex, block, replace);
Expand Down
4 changes: 2 additions & 2 deletions src/components/modules/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export default class Renderer extends Module {
*/
public async insertBlock(item: OutputBlockData): Promise<void> {
const { Tools, BlockManager } = this.Editor;
const tool = item.type;
const data = item.data;
const { type: tool, data, tunes } = item;

if (Tools.available.has(tool)) {
try {
BlockManager.insert({
tool,
data,
tunes,
});
} catch (error) {
_.log(`Block «${tool}» skipped because of plugins error`, 'warn', data);
Expand Down
12 changes: 9 additions & 3 deletions src/components/modules/saver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class Saver extends Module {

_.log('[Editor.js saving]:', 'groupCollapsed');

allExtractedData.forEach(({ tool, data, time, isValid }) => {
allExtractedData.forEach(({ tool, data, tunes, time, isValid }) => {
totalTime += time;

/**
Expand All @@ -104,10 +104,16 @@ export default class Saver extends Module {
return;
}

blocks.push({
const output: any = {
type: tool,
data,
});
};

if (!_.isEmpty(tunes)) {
output.tunes = tunes;
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
}

blocks.push(output);
});

_.log('Total', 'log', totalTime);
Expand Down
Loading