Convert linkify-matrix to TS
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
parent
f12b142311
commit
05b83d1fae
1 changed files with 27 additions and 20 deletions
|
@ -22,7 +22,14 @@ import {
|
||||||
tryTransformPermalinkToLocalHref,
|
tryTransformPermalinkToLocalHref,
|
||||||
} from "./utils/permalinks/Permalinks";
|
} from "./utils/permalinks/Permalinks";
|
||||||
|
|
||||||
function matrixLinkify(linkify) {
|
enum Type {
|
||||||
|
URL = "url",
|
||||||
|
UserId = "userid",
|
||||||
|
RoomAlias = "roomalias",
|
||||||
|
GroupId = "groupid"
|
||||||
|
}
|
||||||
|
|
||||||
|
function matrixLinkify(linkify): void {
|
||||||
// Text tokens
|
// Text tokens
|
||||||
const TT = linkify.scanner.TOKENS;
|
const TT = linkify.scanner.TOKENS;
|
||||||
// Multi tokens
|
// Multi tokens
|
||||||
|
@ -173,11 +180,11 @@ function matrixLinkify(linkify) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// stubs, overwritten in MatrixChat's componentDidMount
|
// stubs, overwritten in MatrixChat's componentDidMount
|
||||||
matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); };
|
matrixLinkify.onUserClick = function(e: MouseEvent, userId: string) { e.preventDefault(); };
|
||||||
matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); };
|
matrixLinkify.onAliasClick = function(e: MouseEvent, roomAlias: string) { e.preventDefault(); };
|
||||||
matrixLinkify.onGroupClick = function(e, groupId) { e.preventDefault(); };
|
matrixLinkify.onGroupClick = function(e: MouseEvent, groupId: string) { e.preventDefault(); };
|
||||||
|
|
||||||
const escapeRegExp = function(string) {
|
const escapeRegExp = function(string): string {
|
||||||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -196,15 +203,15 @@ matrixLinkify.MATRIXTO_MD_LINK_PATTERN =
|
||||||
matrixLinkify.MATRIXTO_BASE_URL= baseUrl;
|
matrixLinkify.MATRIXTO_BASE_URL= baseUrl;
|
||||||
|
|
||||||
matrixLinkify.options = {
|
matrixLinkify.options = {
|
||||||
events: function(href, type) {
|
events: function(href: string, type: Type | string): Partial<GlobalEventHandlers> {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "url": {
|
case Type.URL: {
|
||||||
// intercept local permalinks to users and show them like userids (in userinfo of current room)
|
// intercept local permalinks to users and show them like userids (in userinfo of current room)
|
||||||
try {
|
try {
|
||||||
const permalink = parsePermalink(href);
|
const permalink = parsePermalink(href);
|
||||||
if (permalink && permalink.userId) {
|
if (permalink && permalink.userId) {
|
||||||
return {
|
return {
|
||||||
click: function(e) {
|
onclick: function(e) {
|
||||||
matrixLinkify.onUserClick(e, permalink.userId);
|
matrixLinkify.onUserClick(e, permalink.userId);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -214,32 +221,32 @@ matrixLinkify.options = {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "userid":
|
case Type.UserId:
|
||||||
return {
|
return {
|
||||||
click: function(e) {
|
onclick: function(e) {
|
||||||
matrixLinkify.onUserClick(e, href);
|
matrixLinkify.onUserClick(e, href);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
case "roomalias":
|
case Type.RoomAlias:
|
||||||
return {
|
return {
|
||||||
click: function(e) {
|
onclick: function(e) {
|
||||||
matrixLinkify.onAliasClick(e, href);
|
matrixLinkify.onAliasClick(e, href);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
case "groupid":
|
case Type.GroupId:
|
||||||
return {
|
return {
|
||||||
click: function(e) {
|
onclick: function(e) {
|
||||||
matrixLinkify.onGroupClick(e, href);
|
matrixLinkify.onGroupClick(e, href);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
formatHref: function(href, type) {
|
formatHref: function(href: string, type: Type | string): string {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'roomalias':
|
case Type.RoomAlias:
|
||||||
case 'userid':
|
case Type.UserId:
|
||||||
case 'groupid':
|
case Type.GroupId:
|
||||||
default: {
|
default: {
|
||||||
return tryTransformEntityToPermalink(href);
|
return tryTransformEntityToPermalink(href);
|
||||||
}
|
}
|
||||||
|
@ -250,8 +257,8 @@ matrixLinkify.options = {
|
||||||
rel: 'noreferrer noopener',
|
rel: 'noreferrer noopener',
|
||||||
},
|
},
|
||||||
|
|
||||||
target: function(href, type) {
|
target: function(href: string, type: Type | string): string {
|
||||||
if (type === 'url') {
|
if (type === Type.URL) {
|
||||||
try {
|
try {
|
||||||
const transformed = tryTransformPermalinkToLocalHref(href);
|
const transformed = tryTransformPermalinkToLocalHref(href);
|
||||||
if (transformed !== href || decodeURIComponent(href).match(matrixLinkify.ELEMENT_URL_PATTERN)) {
|
if (transformed !== href || decodeURIComponent(href).match(matrixLinkify.ELEMENT_URL_PATTERN)) {
|
Loading…
Add table
Add a link
Reference in a new issue