diff --git a/modules/helpers/Keys/BspaceKey.js b/modules/helpers/Keys/BspaceKey.js index 8ff6879..c76be79 100644 --- a/modules/helpers/Keys/BspaceKey.js +++ b/modules/helpers/Keys/BspaceKey.js @@ -1,7 +1,6 @@ import CombineBlocks from '../Manipulation/CombineBlocks'; import CombineBlockPrev from '../Manipulation/CombineBlockPrev'; import ToggleBlockType from '../Manipulation/ToggleBlockType'; -import DeleteFigure from '../Manipulation/DeleteFigure'; const KEY_CODES = { 'bspace': 8 }; @@ -29,11 +28,8 @@ class BspaceKey { const node = document.getElementsByName(guids.anchor)[0]; const type = node.tagName.toLowerCase(); - if (this.selection.isFigure() && !this.selection.isCaption()) { - results = this._deleteFigure().execute(guids); - // combine the blocks selected - } else if (this.selection.crossBlock()) { + if (this.selection.crossBlock()) { results = this._combineBlocks().execute(guids, offsets); // beginning of a list item converts to a paragraph @@ -65,10 +61,6 @@ class BspaceKey { }); } - _deleteFigure() { - return new DeleteFigure(this.content); - } - _toggleBlockType() { return new ToggleBlockType(this.content); } diff --git a/modules/helpers/Keys/DeleteKey.js b/modules/helpers/Keys/DeleteKey.js index c65170e..28c38f9 100644 --- a/modules/helpers/Keys/DeleteKey.js +++ b/modules/helpers/Keys/DeleteKey.js @@ -1,6 +1,5 @@ import CombineBlocks from '../Manipulation/CombineBlocks'; import CombineBlockNext from '../Manipulation/CombineBlockNext'; -import DeleteFigure from '../Manipulation/DeleteFigure'; const KEY_CODES = { 'delete': 46 }; @@ -23,10 +22,7 @@ class DeleteKey { const offsets = this.selection.offsets(); let results; - if (this.selection.isFigure() && !this.selection.isCaption()) { - results = this._deleteFigure().execute(guids); - - } else if (this.selection.crossBlock()) { + if (this.selection.crossBlock()) { results = this._combineBlocks().execute(guids, offsets); } else if (this.selection.endOfBlock()) { @@ -53,10 +49,6 @@ class DeleteKey { }); } - _deleteFigure() { - return new DeleteFigure(this.content); - } - _combineBlocks() { return new CombineBlocks(this.content); } diff --git a/modules/helpers/Keys/OtherKey.js b/modules/helpers/Keys/OtherKey.js index ad0c8ef..0f2e03e 100644 --- a/modules/helpers/Keys/OtherKey.js +++ b/modules/helpers/Keys/OtherKey.js @@ -39,7 +39,7 @@ class OtherKey { const node = document.getElementsByName(guids.anchor)[0]; let text; - if (node && node.getAttribute('data-figure')) { + if (false && node && node.getAttribute('data-figure')) { const caption = node.getElementsByTagName("figcaption")[0]; text = caption.textContent; } else { diff --git a/modules/plugins/image/index.js b/modules/plugins/image/index.js index f85f30b..f5ad482 100644 --- a/modules/plugins/image/index.js +++ b/modules/plugins/image/index.js @@ -1,6 +1,10 @@ import Block from "./Image"; import ReturnKey from "./ImageReturnKey"; import './Image.scss'; + +// figure +import BspaceKey from '../shared/FigureBspaceKey'; +import DeleteKey from '../shared/FigureDeleteKey'; import '../shared/Figure.scss'; import '../shared/FigCaption.scss'; @@ -11,6 +15,8 @@ const Plugin = { installKeys(config) { config.use(ReturnKey, { before: 'return' }); + config.use(BspaceKey, { before: 'bspace' }); + config.use(DeleteKey, { before: 'delete' }); }, }; diff --git a/modules/plugins/shared/FigureBspaceKey.js b/modules/plugins/shared/FigureBspaceKey.js new file mode 100644 index 0000000..626a31b --- /dev/null +++ b/modules/plugins/shared/FigureBspaceKey.js @@ -0,0 +1,48 @@ +import DeleteFigure from './lib/DeleteFigure'; + +const KEY_CODES = { 'bspace': 8 }; + +class FigureBspaceKey { + constructor(content, selection) { + this.content = content; + this.selection = selection; + } + + static getName() { + return 'bspace'; + } + + // return or ctrl+m + matches(event) { + return event.keyCode === KEY_CODES.bspace; + } + + down(callback) { + const guids = this.selection.guids(); + const offsets = this.selection.offsets(); + + if (this.selection.isFigure() && !this.selection.isCaption()) { + const command = new DeleteFigure(this.content); + const results = command.execute(guids); + callback({ + content: results.content, + position: results.position, + stopPropagation: !!results, + preventDefault: !!results, + emit: true + }); + } else { + callback({ content: this.content }); + } + } + + up(callback) { + callback({ content: this.content }); + } + + _deleteFigure() { + return new DeleteFigure(this.content); + } +} + +export default FigureBspaceKey; diff --git a/modules/plugins/shared/FigureDeleteKey.js b/modules/plugins/shared/FigureDeleteKey.js new file mode 100644 index 0000000..4326cc3 --- /dev/null +++ b/modules/plugins/shared/FigureDeleteKey.js @@ -0,0 +1,43 @@ +import DeleteFigure from './lib/DeleteFigure'; + +const KEY_CODES = { 'delete': 46 }; + +class FigureDeleteKey { + constructor(content, selection) { + this.content = content; + this.selection = selection; + } + + static getName() { + return 'figure-delete'; + } + + matches(event) { + return event.keyCode === KEY_CODES.delete; + } + + down(callback) { + const guids = this.selection.guids(); + const offsets = this.selection.offsets(); + + if (this.selection.isFigure() && !this.selection.isCaption()) { + const command = new DeleteFigure(this.content); + const results = command.execute(guids); + callback({ + content: results.content, + position: results.position, + stopPropagation: !!results, + preventDefault: !!results, + emit: true + }); + } else { + callback({ content: this.content }); + } + } + + up(callback) { + callback({ content: this.content }); + } +} + +export default FigureDeleteKey; diff --git a/modules/plugins/shared/__tests__/BspaceFigureKey-test.js b/modules/plugins/shared/__tests__/BspaceFigureKey-test.js new file mode 100644 index 0000000..e69de29 diff --git a/modules/plugins/shared/__tests__/DeleteFigureKey-test.js b/modules/plugins/shared/__tests__/DeleteFigureKey-test.js new file mode 100644 index 0000000..e69de29 diff --git a/modules/helpers/Manipulation/DeleteFigure.js b/modules/plugins/shared/lib/DeleteFigure.js similarity index 86% rename from modules/helpers/Manipulation/DeleteFigure.js rename to modules/plugins/shared/lib/DeleteFigure.js index 33c2aa2..0ef9a2a 100644 --- a/modules/helpers/Manipulation/DeleteFigure.js +++ b/modules/plugins/shared/lib/DeleteFigure.js @@ -1,6 +1,6 @@ import Immutable from 'immutable'; -import ContentFinder from '../ContentFinder'; -import Guid from '../Guid'; +import ContentFinder from '../../../helpers/ContentFinder'; +import Guid from '../../../helpers/Guid'; class DeleteFigure { constructor(content) { diff --git a/modules/helpers/Manipulation/__tests__/DeleteFigure-test.js b/modules/plugins/shared/lib/__tests__/DeleteFigure-test.js similarity index 100% rename from modules/helpers/Manipulation/__tests__/DeleteFigure-test.js rename to modules/plugins/shared/lib/__tests__/DeleteFigure-test.js diff --git a/modules/plugins/youtube/index.js b/modules/plugins/youtube/index.js index 50ed146..8fdcaa4 100644 --- a/modules/plugins/youtube/index.js +++ b/modules/plugins/youtube/index.js @@ -1,6 +1,10 @@ import Block from "./YouTube"; import ReturnKey from "./YouTubeReturnKey"; import './YouTube.scss'; + +// figure +import BspaceKey from '../shared/FigureBspaceKey'; +import DeleteKey from '../shared/FigureDeleteKey'; import '../shared/Figure.scss'; import '../shared/FigCaption.scss'; @@ -11,6 +15,8 @@ const Plugin = { installKeys(config) { config.use(ReturnKey, { before: 'return' }); + config.use(BspaceKey, { before: 'bspace' }); + config.use(DeleteKey, { before: 'delete' }); }, };