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

add helpers and disable command

parent 8ec79bf1
No related branches found
No related tags found
1 merge request!306Answer nodeview
...@@ -5,6 +5,7 @@ import { Tools } from 'wax-prosemirror-services'; ...@@ -5,6 +5,7 @@ import { Tools } from 'wax-prosemirror-services';
import { Commands } from 'wax-prosemirror-utilities'; import { Commands } from 'wax-prosemirror-utilities';
import { Fragment } from 'prosemirror-model'; import { Fragment } from 'prosemirror-model';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import helpers from './helpers/helpers';
import ToolBarBtn from './components/ToolBarBtn'; import ToolBarBtn from './components/ToolBarBtn';
const checkifEmpty = view => { const checkifEmpty = view => {
...@@ -15,12 +16,16 @@ const checkifEmpty = view => { ...@@ -15,12 +16,16 @@ const checkifEmpty = view => {
}); });
}; };
const createQuestion = (state, dispatch, tr) => { const createQuestion = (state, dispatch, tr, context) => {
const newAnswerId = uuidv4();
const answerOption = state.config.schema.nodes.multiple_choice.create( const answerOption = state.config.schema.nodes.multiple_choice.create(
{ id: uuidv4() }, { id: newAnswerId },
Fragment.empty, Fragment.empty,
); );
dispatch(tr.replaceSelectionWith(answerOption)); dispatch(tr.replaceSelectionWith(answerOption));
setTimeout(() => {
helpers.createEmptyParagraph(context, newAnswerId);
}, 100);
}; };
@injectable() @injectable()
...@@ -30,7 +35,7 @@ class MultipleChoiceQuestion extends Tools { ...@@ -30,7 +35,7 @@ class MultipleChoiceQuestion extends Tools {
name = 'Multiple Choice'; name = 'Multiple Choice';
get run() { get run() {
return view => { return (view, context) => {
checkifEmpty(view); checkifEmpty(view);
const { state, dispatch } = view; const { state, dispatch } = view;
...@@ -42,7 +47,7 @@ class MultipleChoiceQuestion extends Tools { ...@@ -42,7 +47,7 @@ class MultipleChoiceQuestion extends Tools {
setTimeout(() => { setTimeout(() => {
state.doc.nodesBetween(from, to, (node, pos) => { state.doc.nodesBetween(from, to, (node, pos) => {
if (node.type.name === 'question_wrapper') { if (node.type.name === 'question_wrapper') {
createQuestion(state, dispatch, tr); createQuestion(state, dispatch, tr, context);
} else { } else {
tr.setBlockType( tr.setBlockType(
from, from,
...@@ -53,7 +58,7 @@ class MultipleChoiceQuestion extends Tools { ...@@ -53,7 +58,7 @@ class MultipleChoiceQuestion extends Tools {
}, },
); );
if (!tr.steps.length) return false; if (!tr.steps.length) return false;
createQuestion(state, dispatch, tr); createQuestion(state, dispatch, tr, context);
} }
}); });
state.schema.nodes.question_wrapper.spec.atom = true; state.schema.nodes.question_wrapper.spec.atom = true;
...@@ -66,7 +71,14 @@ class MultipleChoiceQuestion extends Tools { ...@@ -66,7 +71,14 @@ class MultipleChoiceQuestion extends Tools {
} }
select = (state, activeViewId) => { select = (state, activeViewId) => {
return true; let status = true;
const { from, to } = state.selection;
state.doc.nodesBetween(from, to, (node, pos) => {
if (node.type.name === 'question_wrapper') {
status = false;
}
});
return status;
}; };
get enable() { get enable() {
......
...@@ -6,12 +6,10 @@ import { WaxContext } from 'wax-prosemirror-core'; ...@@ -6,12 +6,10 @@ import { WaxContext } from 'wax-prosemirror-core';
import { PlusSquareOutlined, DeleteOutlined } from '@ant-design/icons'; import { PlusSquareOutlined, DeleteOutlined } from '@ant-design/icons';
import { Fragment } from 'prosemirror-model'; import { Fragment } from 'prosemirror-model';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import helpers from '../helpers/helpers';
import EditorComponent from './EditorComponent'; import EditorComponent from './EditorComponent';
import FeedbackComponent from './FeedbackComponent';
import SwitchComponent from './SwitchComponent'; import SwitchComponent from './SwitchComponent';
import Button from './Button'; import Button from './Button';
import { nodes } from 'prosemirror-schema-basic';
const Wrapper = styled.div` const Wrapper = styled.div`
display: flex; display: flex;
...@@ -140,29 +138,7 @@ export default ({ node, view, getPos }) => { ...@@ -140,29 +138,7 @@ export default ({ node, view, getPos }) => {
); );
// create Empty Paragraph // create Empty Paragraph
setTimeout(() => { setTimeout(() => {
if (context.view[newAnswerId]) { helpers.createEmptyParagraph(context, newAnswerId);
context.view[newAnswerId].dispatch(
context.view[newAnswerId].state.tr.setSelection(
TextSelection.between(
context.view[newAnswerId].state.selection.$anchor,
context.view[newAnswerId].state.selection.$head,
),
),
);
let type = context.view.main.state.schema.nodes.paragraph;
context.view[newAnswerId].dispatch(
context.view[newAnswerId].state.tr.insert(0, type.create()),
);
}
context.view[newAnswerId].dispatch(
context.view[newAnswerId].state.tr.setSelection(
TextSelection.between(
context.view[newAnswerId].state.selection.$anchor,
context.view[newAnswerId].state.selection.$head,
),
),
);
}, 100); }, 100);
} }
} }
......
...@@ -14,7 +14,7 @@ const StyledButton = styled(MenuButton)` ...@@ -14,7 +14,7 @@ const StyledButton = styled(MenuButton)`
const ToolBarBtn = ({ view = {}, item }) => { const ToolBarBtn = ({ view = {}, item }) => {
const { active, icon, label, onlyOnMain, run, select, title } = item; const { active, icon, label, onlyOnMain, run, select, title } = item;
const context = useContext(WaxContext);
const { const {
view: { main }, view: { main },
activeViewId, activeViewId,
...@@ -45,7 +45,7 @@ const ToolBarBtn = ({ view = {}, item }) => { ...@@ -45,7 +45,7 @@ const ToolBarBtn = ({ view = {}, item }) => {
label={label} label={label}
onMouseDown={e => { onMouseDown={e => {
e.preventDefault(); e.preventDefault();
run(main); run(main, context);
}} }}
title={title} title={title}
/> />
......
import { v4 as uuidv4 } from 'uuid';
import { TextSelection } from 'prosemirror-state';
import { Fragment } from 'prosemirror-model';
const createEmptyParagraph = (context, newAnswerId) => {
if (context.view[newAnswerId]) {
context.view[newAnswerId].dispatch(
context.view[newAnswerId].state.tr.setSelection(
TextSelection.between(
context.view[newAnswerId].state.selection.$anchor,
context.view[newAnswerId].state.selection.$head,
),
),
);
let type = context.view.main.state.schema.nodes.paragraph;
context.view[newAnswerId].dispatch(
context.view[newAnswerId].state.tr.insert(0, type.create()),
);
}
context.view[newAnswerId].dispatch(
context.view[newAnswerId].state.tr.setSelection(
TextSelection.between(
context.view[newAnswerId].state.selection.$anchor,
context.view[newAnswerId].state.selection.$head,
),
),
);
};
export default {
createEmptyParagraph,
};
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