-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(demo): add images demo #WIK-563
- Loading branch information
王焕
committed
May 19, 2021
1 parent
5fd8298
commit 9b6e1de
Showing
8 changed files
with
184 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, ElementRef, Renderer2, ChangeDetectorRef, HostListener, Output, EventEmitter } from '@angular/core'; | ||
import { BaseElementComponent } from 'slate-angular'; | ||
|
||
@Component({ | ||
selector: 'demo-element-image', | ||
templateUrl: 'image-component.html' | ||
selector: 'demo-element-image,[demoImage]', | ||
template: `<img [src]="element.url" alt="" contenteditable="false">`, | ||
host: { | ||
'[attr.contenteditable]': 'false', | ||
class: 'demo-element-image' | ||
} | ||
}) | ||
export class DemoElementImageComponent extends BaseElementComponent { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div class="demo-rich-editor-wrapper single"> | ||
<div class="demo-global-toolbar"> | ||
<demo-button (onMouseDown)="addImages()"> | ||
<span class="material-icons">image</span> | ||
</demo-button> | ||
</div> | ||
<slate-editable class="demo-slate-angular-editor demo-image-editor" [editor]="editor" [(ngModel)]="value" | ||
(ngModelChange)="valueChange($event)" [renderElement]="renderElement()"> | ||
</slate-editable> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { Component, OnInit, HostListener, ChangeDetectorRef } from '@angular/core'; | ||
import { withAngular, AngularEditor } from 'slate-angular'; | ||
import { createEditor, Transforms } from 'slate'; | ||
import { DemoElementImageComponent } from '../components/image/image-component'; | ||
|
||
@Component({ | ||
selector: 'demo-images', | ||
templateUrl: 'images.component.html' | ||
}) | ||
export class DemoImagesComponent implements OnInit { | ||
value = initialValue; | ||
|
||
editor = withAngular(createEditor()); | ||
|
||
@HostListener('mousedown', ['$event']) | ||
imageMousedown(event: MouseEvent) { | ||
event.stopPropagation() | ||
document.querySelectorAll('img').forEach(item => { | ||
item.classList.remove('outline') | ||
}) | ||
const target = event.target as HTMLElement; | ||
if( target.tagName === 'DEMO-ELEMENT-IMAGE'){ | ||
target.children[0].classList.add('outline') | ||
} | ||
if (target.tagName === 'IMG') { | ||
target.classList.add('outline'); | ||
} | ||
return | ||
} | ||
constructor(private cdr: ChangeDetectorRef) {} | ||
|
||
renderElement() { | ||
return (element: any) => { | ||
if (element.type === 'image') { | ||
return DemoElementImageComponent; | ||
} | ||
return null; | ||
}; | ||
} | ||
|
||
ngOnInit() {} | ||
|
||
isImgUrl(imgUrl: string) { | ||
return new Promise((resolve, reject) => { | ||
var ImgObj = new Image(); | ||
ImgObj.src = imgUrl; | ||
ImgObj.onload = () => { | ||
resolve(true); | ||
} | ||
ImgObj.onerror = () => { | ||
reject(false) | ||
} | ||
}).catch(error => { }); | ||
} | ||
|
||
createImageNode(imgUrl: string) { | ||
const imageNode = { | ||
type: 'image', | ||
url: imgUrl, | ||
children: [ | ||
{ | ||
'text': '' | ||
} | ||
], | ||
voids: true | ||
} | ||
Transforms.insertNodes(this.editor, imageNode); | ||
AngularEditor.deselect(this.editor); | ||
this.cdr.detectChanges(); | ||
const dom = AngularEditor.toDOMNode(this.editor, imageNode); | ||
console.log(dom,23423) | ||
dom.children[0].classList.add('outline'); | ||
} | ||
|
||
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, | ||
"name": "src=http___s13.sinaimg.cn_bmiddle_4d049168cc5e11e7fb13c&refer=http___s13.sinaimg.jpeg", | ||
"width": 400, | ||
"height": 300, | ||
"thumbUrl": "https://atlas-rc.pingcode.com/files/public/5ffa68d453ffebf847cf49b9/origin-url", | ||
"originUrl": "https://atlas-rc.pingcode.com/files/public/5ffa68d453ffebf847cf49b9/origin-url", | ||
"align": "center" | ||
}, | ||
{ | ||
"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" | ||
} | ||
]; | ||
|