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

create first option

parent c73eb2ad
No related branches found
No related tags found
1 merge request!307Answer nodeview
...@@ -28,31 +28,31 @@ class MultipleChoiceQuestion extends Tools { ...@@ -28,31 +28,31 @@ class MultipleChoiceQuestion extends Tools {
return (view, context) => { return (view, context) => {
checkifEmpty(view); checkifEmpty(view);
const { state, dispatch } = view; // const { state, dispatch } = view;
const { tr } = state; // const { tr } = state;
/* Create Wrapping */ // /* Create Wrapping */
let { $from, $to } = state.selection; // let { $from, $to } = state.selection;
let range = $from.blockRange($to), // let range = $from.blockRange($to),
wrapping = // wrapping =
range && // range &&
findWrapping( // findWrapping(
range, // range,
state.config.schema.nodes.multiple_choice_container, // state.config.schema.nodes.multiple_choice_container,
{}, // {},
); // );
if (!wrapping) return false; // if (!wrapping) return false;
tr.wrap(range, wrapping).scrollIntoView(); // tr.wrap(range, wrapping).scrollIntoView();
/* create First Option */ // /* create First Option */
const newAnswerId = uuidv4(); // const newAnswerId = uuidv4();
const answerOption = state.config.schema.nodes.multiple_choice.create( // const answerOption = state.config.schema.nodes.multiple_choice.create(
{ id: newAnswerId }, // { id: newAnswerId },
Fragment.empty, // Fragment.empty,
); // );
dispatch(tr.replaceSelectionWith(answerOption)); // dispatch(tr.replaceSelectionWith(answerOption));
setTimeout(() => { // setTimeout(() => {
helpers.createEmptyParagraph(context, newAnswerId); // helpers.createEmptyParagraph(context, newAnswerId);
}, 100); // }, 100);
}; };
} }
......
...@@ -3,7 +3,13 @@ import React, { useContext, useMemo } from 'react'; ...@@ -3,7 +3,13 @@ import React, { useContext, useMemo } from 'react';
import { WaxContext } from 'wax-prosemirror-core'; import { WaxContext } from 'wax-prosemirror-core';
import styled, { css } from 'styled-components'; import styled, { css } from 'styled-components';
import { MenuButton } from 'wax-prosemirror-components'; import { MenuButton } from 'wax-prosemirror-components';
import { Commands } from 'wax-prosemirror-utilities';
import { Fragment } from 'prosemirror-model';
import { TextSelection } from 'prosemirror-state';
import { wrapIn } from 'prosemirror-commands';
import helpers from '../helpers/helpers';
import { v4 as uuidv4 } from 'uuid';
const activeStyles = css` const activeStyles = css`
pointer-events: none; pointer-events: none;
`; `;
...@@ -36,6 +42,36 @@ const ToolBarBtn = ({ view = {}, item }) => { ...@@ -36,6 +42,36 @@ const ToolBarBtn = ({ view = {}, item }) => {
let isDisabled = !select(state, activeViewId, activeView); let isDisabled = !select(state, activeViewId, activeView);
if (!isEditable) isDisabled = true; if (!isEditable) isDisabled = true;
const createOption = () => {
const { state, dispatch } = main;
/* Create Wrapping */
let { $from, $to } = state.selection;
let range = $from.blockRange($to);
wrapIn(state.config.schema.nodes.multiple_choice_container)(
state,
dispatch,
);
/* set New Selection */
dispatch(
main.state.tr.setSelection(
new TextSelection(main.state.tr.doc.resolve(range.$to.pos)),
),
);
/* create First Option */
const newAnswerId = uuidv4();
const answerOption = main.state.config.schema.nodes.multiple_choice.create(
{ id: newAnswerId },
Fragment.empty,
);
dispatch(main.state.tr.replaceSelectionWith(answerOption));
setTimeout(() => {
helpers.createEmptyParagraph(context, newAnswerId);
}, 50);
};
const ToolBarBtnComponent = useMemo( const ToolBarBtnComponent = useMemo(
() => ( () => (
<StyledButton <StyledButton
...@@ -45,7 +81,8 @@ const ToolBarBtn = ({ view = {}, item }) => { ...@@ -45,7 +81,8 @@ const ToolBarBtn = ({ view = {}, item }) => {
label={label} label={label}
onMouseDown={e => { onMouseDown={e => {
e.preventDefault(); e.preventDefault();
run(main, context); item.run(view);
createOption();
}} }}
title={title} title={title}
/> />
......
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