Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
wax-prosemirror
Manage
Activity
Members
Labels
Plan
Issues
34
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wax
wax-prosemirror
Merge requests
!396
add inputs
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
add inputs
widgets
into
master
Overview
0
Commits
47
Pipelines
0
Changes
56
Merged
Christos
requested to merge
widgets
into
master
3 years ago
Overview
0
Commits
47
Pipelines
0
Changes
5
Expand
0
0
Merge request reports
Viewing commit
a7072549
Prev
Next
Show latest version
5 files
+
261
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
a7072549
new nodeView files
· a7072549
chris
authored
3 years ago
wax-prosemirror-services/src/MatchingService/components/EditorComponent.js
0 → 100644
+
180
−
0
Options
/* eslint-disable react/prop-types */
import
React
,
{
useContext
,
useRef
,
useEffect
}
from
'
react
'
;
import
styled
from
'
styled-components
'
;
import
{
EditorView
}
from
'
prosemirror-view
'
;
import
{
EditorState
,
TextSelection
}
from
'
prosemirror-state
'
;
import
{
StepMap
}
from
'
prosemirror-transform
'
;
import
{
keymap
}
from
'
prosemirror-keymap
'
;
import
{
baseKeymap
}
from
'
prosemirror-commands
'
;
import
{
undo
,
redo
}
from
'
prosemirror-history
'
;
import
{
WaxContext
}
from
'
wax-prosemirror-core
'
;
import
Placeholder
from
'
../../MultipleChoiceQuestionService/plugins/placeholder
'
;
const
EditorWrapper
=
styled
.
div
`
border: none;
display: flex;
flex: 2 1 auto;
justify-content: left;
.ProseMirror {
white-space: break-spaces;
width: 100%;
word-wrap: break-word;
&:focus {
outline: none;
}
:empty::before {
content: 'Type your answer';
color: #aaa;
float: left;
font-style: italic;
pointer-events: none;
}
p:first-child {
margin: 0;
}
p.empty-node:first-child::before {
content: attr(data-content);
}
.empty-node::before {
color: rgb(170, 170, 170);
float: left;
font-style: italic;
height: 0px;
pointer-events: none;
}
}
`
;
const
EditorComponent
=
({
node
,
view
,
getPos
})
=>
{
const
editorRef
=
useRef
();
const
context
=
useContext
(
WaxContext
);
const
{
app
,
pmViews
:
{
main
},
}
=
context
;
let
questionView
;
const
questionId
=
node
.
attrs
.
id
;
const
isEditable
=
main
.
props
.
editable
(
editable
=>
{
return
editable
;
});
let
finalPlugins
=
[];
const
createKeyBindings
=
()
=>
{
const
keys
=
getKeys
();
Object
.
keys
(
baseKeymap
).
forEach
(
key
=>
{
keys
[
key
]
=
baseKeymap
[
key
];
});
return
keys
;
};
const
getKeys
=
()
=>
{
return
{
'
Mod-z
'
:
()
=>
undo
(
view
.
state
,
view
.
dispatch
),
'
Mod-y
'
:
()
=>
redo
(
view
.
state
,
view
.
dispatch
),
};
};
const
plugins
=
[
keymap
(
createKeyBindings
()),
...
app
.
getPlugins
()];
// eslint-disable-next-line no-shadow
const
createPlaceholder
=
placeholder
=>
{
return
Placeholder
({
content
:
placeholder
,
});
};
finalPlugins
=
finalPlugins
.
concat
([
createPlaceholder
(
'
Type your answer
'
),
...
plugins
,
]);
useEffect
(()
=>
{
questionView
=
new
EditorView
(
{
mount
:
editorRef
.
current
,
},
{
editable
:
()
=>
isEditable
,
state
:
EditorState
.
create
({
doc
:
node
,
plugins
:
finalPlugins
,
}),
dispatchTransaction
,
disallowedTools
:
[
'
Images
'
,
'
Lists
'
,
'
lift
'
,
'
MultipleChoice
'
],
handleDOMEvents
:
{
mousedown
:
()
=>
{
main
.
dispatch
(
main
.
state
.
tr
.
setMeta
(
'
outsideView
'
,
questionId
)
.
setSelection
(
new
TextSelection
(
main
.
state
.
tr
.
doc
.
resolve
(
getPos
()
+
2
+
context
.
pmViews
[
questionId
].
state
.
selection
.
to
,
),
),
),
);
context
.
updateView
({},
questionId
);
if
(
questionView
.
hasFocus
())
questionView
.
focus
();
},
blur
:
(
editorView
,
event
)
=>
{
if
(
questionView
&&
event
.
relatedTarget
===
null
)
{
questionView
.
focus
();
}
},
},
attributes
:
{
spellcheck
:
'
false
'
,
},
},
);
// Set Each note into Wax's Context
context
.
updateView
(
{
[
questionId
]:
questionView
,
},
questionId
,
);
if
(
questionView
.
hasFocus
())
questionView
.
focus
();
},
[]);
const
dispatchTransaction
=
tr
=>
{
const
{
state
,
transactions
}
=
questionView
.
state
.
applyTransaction
(
tr
);
questionView
.
updateState
(
state
);
context
.
updateView
({},
questionId
);
if
(
!
tr
.
getMeta
(
'
fromOutside
'
))
{
const
outerTr
=
view
.
state
.
tr
;
const
offsetMap
=
StepMap
.
offset
(
getPos
()
+
1
);
for
(
let
i
=
0
;
i
<
transactions
.
length
;
i
++
)
{
const
{
steps
}
=
transactions
[
i
];
for
(
let
j
=
0
;
j
<
steps
.
length
;
j
++
)
outerTr
.
step
(
steps
[
j
].
map
(
offsetMap
));
}
if
(
outerTr
.
docChanged
)
view
.
dispatch
(
outerTr
.
setMeta
(
'
outsideView
'
,
questionId
));
}
};
return
(
<
EditorWrapper
>
<
div
ref
=
{
editorRef
}
/
>
<
/EditorWrapper
>
);
};
export
default
EditorComponent
;