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

wrap

parent eaaa3f63
No related branches found
No related tags found
No related merge requests found
import { injectable } from 'inversify'; import { injectable } from 'inversify';
import { wrapIn } from 'prosemirror-commands'; import { wrapIn } from 'prosemirror-commands';
import { NodeSelection } from 'prosemirror-state'; import { liftTarget, findWrapping } from 'prosemirror-transform';
import { Mapping, RemoveMarkStep, ReplaceStep } from 'prosemirror-transform';
import Tools from '../lib/Tools'; import Tools from '../lib/Tools';
@injectable() @injectable()
...@@ -14,34 +13,26 @@ export default class OENContainersTool extends Tools { ...@@ -14,34 +13,26 @@ export default class OENContainersTool extends Tools {
return (state, dispatch, className) => { return (state, dispatch, className) => {
const { from, to } = state.selection; const { from, to } = state.selection;
let isInOenContainer = false; let isInOenContainer = false;
let OENContainer = '';
let position = 0;
state.doc.nodesBetween(from, to, (node, pos) => { state.doc.nodesBetween(from, to, (node, pos) => {
if (node.type.name === 'oen_container') { if (node.type.name === 'oen_container') {
isInOenContainer = true; isInOenContainer = true;
OENContainer = node;
position = pos;
console.log(pos);
} }
}); });
if (isInOenContainer) { if (isInOenContainer) {
const map = new Mapping(); const { $from, $to } = state.selection;
const newNode = JSON.parse(JSON.stringify(OENContainer)); const range = $from.blockRange($to);
OENContainer.attrs.class = className; const target = range && liftTarget(range);
console.log('replace', OENContainer); if (target == null) return false;
newNode.attrs = { dispatch(state.tr.lift(range, target));
...newNode.attrs, // const wrapping =
class: className, // range &&
}; // findWrapping(range, state.config.schema.nodes.oen_container, {
console.log(newNode); // class: className,
state.tr.setSelection(NodeSelection.create(state.doc, position)); // });
state.tr.replaceSelectionWith(OENContainer); // if (!wrapping) return false;
// state.tr.setNodeMarkup(map.map(position), null, { // if (dispatch) dispatch(state.tr.wrap(range, wrapping).scrollIntoView());
// ...OENContainer.attrs,
// class: className,
// });
dispatch(state.tr);
} else { } else {
const node = className === 'section' ? 'oen_section' : 'oen_container'; const node = className === 'section' ? 'oen_section' : 'oen_container';
......
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