Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 15 additions & 9 deletions src/components/modules/toolbar/blockSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,30 @@ export default class BlockSettings extends Module<BlockSettingsNodes> {

this.opened = false;

/**
* If selection is at editor on Block Settings closing,
* it means that caret placed at some editable element inside the Block Settings.
* Previously we have saved the selection, then open the Block Settings and set caret to the input
*
* So, we need to restore selection back to Block after closing the Block Settings
*/
const { BlockManager, BlockSelection, CrossBlockSelection, UI } = this.Editor;
const settingsBlock = BlockManager.currentBlock;

/** Restore the saved selection when it moved outside the editable area. */
if (!SelectionUtils.isAtEditor) {
const focusInSettings = this.nodes.wrapper?.contains(document.activeElement);

this.selection.restore();

const focusNode = SelectionUtils.get()?.focusNode;

/** The settings can be opened for a different block than the one with the caret. */
if (focusInSettings === true && settingsBlock && focusNode && UI.nodes.redactor.contains(focusNode)) {
BlockManager.setCurrentBlockByChildNode(focusNode);
}
}

this.selection.clearSaved();

/**
* Remove highlighted content of a Block we are working with
*/
if (!this.Editor.CrossBlockSelection.isCrossBlockSelectionStarted && this.Editor.BlockManager.currentBlock) {
this.Editor.BlockSelection.unselectBlock(this.Editor.BlockManager.currentBlock);
if (!CrossBlockSelection.isCrossBlockSelectionStarted && settingsBlock) {
BlockSelection.unselectBlock(settingsBlock);
}

/** Tell to subscribers that block settings is closed */
Expand Down
69 changes: 69 additions & 0 deletions test/cypress/tests/api/tunes.cy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type EditorJS from '../../../../types';
import type { API } from '../../../../types';
import type { TunesMenuConfig } from '../../../../types/tools';

/* eslint-disable @typescript-eslint/no-empty-function */
Expand Down Expand Up @@ -134,6 +136,73 @@ describe('Editor Tunes Api', () => {
.should('contain.text', sampleText);
});

it('should make the block with the restored caret current when a tune closes settings', () => {
/** A tune that closes block settings when its input is confirmed */
class TestTune {
/** Identifies the tool as a tune */
public static readonly isTune = true;

/** @param options - tune constructor options */
constructor(private readonly options: { api: API }) {}

/** @returns {HTMLElement} input displayed in block settings */
public render(): HTMLElement {
const input = document.createElement('input');

input.dataset.cy = 'test-tune-input';
input.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
this.options.api.toolbar.toggleBlockSettings(false);
}
});

return input;
}
}

cy.createEditor({
data: {
blocks: [
{
id: 'first',
type: 'paragraph',
data: { text: 'Before after' },
},
{
id: 'second',
type: 'paragraph',
data: { text: 'Second paragraph' },
},
],
},
tools: { testTune: TestTune },
tunes: [ 'testTune' ],
}).as('editorInstance');

cy.get('[data-id=first] .ce-paragraph').click()
.then(($paragraph) => {
Cypress.dom.getWindowByElement($paragraph[0]).getSelection()
.collapse($paragraph[0].firstChild, 'Before '.length);
});
cy.get('[data-id=second] .ce-paragraph').trigger('mousemove');
cy.get('[data-cy=editorjs]').find('.ce-toolbar__settings-btn')
.click();

cy.get('[data-cy=test-tune-input]').type('value{enter}');
cy.get('[data-cy=test-tune-input]').should('not.exist');
cy.get('[data-id=first] .ce-paragraph').should('have.focus');
cy.get<EditorJS>('@editorInstance').then((editor) => {
expect(editor.blocks.getCurrentBlockIndex()).to.eq(0);
});

cy.focused().type('{enter}');
cy.get('[data-cy=editorjs]').find('.ce-paragraph')
.should(($paragraphs) => {
expect($paragraphs.toArray().map(paragraph => paragraph.textContent)).to.deep.equal(['Before ', 'after', 'Second paragraph']);
});
});

it('should support label alias', () => {
/** Test tune that should appear be rendered in block tunes menu */
class TestTune {
Expand Down