diff --git a/editors/demo/src/HHMI/config/config.js b/editors/demo/src/HHMI/config/config.js index 3257bebfda40a35db65aba489996d0df6fb12bd1..e5f77e92e204fb6234a2aa9f187d897a7364f482 100644 --- a/editors/demo/src/HHMI/config/config.js +++ b/editors/demo/src/HHMI/config/config.js @@ -29,11 +29,16 @@ import { EssayToolGroupService, MatchingService, MatchingToolGroupService, + ChatService, } from 'wax-prosemirror-services'; import { DefaultSchema } from 'wax-prosemirror-utilities'; import invisibles, { hardBreak } from '@guardian/prosemirror-invisibles'; +const getContent = source => { + console.log('editor content', source); +}; + export default { MenuService: [ { @@ -63,12 +68,14 @@ export default { }, ], + // ChatService: { getContent }, SchemaService: DefaultSchema, RulesService: [emDash, ellipsis], PmPlugins: [columnResizing(), tableEditing(), invisibles([hardBreak()])], services: [ + // new ChatService(), new MatchingService(), new MatchingToolGroupService(), new FillTheGapQuestionService(), diff --git a/wax-prosemirror-core/src/WaxView.js b/wax-prosemirror-core/src/WaxView.js index 26b332b0d7c175a8e83ef50eabf3e6a11df4f885..5457ae9ad907921e594c60767c1a5218eb46f5b0 100644 --- a/wax-prosemirror-core/src/WaxView.js +++ b/wax-prosemirror-core/src/WaxView.js @@ -82,6 +82,7 @@ const WaxView = forwardRef((props, ref) => { state: EditorState.create(options), dispatchTransaction, disallowedTools: [], + options, user, scrollMargin: 200, scrollThreshold: 200, diff --git a/wax-prosemirror-plugins/index.js b/wax-prosemirror-plugins/index.js index 61ff650c0465308b0fabc0412a5272c4a3a9cc3c..c9a0296f1365ff793c2970cb5260b31a3e2a6165 100644 --- a/wax-prosemirror-plugins/index.js +++ b/wax-prosemirror-plugins/index.js @@ -11,3 +11,4 @@ export { default as mathSelectPlugin } from './src/math/math-select'; export { default as FindAndReplacePlugin } from './src/findAndReplace/FindAndReplacePlugin'; export { default as PlaceHolderPlugin } from './src/images/placeHolderPlugin'; export { default as captionPlugin } from './src/images/captionPlugin'; +export { default as ChatPlugin } from './src/ChatPlugin'; diff --git a/wax-prosemirror-plugins/src/ChatPlugin.js b/wax-prosemirror-plugins/src/ChatPlugin.js new file mode 100644 index 0000000000000000000000000000000000000000..369932323b75a4b84a1d0c77890ffb90cb7e9499 --- /dev/null +++ b/wax-prosemirror-plugins/src/ChatPlugin.js @@ -0,0 +1,61 @@ +import { EditorState, Plugin, PluginKey } from 'prosemirror-state'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { DOMSerializer, DOMParser } from 'prosemirror-model'; + +const chatPlugin = new PluginKey('chatPlugin'); + +const serializer = schema => { + const WaxSerializer = DOMSerializer.fromSchema(schema); + return content => { + const container = document.createElement('article'); + container.appendChild(WaxSerializer.serializeFragment(content)); + return container.innerHTML; + }; +}; + +const parser = schema => { + const WaxParser = DOMParser.fromSchema(schema); + + return content => { + const container = document.createElement('article'); + + container.innerHTML = content; + return WaxParser.parse(container); + }; +}; + +export default props => { + return new Plugin({ + key: chatPlugin, + state: { + init: (_, state) => {}, + apply(tr, prev, _, newState) {}, + }, + props: { + handleKeyDown(view, event) { + if (event.key === 'Enter' && !event.shiftKey) { + if (view.state.doc.content.size <= 2) { + return true; + } + const WaxOptions = { + doc: {}, + schema: view.props.options.schema, + plugins: view.props.options.plugins, + }; + const parse = parser(view.props.options.schema); + WaxOptions.doc = parse(''); + + const serialize = serializer(view.props.options.schema); + props.getContent(serialize(view.state.doc.content)); + + view.updateState(EditorState.create(WaxOptions)); + if (view.dispatch) { + view.state.tr.setMeta('addToHistory', false); + } + return true; + } + return false; + }, + }, + }); +}; diff --git a/wax-prosemirror-services/index.js b/wax-prosemirror-services/index.js index c368f865b42e13397cfbcd6a3d71b819411d7faa..fb9d17a612b5786d42dd0f23bef769fbc76bef80 100644 --- a/wax-prosemirror-services/index.js +++ b/wax-prosemirror-services/index.js @@ -50,7 +50,7 @@ export { default as TrueFalseQuestionService } from './src/MultipleChoiceQuestio export { default as FillTheGapQuestionService } from './src/FillTheGapQuestionService/FillTheGapQuestionService'; export { default as EssayService } from './src/EssayService/EssayService'; export { default as MatchingService } from './src/MatchingService/MatchingService'; - +export { default as ChatService } from './src/ChatService/ChatService'; /* ToolGroups */ diff --git a/wax-prosemirror-services/src/ChatService/ChatService.js b/wax-prosemirror-services/src/ChatService/ChatService.js new file mode 100644 index 0000000000000000000000000000000000000000..328194cb83bfe2eb22533067957a555209626e79 --- /dev/null +++ b/wax-prosemirror-services/src/ChatService/ChatService.js @@ -0,0 +1,12 @@ +import { ChatPlugin } from 'wax-prosemirror-plugins'; +import Service from '../Service'; + +class ChatService extends Service { + name = 'ChatService'; + + boot() { + this.app.PmPlugins.add('chatPlugin', ChatPlugin(this.config)); + } +} + +export default ChatService;