From 0fa350aab8c0231f19aa4b8580947962e856aeef Mon Sep 17 00:00:00 2001 From: chris <kokosias@yahoo.gr> Date: Mon, 17 Jan 2022 03:07:23 +0200 Subject: [PATCH] create question/answer component --- .../src/EssayService/EssayAnswerNodeView.js | 2 +- .../src/EssayService/EssayQuestion.js | 73 ++++++++++++++++++- .../src/EssayService/EssayQuestionNodeView.js | 2 +- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/wax-prosemirror-services/src/EssayService/EssayAnswerNodeView.js b/wax-prosemirror-services/src/EssayService/EssayAnswerNodeView.js index 8d77d716b..01c9fd33d 100644 --- a/wax-prosemirror-services/src/EssayService/EssayAnswerNodeView.js +++ b/wax-prosemirror-services/src/EssayService/EssayAnswerNodeView.js @@ -19,7 +19,7 @@ export default class EssayAnswerNodeView extends AbstractNodeView { } static name() { - return 'essay'; + return 'essay_answer'; } update(node) { diff --git a/wax-prosemirror-services/src/EssayService/EssayQuestion.js b/wax-prosemirror-services/src/EssayService/EssayQuestion.js index b918f8251..dc85658ad 100644 --- a/wax-prosemirror-services/src/EssayService/EssayQuestion.js +++ b/wax-prosemirror-services/src/EssayService/EssayQuestion.js @@ -3,6 +3,9 @@ import { isEmpty } from 'lodash'; import { injectable } from 'inversify'; import { v4 as uuidv4 } from 'uuid'; import { Commands } from 'wax-prosemirror-utilities'; +import { Fragment } from 'prosemirror-model'; +import { TextSelection } from 'prosemirror-state'; +import { findWrapping } from 'prosemirror-transform'; import ToolBarBtn from './components/ToolBarBtn'; import Tools from '../lib/Tools'; @@ -20,6 +23,34 @@ const checkifEmpty = view => { } }; +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, + ), + ), + ); + if (context.view[newAnswerId].dispatch) { + const 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, + ), + ), + ); + context.view[newAnswerId].focus(); + } +}; + @injectable() class EssayQuestion extends Tools { title = 'Add Essay Question'; @@ -28,9 +59,47 @@ class EssayQuestion extends Tools { label = 'Essay'; get run() { - return (main, dispatch) => { + return (main, context) => { checkifEmpty(main); - //const { state, dispatch } = main; + const { state, dispatch } = main; + /* Create Wrapping */ + const { $from, $to } = state.selection; + const range = $from.blockRange($to); + const { tr } = main.state; + + const wrapping = + range && + findWrapping(range, main.state.config.schema.nodes.essay_container, { + id: uuidv4(), + }); + if (!wrapping) return false; + tr.wrap(range, wrapping); + + // const map = tr.mapping.maps[0]; + // let newPos = 0; + // map.forEach((_from, _to, _newFrom, newTo) => { + // newPos = newTo; + // }); + + tr.setSelection(TextSelection.create(tr.doc, range.$to.pos)); + + const essayQuestion = main.state.config.schema.nodes.essay_question.create( + { id: uuidv4() }, + Fragment.empty, + ); + const essayAnswer = main.state.config.schema.nodes.essay_answer.create( + { id: uuidv4() }, + Fragment.empty, + ); + + tr.replaceSelectionWith(essayQuestion); + tr.replaceSelectionWith(essayAnswer); + dispatch(tr); + + // setTimeout(() => { + // createEmptyParagraph(context, essayQuestion.attrs.id); + // createEmptyParagraph(context, essayAnswer.attrs.id); + // }, 50); }; } diff --git a/wax-prosemirror-services/src/EssayService/EssayQuestionNodeView.js b/wax-prosemirror-services/src/EssayService/EssayQuestionNodeView.js index b10ec5655..867d0dcd1 100644 --- a/wax-prosemirror-services/src/EssayService/EssayQuestionNodeView.js +++ b/wax-prosemirror-services/src/EssayService/EssayQuestionNodeView.js @@ -19,7 +19,7 @@ export default class EssayQuestionNodeView extends AbstractNodeView { } static name() { - return 'essay'; + return 'essay_question'; } update(node) { -- GitLab