Skip to content

Commit

Permalink
Use file handler modal window instead of direct downloading for all u…
Browse files Browse the repository at this point in the history
…ploaded file links
  • Loading branch information
yurabakhtin committed Jan 30, 2024
1 parent 8fc807b commit d5a961e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/editor/core/humhub-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export function getUserLocale() {
return isHumhub() ? humhub.modules.user.config.locale.split("-")[0] : null;
}

export function filterFileUrl(url) {
export function filterFileUrl(url, mode) {
if (!window.humhub) {
return {url: url, guid: null};
}

return isHumhub() ? humhub.modules.file.filterFileUrl(url) : url;
return isHumhub() ? humhub.modules.file.filterFileUrl(url, mode) : url;
}

export function getLoaderWidget() {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/core/plugins/file_handler/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const schema = {
parseMarkdown: {
mark: "link",
getAttrs: function (tok) {
let {url, guid} = filterFileUrl(tok.attrGet("href"));
let {url, guid} = filterFileUrl(tok.attrGet("href"), 'view');

if (!validateHref(url)) {
url = '#';
Expand Down
3 changes: 2 additions & 1 deletion src/editor/core/plugins/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const link = {
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
const hrefIndex = tokens[idx].attrIndex('href');

let {url, guid} = filterFileUrl(tokens[idx].attrs[hrefIndex][1]);
let {url, guid} = filterFileUrl(tokens[idx].attrs[hrefIndex][1], 'view');

tokens[idx].attrs[hrefIndex][1] = url;

if (guid) {
tokens[idx].attrPush(['data-file-guid', guid]); // add new attribute
tokens[idx].attrPush(['data-file-download', '']); // add new attribute
tokens[idx].attrPush(['data-file-url', url]); // add new attribute
tokens[idx].attrPush(['data-target', '#globalModal']);
}

// If you are sure other plugins can't add `target` - drop check below
Expand Down
2 changes: 1 addition & 1 deletion src/editor/core/plugins/link/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const schema = {
toDOM(node) { let {href, title} = node.attrs; return ["a", {href, title}, 0] },
parseMarkdown: {
mark: "link", getAttrs: function (tok) {
let {url, guid} = filterFileUrl(tok.attrGet("href"));
let {url, guid} = filterFileUrl(tok.attrGet("href"), 'view');

if (!validateHref(url)) {
url = '#';
Expand Down
11 changes: 6 additions & 5 deletions test/humhub-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19093,12 +19093,12 @@ function getUserLocale() {
return isHumhub() ? humhub.modules.user.config.locale.split("-")[0] : null;
}

function filterFileUrl(url) {
function filterFileUrl(url, mode) {
if (!window.humhub) {
return {url: url, guid: null};
}

return isHumhub() ? humhub.modules.file.filterFileUrl(url) : url;
return isHumhub() ? humhub.modules.file.filterFileUrl(url, mode) : url;
}

function getLoaderWidget() {
Expand Down Expand Up @@ -125222,7 +125222,7 @@ var schema$a = {
var title = ref.title; return ["a", {href: href, title: title}, 0] },
parseMarkdown: {
mark: "link", getAttrs: function (tok) {
var ref = filterFileUrl(tok.attrGet("href"));
var ref = filterFileUrl(tok.attrGet("href"), 'view');
var url = ref.url;
var guid = ref.guid;

Expand Down Expand Up @@ -125463,7 +125463,7 @@ var link = {
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
var hrefIndex = tokens[idx].attrIndex('href');

var ref = filterFileUrl(tokens[idx].attrs[hrefIndex][1]);
var ref = filterFileUrl(tokens[idx].attrs[hrefIndex][1], 'view');
var url = ref.url;
var guid = ref.guid;

Expand All @@ -125473,6 +125473,7 @@ var link = {
tokens[idx].attrPush(['data-file-guid', guid]); // add new attribute
tokens[idx].attrPush(['data-file-download', '']); // add new attribute
tokens[idx].attrPush(['data-file-url', url]); // add new attribute
tokens[idx].attrPush(['data-target', '#globalModal']);
}

// If you are sure other plugins can't add `target` - drop check below
Expand Down Expand Up @@ -128210,7 +128211,7 @@ var schema = {
parseMarkdown: {
mark: "link",
getAttrs: function (tok) {
var ref = filterFileUrl(tok.attrGet("href"));
var ref = filterFileUrl(tok.attrGet("href"), 'view');
var url = ref.url;
var guid = ref.guid;

Expand Down

0 comments on commit d5a961e

Please sign in to comment.