Skip to content
Snippets Groups Projects
Commit 7ab56516 authored by chris's avatar chris
Browse files

check heading level from config

parent 6700eda6
No related branches found
No related tags found
No related merge requests found
Showing with 92 additions and 9 deletions
......@@ -121,6 +121,7 @@ export default {
// OrderedListService: { subList: false },
// BulletListService: { subList: false },
// JoinUpService: { subList: false },
OENContainersService: [
{
groupHeader: 'Core Elements',
......@@ -139,7 +140,7 @@ export default {
},
{
displayName: 'outline',
headingLevel: 2,
headingLevel: 3,
nestedHeadingLevel: null,
className: 'outline',
},
......
......@@ -4,6 +4,7 @@ import { isEmpty } from 'lodash';
import { LeftSideButton } from 'wax-prosemirror-components';
import { Commands } from 'wax-prosemirror-utilities';
import Tools from '../../lib/Tools';
import checkLevelFromConfig from './checkLevelFromConfig';
@injectable()
export default class Heading2 extends Tools {
......@@ -37,6 +38,15 @@ export default class Heading2 extends Tools {
select = (state, activeViewId) => {
if (activeViewId !== 'main') return false;
if (this.config) {
const allowedLevel = checkLevelFromConfig(
state,
activeViewId,
this.config,
);
if (allowedLevel > 2) return false;
}
return true;
};
......
......@@ -4,6 +4,7 @@ import { isEmpty } from 'lodash';
import { LeftSideButton } from 'wax-prosemirror-components';
import { Commands } from 'wax-prosemirror-utilities';
import Tools from '../../lib/Tools';
import checkLevelFromConfig from './checkLevelFromConfig';
@injectable()
export default class Heading3 extends Tools {
......@@ -37,6 +38,15 @@ export default class Heading3 extends Tools {
select = (state, activeViewId) => {
if (activeViewId !== 'main') return false;
if (this.config) {
const allowedLevel = checkLevelFromConfig(
state,
activeViewId,
this.config,
);
if (allowedLevel > 3) return false;
}
return true;
};
......
......@@ -4,6 +4,7 @@ import { isEmpty } from 'lodash';
import { LeftSideButton } from 'wax-prosemirror-components';
import { Commands } from 'wax-prosemirror-utilities';
import Tools from '../../lib/Tools';
import checkLevelFromConfig from './checkLevelFromConfig';
@injectable()
export default class Heading4 extends Tools {
......@@ -37,6 +38,15 @@ export default class Heading4 extends Tools {
select = (state, activeViewId) => {
if (activeViewId !== 'main') return false;
if (this.config) {
const allowedLevel = checkLevelFromConfig(
state,
activeViewId,
this.config,
);
if (allowedLevel > 4) return false;
}
return true;
};
......
......@@ -4,6 +4,7 @@ import { isEmpty } from 'lodash';
import { LeftSideButton } from 'wax-prosemirror-components';
import { Commands } from 'wax-prosemirror-utilities';
import Tools from '../../lib/Tools';
import checkLevelFromConfig from './checkLevelFromConfig';
@injectable()
export default class Heading2 extends Tools {
......@@ -37,6 +38,15 @@ export default class Heading2 extends Tools {
select = (state, activeViewId) => {
if (activeViewId !== 'main') return false;
if (this.config) {
const allowedLevel = checkLevelFromConfig(
state,
activeViewId,
this.config,
);
if (allowedLevel > 5) return false;
}
return true;
};
......
......@@ -7,11 +7,21 @@ import Service from '../../Service';
class HeadingService extends Service {
register() {
this.container.bind('Heading2').to(Heading2);
this.container.bind('Heading3').to(Heading3);
this.container.bind('Heading4').to(Heading4);
this.container.bind('Heading5').to(Heading5);
this.container.bind('Heading6').to(Heading6);
this.container.bind('Heading2').toDynamicValue(() => {
return new Heading2(this.config.get('config.OENContainersService'));
});
this.container.bind('Heading3').toDynamicValue(() => {
return new Heading3(this.config.get('config.OENContainersService'));
});
this.container.bind('Heading4').toDynamicValue(() => {
return new Heading4(this.config.get('config.OENContainersService'));
});
this.container.bind('Heading5').toDynamicValue(() => {
return new Heading5(this.config.get('config.OENContainersService'));
});
this.container.bind('Heading6').toDynamicValue(() => {
return new Heading6(this.config.get('config.OENContainersService'));
});
const createNode = this.container.get('CreateNode');
createNode({
heading2: {
......
import { result, find } from 'lodash';
const checkLevelFromConfig = (state, activeViewId, config) => {
let level = 0;
const { from, to } = state.selection;
state.doc.nodesBetween(from, to, (node, pos) => {
if (node.type.name === 'oen_container') {
config.forEach(group => {
const a = result(
find(group.items, item => {
return item.className === node.attrs.class;
}),
'headingLevel',
);
if (a) level = a;
});
}
});
return level;
};
export default checkLevelFromConfig;
......@@ -4,6 +4,7 @@ import { injectable } from 'inversify';
import { TitleButton } from 'wax-prosemirror-components';
import { Commands, DocumentHelpers } from 'wax-prosemirror-utilities';
import Tools from '../../lib/Tools';
import checkLevelFromConfig from '../HeadingService/checkLevelFromConfig';
@injectable()
export default class Title extends Tools {
......@@ -31,6 +32,15 @@ export default class Title extends Tools {
selection: { $from, $to },
} = state;
if (this.config) {
const allowedLevel = checkLevelFromConfig(
state,
activeViewId,
this.config,
);
if (allowedLevel > 1) return false;
}
const titleCounter = DocumentHelpers.findChildrenByType(
state.doc,
state.config.schema.nodes.title,
......
......@@ -3,10 +3,10 @@ import Service from '../../Service';
import Title from './Title';
class TitleService extends Service {
name = 'TitleService';
register() {
this.container.bind('Title').to(Title);
this.container.bind('Title').toDynamicValue(() => {
return new Title(this.config.get('config.OENContainersService'));
});
const createNode = this.container.get('CreateNode');
createNode(
{
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment