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

Adds modifyBlocks option to make structure read-only #570

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
38 changes: 38 additions & 0 deletions spec/javascripts/units/block/unmodifiable.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});

});

});
16 changes: 9 additions & 7 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions src/blocks/scribe-plugins/scribe-text-block-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var {
selectToEnd
} = require('./shared.js');

var config = require('./../../config');

var ScribeTextBlockPlugin = function(block) {
return function(scribe) {

Expand Down Expand Up @@ -53,7 +55,7 @@ var ScribeTextBlockPlugin = function(block) {

scribe.el.addEventListener('keydown', function(ev) {

if (block.supressKeyListeners) {
if (block.supressKeyListeners || !config.defaults.modifyBlocks) {
return;
}

Expand Down Expand Up @@ -90,7 +92,7 @@ var ScribeTextBlockPlugin = function(block) {

scribe.el.addEventListener('keyup', function(ev) {

if (block.supressKeyListeners) {
if (block.supressKeyListeners || !config.defaults.modifyBlocks) {
return;
}

Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions src/templates/block-addition-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
var config = require('../config');

module.exports = () => {
if (!config.defaults.modifyBlocks) {
return '';
}

return `
<div class="st-block-addition-top">
<div class="st-block-addition-top__button" type="button"></div>
Expand Down
4 changes: 4 additions & 0 deletions src/templates/block-addition.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
var config = require('../config');

module.exports = () => {
if (!config.defaults.modifyBlocks) {
return '';
}

return `
<button class="st-block-addition" type="button">
<span class="st-block-addition__button">
Expand Down