Skip to content

Commit

Permalink
fix: correctly apply file link attributes (#1626)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimio authored Dec 17, 2024
1 parent f73c397 commit d68fa92
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export class FileIoService {
method: 'POST',
body,
}).then(async (response: Response) => response.json()),
).pipe(map((result) => ({name: result.name, link: result.link})));
).pipe(
map((result) => ({
name: result.name,
link: result.link,
attrs: {
class: 'file-link',
},
})),
);
}
}
4 changes: 3 additions & 1 deletion projects/editor/common/attached.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface TuiEditorAttachedFile<T = Record<string, any>> {
import type {TuiLinkAttributes} from './tui-link-attributes';

export interface TuiEditorAttachedFile<T = TuiLinkAttributes> {
attrs?: T;
link: string;
name: string;
Expand Down
1 change: 1 addition & 0 deletions projects/editor/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export * from './image';
export * from './image-loader';
export * from './parsed-gradient';
export * from './tiptap-editor';
export * from './tui-link-attributes';
export * from './youtube';
5 changes: 5 additions & 0 deletions projects/editor/common/tui-link-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface TuiLinkAttributes {
target?: string | null;
rel?: string | null;
class?: string | null;
}
13 changes: 11 additions & 2 deletions projects/editor/extensions/file-link/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {TuiEditorAttachedFile} from '@taiga-ui/editor/common';
import type {TuiEditorAttachedFile, TuiLinkAttributes} from '@taiga-ui/editor/common';
import {Extension} from '@tiptap/core';

declare module '@tiptap/core' {
Expand All @@ -9,6 +9,12 @@ declare module '@tiptap/core' {
}
}

function linkAttributesToString(attrs: TuiLinkAttributes): string {
return Object.entries(attrs)
.map(([key, value]) => `${key}="${value}"`)
.join(' ');
}

export const TuiFileLink = Extension.create({
name: 'fileLink',

Expand All @@ -20,6 +26,9 @@ export const TuiFileLink = Extension.create({
const {selection} = state;
const selectedSize = Math.abs(selection.to - selection.from);
const whitespace = '<span style="font-size: 15px"> </span>';
const attrs = fileLink.attrs
? linkAttributesToString(fileLink.attrs)
: '';

return (
selectedSize > 0
Expand All @@ -32,7 +41,7 @@ export const TuiFileLink = Extension.create({
.setTextSelection(selection.to)
.insertContent(whitespace)
: chain().insertContent(
`<a href="${fileLink.link}">${fileLink.name}</a>${whitespace}`,
`<a href="${fileLink.link}" ${attrs}>${fileLink.name}</a>${whitespace}`,
)
)
.setTextSelection(selection.to)
Expand Down

0 comments on commit d68fa92

Please sign in to comment.