-
Notifications
You must be signed in to change notification settings - Fork 30
/
images.component.ts
124 lines (112 loc) · 3.29 KB
/
images.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Component, OnInit } from '@angular/core';
import { withAngular } from 'slate-angular';
import { createEditor, Transforms, Editor, Element } from 'slate';
import { DemoElementImageComponent } from '../components/image/image-component';
import { ImageElement } from '../../../custom-types';
import { FormsModule } from '@angular/forms';
import { SlateEditable } from '../../../packages/src/components/editable/editable.component';
import { DemoButtonComponent } from '../components/button/button.component';
@Component({
selector: 'demo-images',
templateUrl: 'images.component.html',
standalone: true,
imports: [DemoButtonComponent, SlateEditable, FormsModule]
})
export class DemoImagesComponent implements OnInit {
value = initialValue;
editor = withImage(withAngular(createEditor()));
constructor() {}
renderElement() {
return (element: Element) => {
if (element.type === 'image') {
return DemoElementImageComponent;
}
return null;
};
}
ngOnInit() {}
isImgUrl(imgUrl: string) {
return new Promise((resolve, reject) => {
const imgObj = new Image();
imgObj.src = imgUrl;
imgObj.onload = () => {
resolve(true);
};
imgObj.onerror = () => {
reject(false);
};
}).catch(error => {});
}
createImageNode(imgUrl: string) {
const imageNode: ImageElement = {
type: 'image',
url: imgUrl,
children: [
{
text: ''
}
]
};
Transforms.insertNodes(this.editor, imageNode);
}
addImages() {
const imgUrl = window.prompt('Enter the URL of the image:');
if (imgUrl) {
this.isImgUrl(imgUrl).then(value => {
if (value) {
this.createImageNode(imgUrl);
} else {
window.alert('URL is not an image');
}
});
}
}
valueChange(event) {}
}
const initialValue = [
{
type: 'paragraph',
children: [
{
text: 'In addition to nodes that contain editable text, you can also create other types of nodes, like images or videos.'
}
],
key: 'HdSTK'
},
{
type: 'image',
url: 'https://source.unsplash.com/kFrdX5IeQzI',
children: [
{
text: ''
}
],
key: 'EwcCn',
voids: true
},
{
type: 'paragraph',
children: [
{
text: 'This example shows images in action. It features two ways to add images. You can either add an image via the toolbar icon above, or if you want in on a little secret, copy an image URL to your keyboard and paste it anywhere in the editor!'
}
],
key: 'ecJaY'
},
{
type: 'paragraph',
children: [
{
text: ''
}
],
key: 'zRTHT'
}
];
const withImage = (editor: Editor) => {
const { isVoid } = editor;
editor.isVoid = element => {
return element.type === 'image' || isVoid(element);
};
return editor;
};