-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: image view as angular element
- Loading branch information
Showing
22 changed files
with
250 additions
and
365 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 was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,9 @@ | ||
<span class="NgxEditor__ImageWrapper" [ngClass]="{'NgxEditor__Resizer--Active': selected}" [style.width]="outerWidth"> | ||
<span class="NgxEditor__ResizeHandle" *ngIf="selected" (mousedown)="startResizing($event)"> | ||
<span class="NgxEditor__ResizeHandle--TL" ></span> | ||
<span class="NgxEditor__ResizeHandle--TR" ></span> | ||
<span class="NgxEditor__ResizeHandle--BL"></span> | ||
<span class="NgxEditor__ResizeHandle--BR"></span> | ||
</span> | ||
<img [src]="src" [alt]="alt" [title]="title" #imgEl /> | ||
</span> |
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,59 @@ | ||
$primary: #1a73e8; | ||
|
||
img { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
|
||
.NgxEditor__ImageWrapper { | ||
position: relative; | ||
display: inline-block; | ||
line-height: 0; | ||
padding: 2px; | ||
|
||
&.NgxEditor__Resizer--Active { | ||
padding: 1px; | ||
border: 1px solid $primary; | ||
} | ||
|
||
.NgxEditor__ResizeHandle { | ||
position: absolute; | ||
height: 100%; | ||
width: 100%; | ||
|
||
.NgxEditor__ResizeHandle--TL, | ||
.NgxEditor__ResizeHandle--BL, | ||
.NgxEditor__ResizeHandle--TR, | ||
.NgxEditor__ResizeHandle--BR { | ||
position: absolute; | ||
width: 7px; | ||
height: 7px; | ||
background-color: $primary; | ||
border: 1px solid white; | ||
} | ||
|
||
.NgxEditor__ResizeHandle--BR { | ||
bottom: -5px; | ||
right: -5px; | ||
cursor: se-resize; | ||
} | ||
|
||
.NgxEditor__ResizeHandle--TR { | ||
top: -5px; | ||
right: -5px; | ||
cursor: ne-resize; | ||
} | ||
|
||
.NgxEditor__ResizeHandle--TL { | ||
top: -5px; | ||
left: -5px; | ||
cursor: nw-resize; | ||
} | ||
|
||
.NgxEditor__ResizeHandle--BL { | ||
bottom: -5px; | ||
left: -5px; | ||
cursor: sw-resize; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/lib/components/image-view/image-view.component.spec.ts
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,25 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { ImageViewComponent } from './image-view.component'; | ||
|
||
describe('ImageComponent', () => { | ||
let component: ImageViewComponent; | ||
let fixture: ComponentFixture<ImageViewComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [ ImageViewComponent ] | ||
}) | ||
.compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(ImageViewComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
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,66 @@ | ||
import { | ||
Component, ElementRef, EventEmitter, | ||
Input, Output, ViewChild | ||
} from '@angular/core'; | ||
import { EditorView } from 'prosemirror-view'; | ||
|
||
@Component({ | ||
selector: 'ngx-image-view', | ||
templateUrl: './image-view.component.html', | ||
styleUrls: ['./image-view.component.scss'] | ||
}) | ||
|
||
export class ImageViewComponent { | ||
private resizing = false; | ||
@Input() src: string; | ||
@Input() alt = ''; | ||
@Input() title = ''; | ||
@Input() outerWidth = ''; | ||
@Input() selected = false; | ||
@Input() view: EditorView; | ||
|
||
@Output() imageResize = new EventEmitter(); | ||
|
||
@ViewChild('imgEl', { static: true }) imgEl: ElementRef; | ||
|
||
constructor() { } | ||
|
||
startResizing(e: MouseEvent): void { | ||
e.preventDefault(); | ||
this.resizeImage(e); | ||
} | ||
|
||
resizeImage(evt: MouseEvent): void { | ||
const startX = evt.pageX; | ||
const startWidth = this.imgEl.nativeElement.clientWidth; | ||
|
||
const { width } = window.getComputedStyle(this.view.dom); | ||
const editorWidth = parseInt(width, 10); | ||
|
||
const onMouseMove = (e: MouseEvent) => { | ||
const currentX = e.pageX; | ||
const diffInPx = currentX - startX; | ||
const computedWidth = startWidth + diffInPx; | ||
|
||
// prevent image overflow the editor | ||
// prevent resizng below 20px | ||
if (computedWidth > editorWidth || computedWidth < 20) { | ||
return; | ||
} | ||
|
||
this.outerWidth = `${computedWidth}px`; | ||
}; | ||
|
||
const onMouseUp = (e: MouseEvent) => { | ||
e.preventDefault(); | ||
|
||
document.removeEventListener('mousemove', onMouseMove); | ||
document.removeEventListener('mouseup', onMouseUp); | ||
|
||
this.imageResize.emit(); | ||
}; | ||
|
||
document.addEventListener('mousemove', onMouseMove); | ||
document.addEventListener('mouseup', onMouseUp); | ||
} | ||
} |
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,3 +1 @@ | ||
<div class="NgxEditor" #ngxEditor> | ||
<ngx-bubble [editor]="editor"></ngx-bubble> | ||
</div> | ||
<div class="NgxEditor" #ngxEditor></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
Oops, something went wrong.