From cd7282bc9766ced41d0df568f79640f997596533 Mon Sep 17 00:00:00 2001 From: chris <kokosias@yahoo.gr> Date: Fri, 30 Jul 2021 11:49:31 +0300 Subject: [PATCH] add helpers and disable command --- .../MultipleChoiceQuestion.js | 24 ++++++++++---- .../components/QuestionComponent.js | 28 ++-------------- .../components/ToolBarBtn.js | 4 +-- .../helpers/helpers.js | 33 +++++++++++++++++++ 4 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 editors/demo/src/HHMI/MultipleChoiceQuestionService/helpers/helpers.js diff --git a/editors/demo/src/HHMI/MultipleChoiceQuestionService/MultipleChoiceQuestion.js b/editors/demo/src/HHMI/MultipleChoiceQuestionService/MultipleChoiceQuestion.js index 33e965293..02b0d0d71 100644 --- a/editors/demo/src/HHMI/MultipleChoiceQuestionService/MultipleChoiceQuestion.js +++ b/editors/demo/src/HHMI/MultipleChoiceQuestionService/MultipleChoiceQuestion.js @@ -5,6 +5,7 @@ import { Tools } from 'wax-prosemirror-services'; import { Commands } from 'wax-prosemirror-utilities'; import { Fragment } from 'prosemirror-model'; import { v4 as uuidv4 } from 'uuid'; +import helpers from './helpers/helpers'; import ToolBarBtn from './components/ToolBarBtn'; 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( - { id: uuidv4() }, + { id: newAnswerId }, Fragment.empty, ); dispatch(tr.replaceSelectionWith(answerOption)); + setTimeout(() => { + helpers.createEmptyParagraph(context, newAnswerId); + }, 100); }; @injectable() @@ -30,7 +35,7 @@ class MultipleChoiceQuestion extends Tools { name = 'Multiple Choice'; get run() { - return view => { + return (view, context) => { checkifEmpty(view); const { state, dispatch } = view; @@ -42,7 +47,7 @@ class MultipleChoiceQuestion extends Tools { setTimeout(() => { state.doc.nodesBetween(from, to, (node, pos) => { if (node.type.name === 'question_wrapper') { - createQuestion(state, dispatch, tr); + createQuestion(state, dispatch, tr, context); } else { tr.setBlockType( from, @@ -53,7 +58,7 @@ class MultipleChoiceQuestion extends Tools { }, ); if (!tr.steps.length) return false; - createQuestion(state, dispatch, tr); + createQuestion(state, dispatch, tr, context); } }); state.schema.nodes.question_wrapper.spec.atom = true; @@ -66,7 +71,14 @@ class MultipleChoiceQuestion extends Tools { } 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() { diff --git a/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/QuestionComponent.js b/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/QuestionComponent.js index 17c2f540b..ab009e895 100644 --- a/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/QuestionComponent.js +++ b/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/QuestionComponent.js @@ -6,12 +6,10 @@ import { WaxContext } from 'wax-prosemirror-core'; import { PlusSquareOutlined, DeleteOutlined } from '@ant-design/icons'; import { Fragment } from 'prosemirror-model'; import { v4 as uuidv4 } from 'uuid'; - +import helpers from '../helpers/helpers'; import EditorComponent from './EditorComponent'; -import FeedbackComponent from './FeedbackComponent'; import SwitchComponent from './SwitchComponent'; import Button from './Button'; -import { nodes } from 'prosemirror-schema-basic'; const Wrapper = styled.div` display: flex; @@ -140,29 +138,7 @@ export default ({ node, view, getPos }) => { ); // create Empty Paragraph setTimeout(() => { - 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, - ), - ), - ); + helpers.createEmptyParagraph(context, newAnswerId); }, 100); } } diff --git a/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/ToolBarBtn.js b/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/ToolBarBtn.js index 9c55715f5..0f0fbe822 100644 --- a/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/ToolBarBtn.js +++ b/editors/demo/src/HHMI/MultipleChoiceQuestionService/components/ToolBarBtn.js @@ -14,7 +14,7 @@ const StyledButton = styled(MenuButton)` const ToolBarBtn = ({ view = {}, item }) => { const { active, icon, label, onlyOnMain, run, select, title } = item; - + const context = useContext(WaxContext); const { view: { main }, activeViewId, @@ -45,7 +45,7 @@ const ToolBarBtn = ({ view = {}, item }) => { label={label} onMouseDown={e => { e.preventDefault(); - run(main); + run(main, context); }} title={title} /> diff --git a/editors/demo/src/HHMI/MultipleChoiceQuestionService/helpers/helpers.js b/editors/demo/src/HHMI/MultipleChoiceQuestionService/helpers/helpers.js new file mode 100644 index 000000000..4962ea2a9 --- /dev/null +++ b/editors/demo/src/HHMI/MultipleChoiceQuestionService/helpers/helpers.js @@ -0,0 +1,33 @@ +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, +}; -- GitLab