Initial editor functionality

This commit is contained in:
garrettmills 2020-02-08 03:36:14 -06:00
parent 81d9706830
commit b0bc6ca20c
19 changed files with 366 additions and 0 deletions

View File

@ -14,6 +14,10 @@ const routes: Routes = [
{ {
path: 'list', path: 'list',
loadChildren: () => import('./list/list.module').then(m => m.ListPageModule) loadChildren: () => import('./list/list.module').then(m => m.ListPageModule)
},
{
path: 'editor',
loadChildren: () => import('./pages/editor/editor.module').then( m => m.EditorPageModule)
} }
]; ];

View File

@ -20,6 +20,11 @@ export class AppComponent {
title: 'List', title: 'List',
url: '/list', url: '/list',
icon: 'list' icon: 'list'
},
{
title: 'Editor',
url: '/editor',
icon: 'edit'
} }
]; ];

View File

@ -9,6 +9,7 @@ import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module'; import { AppRoutingModule } from './app-routing.module';
import {HttpClientModule} from '@angular/common/http'; import {HttpClientModule} from '@angular/common/http';
import {ComponentsModule} from './components/components.module';
@NgModule({ @NgModule({
declarations: [AppComponent], declarations: [AppComponent],
@ -18,6 +19,7 @@ import {HttpClientModule} from '@angular/common/http';
IonicModule.forRoot(), IonicModule.forRoot(),
AppRoutingModule, AppRoutingModule,
HttpClientModule, HttpClientModule,
ComponentsModule,
], ],
providers: [ providers: [
StatusBar, StatusBar,

View File

@ -0,0 +1,15 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {HostComponent} from './editor/host/host.component';
@NgModule({
declarations: [HostComponent],
imports: [
CommonModule
],
entryComponents: [HostComponent],
exports: [
HostComponent
]
})
export class ComponentsModule { }

View File

@ -0,0 +1,13 @@
<ng-container>
<div
*ngIf="record.type === 'paragraph' || record.type === 'header1' || record.type === 'header2' || record.type === 'header3' || record.type === 'header4' || record.type === 'block_code'"
class="host-host ion-padding"
contenteditable="true"
(keyup)="onKeyUp($event)"
(blur)="record.value=hostContainer.innerText"
#hostContainer
[ngClass]="{'paragraph': record.type === 'paragraph', 'header1': record.type === 'header1', 'header2': record.type === 'header2', 'header3': record.type === 'header3', 'header4': record.type === 'header4', 'block_code': record.type === 'block_code'}"
>
{{ record.value }}
</div>
</ng-container>

View File

@ -0,0 +1,26 @@
.host-host.header1 {
font-weight: bold;
font-size: 24pt;
}
.host-host.header2 {
font-weight: bold;
font-size: 21pt;
}
.host-host.header3 {
font-weight: bold;
font-size: 18pt;
}
.host-host.header4 {
font-weight: bold;
font-size: 16pt;
}
.host-host.block_code {
font-family: monospace;
font-size: 12pt;
background-color: #ddd;
margin-bottom: 15px;
}

View File

@ -0,0 +1,24 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { HostComponent } from './host.component';
describe('HostComponent', () => {
let component: HostComponent;
let fixture: ComponentFixture<HostComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ HostComponent ],
imports: [IonicModule.forRoot()]
}).compileComponents();
fixture = TestBed.createComponent(HostComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,48 @@
import {Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
import HostRecord from '../../../structures/HostRecord';
@Component({
selector: 'editor-host',
templateUrl: './host.component.html',
styleUrls: ['./host.component.scss'],
})
export class HostComponent implements OnInit {
@Input() record: HostRecord;
@Output() recordChange = new EventEmitter<HostRecord>();
@Output() newHostRequested = new EventEmitter<HostComponent>();
@Output() destroyHostRequested = new EventEmitter<HostComponent>();
@ViewChild('hostContainer', {static: false}) hostContainer: ElementRef;
constructor() { }
ngOnInit() {}
onKeyUp($event) {
const innerText = this.hostContainer.nativeElement.innerText.trim()
if ( $event.code === 'Enter'
&& ( this.record.type !== 'block_code'
|| (innerText.endsWith('```') && (innerText.match(/`/g) || []).length >= 6)
)
) {
this.newHostRequested.emit(this);
this.hostContainer.nativeElement.innerText = this.hostContainer.nativeElement.innerText.trim();
} else if ( $event.code === 'Backspace' && !this.hostContainer.nativeElement.innerText.trim() ) {
this.destroyHostRequested.emit(this);
}
if ( innerText.startsWith('# ') ) {
this.record.type = 'header1';
} else if ( innerText.startsWith('## ') ) {
this.record.type = 'header2';
} else if ( innerText.startsWith('### ') ) {
this.record.type = 'header3';
} else if ( innerText.startsWith('#### ') ) {
this.record.type = 'header4';
} else if ( innerText.startsWith('```') ) {
this.record.type = 'block_code';
} else {
this.record.type = 'paragraph';
}
}
}

View File

@ -0,0 +1,3 @@
<p>
paragraph works!
</p>

View File

@ -0,0 +1,24 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { ParagraphComponent } from './paragraph.component';
describe('ParagraphComponent', () => {
let component: ParagraphComponent;
let fixture: ComponentFixture<ParagraphComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ParagraphComponent ],
imports: [IonicModule.forRoot()]
}).compileComponents();
fixture = TestBed.createComponent(ParagraphComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,14 @@
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-paragraph',
templateUrl: './paragraph.component.html',
styleUrls: ['./paragraph.component.scss'],
})
export class ParagraphComponent implements OnInit {
constructor() { }
ngOnInit() {}
}

View File

@ -0,0 +1,17 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { EditorPage } from './editor.page';
const routes: Routes = [
{
path: '',
component: EditorPage
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class EditorPageRoutingModule {}

View File

@ -0,0 +1,22 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { EditorPageRoutingModule } from './editor-routing.module';
import { EditorPage } from './editor.page';
import {ComponentsModule} from '../../components/components.module';
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
EditorPageRoutingModule,
ComponentsModule
],
declarations: [EditorPage]
})
export class EditorPageModule {}

View File

@ -0,0 +1,26 @@
<ion-header>
<ion-toolbar>
<ion-title>Note Editor</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ng-container>
<div class="editor-buttons">
<ion-button class="ion-padding ion-margin">Save</ion-button>
</div>
<div class="editor-root ion-padding">
Hello, world!
<div class="host-container ion-padding">
<editor-host
#editorHosts
*ngFor="let record of hostRecords"
[(record)]="record"
(newHostRequested)="onNewHostRequested($event)"
(destroyHostRequested)="onDestroyHostRequested($event)"
></editor-host>
</div>
</div>
</ng-container>
</ion-content>

View File

View File

@ -0,0 +1,24 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { EditorPage } from './editor.page';
describe('EditorPage', () => {
let component: EditorPage;
let fixture: ComponentFixture<EditorPage>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ EditorPage ],
imports: [IonicModule.forRoot()]
}).compileComponents();
fixture = TestBed.createComponent(EditorPage);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,90 @@
import {Component, OnInit, ViewChildren} from '@angular/core';
import HostRecord from '../../structures/HostRecord';
@Component({
selector: 'app-editor',
templateUrl: './editor.page.html',
styleUrls: ['./editor.page.scss'],
})
export class EditorPage implements OnInit {
public hostRecords: Array<HostRecord> = [new HostRecord('I am a record.')];
@ViewChildren('editorHosts') editorHosts;
constructor() { }
ngOnInit() {
console.log('Editor component: ', this);
}
onNewHostRequested($event) {
const insertAfter = this.getIndexFromRecord($event.record);
const record = new HostRecord('');
const newHosts = []
this.hostRecords.forEach((rec, i) => {
newHosts.push(rec);
if ( i === insertAfter ) {
newHosts.push(record);
}
})
this.hostRecords = newHosts;
setTimeout(() => {
const host = this.editorHosts.toArray()[insertAfter + 1].hostContainer.nativeElement;
const s = window.getSelection();
const r = document.createRange();
r.setStart(host, 0);
r.setEnd(host, 0);
s.removeAllRanges();
s.addRange(r);
}, 0);
}
onDestroyHostRequested($event) {
let removedIndex = 0;
const newHostRecords = this.editorHosts.filter((host, i) => {
if ( $event.record === host.record ) {
removedIndex = i;
}
return host.record !== $event.record;
});
const removedHost = this.editorHosts[removedIndex];
const hostRecords = newHostRecords.map(host => host.record);
this.hostRecords = hostRecords;
setTimeout(() => {
let focusIndex;
if ( removedIndex === 0 && this.editorHosts.toArray().length ) {
focusIndex = 0;
} else if ( removedIndex !== 0 ) {
focusIndex = removedIndex - 1;
}
if ( focusIndex >= 0 ) {
const host = this.editorHosts.toArray()[focusIndex].hostContainer.nativeElement;
const s = window.getSelection();
const r = document.createRange();
r.setStart(host, 0);
r.setEnd(host, 0);
s.removeAllRanges();
s.addRange(r);
}
}, 0);
}
protected getIndexFromRecord(record) {
let index;
this.editorHosts.toArray().forEach((host, i) => {
if ( host.record === record ) {
index = i;
}
});
return index;
}
}

View File

@ -0,0 +1,9 @@
export default class HostRecord {
public value = '';
public type: 'paragraph'|'header1'|'header2'|'header3'|'header4'|'block_code' = 'paragraph';
constructor(value = '') {
this.value = value;
}
}