Skip to content

Commit

Permalink
File handler
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Jan 29, 2024
1 parent 2c17f9c commit 8fc807b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/editor/core/plugins/file_handler/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import {isHumhub} from "../../humhub-bridge"
import {canInsertLink, MenuItem} from "../../menu"

let isProsemirrorFileHandlerActive = false

const getFileHandlerItem = function(context, link, index) {
link.on('click', function () {
isProsemirrorFileHandlerActive = false
})

return new MenuItem({
label: link.html(),
title: link.text(),
Expand All @@ -18,6 +24,7 @@ const getFileHandlerItem = function(context, link, index) {
},
run() {
link.click()
isProsemirrorFileHandlerActive = true
}
})
}
Expand All @@ -28,7 +35,7 @@ const initFileHandler = function(context) {
}

humhub.event.on('humhub:file:created', (evt, file) => {
if (typeof context.editor.view !== 'undefined') {
if (isProsemirrorFileHandlerActive && typeof context.editor.view !== 'undefined') {
const view = context.editor.view
view.dispatch(view.state.tr.replaceSelectionWith(createFileHandlerNode(context, file), false));
}
Expand All @@ -38,12 +45,12 @@ const initFileHandler = function(context) {
const uploadWidget = context.editor.$.closest('form').find('[data-ui-widget="file.Upload"]').last()
if (uploadWidget.length) {
humhub.require('ui.widget').Widget.instance(uploadWidget).on('humhub:file:uploadEnd', (evt, response) => {
if (typeof context.editor.view !== 'undefined' &&
if (isProsemirrorFileHandlerActive &&
typeof context.editor.view !== 'undefined' &&
response._response.result.files instanceof Array &&
response._response.result.files.length) {
const view = context.editor.view
for (let i = 0; i < response._response.result.files.length; i++) {
console.log('FH humhub:file:uploadEnd', evt, response, response._response.result.files[i])
view.dispatch(view.state.tr.replaceSelectionWith(createFileHandlerNode(context, response._response.result.files[i]), false))
}
}
Expand Down
13 changes: 10 additions & 3 deletions test/humhub-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -128086,7 +128086,13 @@ PresetRegistry.prototype.add = function add (presetId, plugin, options) {
*
*/

var isProsemirrorFileHandlerActive = false;

var getFileHandlerItem = function(context, link, index) {
link.on('click', function () {
isProsemirrorFileHandlerActive = false;
});

return new MenuItem({
label: link.html(),
title: link.text(),
Expand All @@ -128096,6 +128102,7 @@ var getFileHandlerItem = function(context, link, index) {
},
run: function run() {
link.click();
isProsemirrorFileHandlerActive = true;
}
})
};
Expand All @@ -128106,7 +128113,7 @@ var initFileHandler = function(context) {
}

humhub.event.on('humhub:file:created', function (evt, file) {
if (typeof context.editor.view !== 'undefined') {
if (isProsemirrorFileHandlerActive && typeof context.editor.view !== 'undefined') {
var view = context.editor.view;
view.dispatch(view.state.tr.replaceSelectionWith(createFileHandlerNode(context, file), false));
}
Expand All @@ -128116,12 +128123,12 @@ var initFileHandler = function(context) {
var uploadWidget = context.editor.$.closest('form').find('[data-ui-widget="file.Upload"]').last();
if (uploadWidget.length) {
humhub.require('ui.widget').Widget.instance(uploadWidget).on('humhub:file:uploadEnd', function (evt, response) {
if (typeof context.editor.view !== 'undefined' &&
if (isProsemirrorFileHandlerActive &&
typeof context.editor.view !== 'undefined' &&
response._response.result.files instanceof Array &&
response._response.result.files.length) {
var view = context.editor.view;
for (var i = 0; i < response._response.result.files.length; i++) {
console.log('FH humhub:file:uploadEnd', evt, response, response._response.result.files[i]);
view.dispatch(view.state.tr.replaceSelectionWith(createFileHandlerNode(context, response._response.result.files[i]), false));
}
}
Expand Down

0 comments on commit 8fc807b

Please sign in to comment.