Skip to content

Commit 3fcde0e

Browse files
committed
Add RichText Link
1 parent 81b7164 commit 3fcde0e

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed

package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@tiptap/extension-bullet-list": "^2.7.1",
6565
"@tiptap/extension-ordered-list": "^2.7.1",
6666
"@tiptap/extension-code": "^2.7.1",
67+
"@tiptap/extension-link": "^2.7.1",
6768
"uuid": "^10.0.0",
6869
"zone.js": "~0.14.2"
6970
},

src/app/features/spaces/contents/shared/rich-text-editor/rich-text-editor.component.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@
7373
</svg>
7474
</button>
7575
<span class="vertical-divider">&nbsp; </span>
76+
<button
77+
matTooltip="Link"
78+
class="rounded h-7 w-7 flex justify-center items-center ml-1"
79+
[ngClass]="{ selected: editor.isActive('link') }"
80+
(click)="setLink()">
81+
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-link-45deg" viewBox="0 0 16 16">
82+
<path
83+
d="M4.715 6.542 3.343 7.914a3 3 0 1 0 4.243 4.243l1.828-1.829A3 3 0 0 0 8.586 5.5L8 6.086a1 1 0 0 0-.154.199 2 2 0 0 1 .861 3.337L6.88 11.45a2 2 0 1 1-2.83-2.83l.793-.792a4 4 0 0 1-.128-1.287z" />
84+
<path
85+
d="M6.586 4.672A3 3 0 0 0 7.414 9.5l.775-.776a2 2 0 0 1-.896-3.346L9.12 3.55a2 2 0 1 1 2.83 2.83l-.793.792c.112.42.155.855.128 1.287l1.372-1.372a3 3 0 1 0-4.243-4.243z" />
86+
</svg>
87+
</button>
88+
<span class="vertical-divider">&nbsp; </span>
7689
<button
7790
matTooltip="Bullet List"
7891
class="rounded h-7 w-7 flex justify-center items-center ml-1"

src/app/features/spaces/contents/shared/rich-text-editor/rich-text-editor.component.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import ListItem from '@tiptap/extension-list-item';
1717
import OrderedList from '@tiptap/extension-ordered-list';
1818
import BulletList from '@tiptap/extension-bullet-list';
1919
import Code from '@tiptap/extension-code';
20+
import Link from '@tiptap/extension-link';
2021

2122
@Component({
2223
selector: 'll-rich-text-editor',
@@ -33,7 +34,25 @@ export class RichTextEditorComponent implements OnDestroy {
3334
settingsStore = inject(LocalSettingsStore);
3435

3536
editor = new Editor({
36-
extensions: [Document, Text, Paragraph, Bold, Italic, Strike, Underline, Placeholder, History, ListItem, OrderedList, BulletList, Code],
37+
extensions: [
38+
Document,
39+
Text,
40+
Paragraph,
41+
Bold,
42+
Italic,
43+
Strike,
44+
Underline,
45+
Placeholder,
46+
History,
47+
ListItem,
48+
OrderedList,
49+
BulletList,
50+
Code,
51+
Link.configure({
52+
openOnClick: false,
53+
autolink: true,
54+
}),
55+
],
3756
editorProps: {
3857
attributes: {
3958
class: 'p-2 border-color border-t rounded-b-md outline-none',
@@ -44,6 +63,24 @@ export class RichTextEditorComponent implements OnDestroy {
4463

4564
constructor(readonly fe: FormErrorHandlerService) {}
4665

66+
setLink(): void {
67+
const previousUrl = this.editor.getAttributes('link')['href'];
68+
const url = window.prompt('URL', previousUrl);
69+
console.log('previousUrl', previousUrl);
70+
console.log('url', url);
71+
// cancelled
72+
if (url === null) {
73+
return;
74+
}
75+
// empty
76+
if (url === '') {
77+
this.editor.chain().focus().unsetLink().run();
78+
return;
79+
}
80+
// update link
81+
this.editor.chain().focus().setLink({ href: url, target: '_blank' }).run();
82+
}
83+
4784
ngOnDestroy(): void {
4885
this.editor.destroy();
4986
}

src/styles/_rich-text-editor.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ ll-rich-text-editor {
3232
font-size: 0.85rem;
3333
padding: 0.3em 0.3em;
3434
}
35+
/* Link styles */
36+
a {
37+
color: var(--sys-primary);
38+
cursor: pointer;
39+
40+
&:hover {
41+
text-decoration: underline;
42+
}
43+
}
3544
/* List styles */
3645
ul {
3746
list-style: disc;

0 commit comments

Comments
 (0)