From 8f2be98cbd85885a12ec61712fbf8e6d02957104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hermann=20K=C3=A4ser?= Date: Tue, 12 Feb 2019 10:45:22 -0500 Subject: [PATCH] Adds `modifyBlocks` option to make structure read-only Setting the option to false won't allow users to add/remove or re-order blocks. --- .../units/block/unmodifiable.spec.js | 38 +++++++++++++++++++ src/block.js | 16 ++++---- .../scribe-text-block-plugin.js | 6 ++- src/config.js | 3 ++ src/templates/block-addition-top.js | 4 ++ src/templates/block-addition.js | 4 ++ 6 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 spec/javascripts/units/block/unmodifiable.spec.js diff --git a/spec/javascripts/units/block/unmodifiable.spec.js b/spec/javascripts/units/block/unmodifiable.spec.js new file mode 100644 index 000000000..2e6269991 --- /dev/null +++ b/spec/javascripts/units/block/unmodifiable.spec.js @@ -0,0 +1,38 @@ +"use strict"; + +var config = require('../../../../src/config'); + +describe("UnmodifiableBlock Block", function(){ + + var element, editor, block, testHandler; + + beforeEach(function(){ + element = global.createBaseElement(); + editor = new SirTrevor.Editor({ el: element }); + + config.defaults.modifyBlocks = false; + + SirTrevor.Blocks.UnmodifiableBlock = SirTrevor.Block.extend({}); + + block = new SirTrevor.Blocks.UnmodifiableBlock({}, editor.ID, editor.mediator); + }); + + afterEach(function(){ + delete SirTrevor.Blocks.UnmodifiableBlock; + }); + + describe("render", function(){ + + it("has no reorder control", function(){ + expect(block.el.querySelectorAll('.st-block-ui-btn__reorder').length) + .toBe(0); + }); + + it("has no delete control", function(){ + expect(block.el.querySelectorAll('.st-block-ui-btn__delete').length) + .toBe(0); + }); + + }); + +}); diff --git a/src/block.js b/src/block.js index 4264f8e5f..f520e1930 100644 --- a/src/block.js +++ b/src/block.js @@ -331,17 +331,19 @@ Object.assign(Block.prototype, SimpleBlock.fn, require('./block-validations'), { //Init functions for adding functionality _initUIComponents: function() { - this.addDeleteControls(); + if (config.defaults.modifyBlocks) { + this.addDeleteControls(); - this.positioner = new BlockPositioner(this.el, this.mediator); + this.positioner = new BlockPositioner(this.el, this.mediator); - this._withUIComponent(this.positioner, '.st-block-ui-btn__reorder', - this.onPositionerClick); + this._withUIComponent(this.positioner, '.st-block-ui-btn__reorder', + this.onPositionerClick); - this._withUIComponent(new BlockReorder(this.el, this.mediator)); + this._withUIComponent(new BlockReorder(this.el, this.mediator)); - this._withUIComponent(new BlockDeletion(), '.st-block-ui-btn__delete', - this.onDeleteClick); + this._withUIComponent(new BlockDeletion(), '.st-block-ui-btn__delete', + this.onDeleteClick); + } this.onFocus(); this.onBlur(); diff --git a/src/blocks/scribe-plugins/scribe-text-block-plugin.js b/src/blocks/scribe-plugins/scribe-text-block-plugin.js index 738629282..0b7e1141a 100644 --- a/src/blocks/scribe-plugins/scribe-text-block-plugin.js +++ b/src/blocks/scribe-plugins/scribe-text-block-plugin.js @@ -6,6 +6,8 @@ var { selectToEnd } = require('./shared.js'); +var config = require('./../../config'); + var ScribeTextBlockPlugin = function(block) { return function(scribe) { @@ -53,7 +55,7 @@ var ScribeTextBlockPlugin = function(block) { scribe.el.addEventListener('keydown', function(ev) { - if (block.supressKeyListeners) { + if (block.supressKeyListeners || !config.defaults.modifyBlocks) { return; } @@ -90,7 +92,7 @@ var ScribeTextBlockPlugin = function(block) { scribe.el.addEventListener('keyup', function(ev) { - if (block.supressKeyListeners) { + if (block.supressKeyListeners || !config.defaults.modifyBlocks) { return; } diff --git a/src/config.js b/src/config.js index d2252c34a..4fadff5f6 100644 --- a/src/config.js +++ b/src/config.js @@ -32,6 +32,9 @@ module.exports = { instances: [], defaults: { + // If set to false will not allow users to add/remove or move blocks + modifyBlocks: true, + defaultType: false, spinner: { className: 'st-spinner', diff --git a/src/templates/block-addition-top.js b/src/templates/block-addition-top.js index 249091175..e5414c83d 100644 --- a/src/templates/block-addition-top.js +++ b/src/templates/block-addition-top.js @@ -3,6 +3,10 @@ var config = require('../config'); module.exports = () => { + if (!config.defaults.modifyBlocks) { + return ''; + } + return `
diff --git a/src/templates/block-addition.js b/src/templates/block-addition.js index 9524d3e7b..1e6b72c2c 100644 --- a/src/templates/block-addition.js +++ b/src/templates/block-addition.js @@ -3,6 +3,10 @@ var config = require('../config'); module.exports = () => { + if (!config.defaults.modifyBlocks) { + return ''; + } + return `