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

add option

parent f14863be
No related branches found
No related tags found
1 merge request!306Answer nodeview
...@@ -16,15 +16,9 @@ const checkifEmpty = view => { ...@@ -16,15 +16,9 @@ const checkifEmpty = view => {
}; };
const createQuestion = (state, dispatch, tr) => { const createQuestion = (state, dispatch, tr) => {
console.log(state.selection);
const { empty, $from, $to } = state.selection;
let content = Fragment.empty;
if (!empty && $from.sameParent($to) && $from.parent.inlineContent)
content = $from.parent.content.cut($from.parentOffset, $to.parentOffset);
const answerOption = state.config.schema.nodes.multiple_choice.create( const answerOption = state.config.schema.nodes.multiple_choice.create(
{ id: uuidv4() }, { id: uuidv4() },
content, Fragment.empty,
); );
dispatch(tr.replaceSelectionWith(answerOption)); dispatch(tr.replaceSelectionWith(answerOption));
}; };
......
...@@ -11,6 +11,7 @@ import EditorComponent from './EditorComponent'; ...@@ -11,6 +11,7 @@ import EditorComponent from './EditorComponent';
import FeedbackComponent from './FeedbackComponent'; 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;
...@@ -115,12 +116,34 @@ export default ({ node, view, getPos }) => { ...@@ -115,12 +116,34 @@ export default ({ node, view, getPos }) => {
}); });
}; };
const addOption = () => { const addOption = nodeId => {
console.log(node); context.view.main.state.doc.descendants((editorNode, index) => {
if (editorNode.type.name === 'multiple_choice') {
if (editorNode.attrs.id === nodeId) {
context.view.main.dispatch(
context.view.main.state.tr.setSelection(
new TextSelection(
context.view.main.state.tr.doc.resolve(
editorNode.nodeSize + index,
),
),
),
);
const answerOption = context.view.main.state.config.schema.nodes.multiple_choice.create(
{ id: uuidv4() },
Fragment.empty,
);
context.view.main.dispatch(
context.view.main.state.tr.replaceSelectionWith(answerOption),
);
}
}
});
}; };
const questionNumber = 1; const questionNumber = 1;
const readOnly = isEditable; const readOnly = !isEditable;
const showAddIcon = true; const showAddIcon = true;
const showRemoveIcon = true; const showRemoveIcon = true;
...@@ -152,7 +175,10 @@ export default ({ node, view, getPos }) => { ...@@ -152,7 +175,10 @@ export default ({ node, view, getPos }) => {
{showAddIcon && !readOnly && ( {showAddIcon && !readOnly && (
<Button <Button
icon={ icon={
<PlusSquareOutlined onClick={addOption} title="Add Option" /> <PlusSquareOutlined
onClick={() => addOption(node.attrs.id)}
title="Add Option"
/>
} }
/> />
)} )}
......
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