-
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.
- Loading branch information
Showing
7 changed files
with
179 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Component, ElementRef, Renderer2, ChangeDetectorRef } from '@angular/core'; | ||
import { BaseLeafComponent } from 'slate-angular'; | ||
|
||
@Component({ | ||
selector: 'span[demoLeaf]', | ||
template: ` | ||
<span slateString [context]="context" [viewContext]="viewContext" | ||
><span></span | ||
></span> | ||
`, | ||
}) | ||
export class DemoLeafComponent extends BaseLeafComponent { | ||
constructor( | ||
public elementRef: ElementRef, | ||
public cdr: ChangeDetectorRef, | ||
private renderer: Renderer2 | ||
) { | ||
super(elementRef, cdr); | ||
} | ||
|
||
onContextChange() { | ||
super.onContextChange(); | ||
this.changeStyle(); | ||
} | ||
|
||
changeStyle() { | ||
const backgroundColor = this.leaf.highlight ? '#ffeeba' : null; | ||
this.renderer.setStyle(this.nativeElement, 'backgroundColor', backgroundColor); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
demo/app/search-highlighting/search-highlighting.component.html
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,22 @@ | ||
<div class="demo-rich-editor-wrapper single"> | ||
<div class="demo-global-toolbar"> | ||
<div class="search-highlighting-toolbar"> | ||
<span class="material-icons">search</span> | ||
<input | ||
type="search" | ||
placeholder="Search the text..." | ||
[(ngModel)]="keywords" | ||
(ngModelChange)="keywordsChange($event)" | ||
/> | ||
</div> | ||
</div> | ||
<slate-editable | ||
class="demo-slate-angular-editor demo-image-editor" | ||
[editor]="editor" | ||
[(ngModel)]="value" | ||
[renderText]="renderText" | ||
[decorate]="decorate" | ||
[renderLeaf]="renderLeaf" | ||
> | ||
</slate-editable> | ||
</div> |
16 changes: 16 additions & 0 deletions
16
demo/app/search-highlighting/search-highlighting.component.scss
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,16 @@ | ||
.search-highlighting-toolbar { | ||
position: relative; | ||
display: flex; | ||
align-items: center; | ||
top: 0.5em; | ||
left: 0.5em; | ||
color: #ccc; | ||
border: 2px solid #ccc; | ||
border-radius: 4px; | ||
|
||
input { | ||
border: none; | ||
outline: none; | ||
background-color: transparent; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
demo/app/search-highlighting/search-highlighting.component.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,88 @@ | ||
import { ChangeDetectorRef, Component, OnInit } from '@angular/core'; | ||
import { createEditor, NodeEntry, Text } from 'slate'; | ||
import { withAngular } from 'slate-angular'; | ||
import { DemoMarkTextComponent, MarkTypes } from '../components/text/text.component'; | ||
import { DemoLeafComponent } from './leaf.component'; | ||
|
||
@Component({ | ||
selector: 'demo-search-highlight', | ||
templateUrl: './search-highlighting.component.html', | ||
styleUrls: ['./search-highlighting.component.scss'], | ||
}) | ||
export class DemoSearchHighlightingComponent implements OnInit { | ||
keywords = ''; | ||
|
||
value = initialValue; | ||
|
||
editor = withAngular(createEditor()); | ||
|
||
decorate: (nodeEntry: NodeEntry) => Range[]; | ||
|
||
constructor(private cdr: ChangeDetectorRef) {} | ||
|
||
ngOnInit(): void { | ||
this.generateDcoreate(); | ||
} | ||
|
||
keywordsChange(event) { | ||
this.generateDcoreate(); | ||
this.cdr.markForCheck(); | ||
} | ||
|
||
generateDcoreate() { | ||
this.decorate = ([node, path]) => { | ||
const ranges = []; | ||
|
||
if (this.keywords && Text.isText(node)) { | ||
const { text } = node; | ||
const parts = text.split(this.keywords); | ||
let offset = 0; | ||
|
||
parts.forEach((part, i) => { | ||
if (i !== 0) { | ||
ranges.push({ | ||
anchor: { path, offset: offset - this.keywords.length }, | ||
focus: { path, offset }, | ||
highlight: true, | ||
}); | ||
} | ||
|
||
offset = offset + part.length + this.keywords.length; | ||
}); | ||
} | ||
|
||
return ranges; | ||
}; | ||
} | ||
|
||
renderText = (text: Text) => { | ||
if (text.bold || text.italic || text[MarkTypes.code] || text.underlined) { | ||
return DemoMarkTextComponent; | ||
} | ||
} | ||
|
||
renderLeaf = (text: Text) => { | ||
if (text.bold || text.highlight) { | ||
return DemoLeafComponent; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
const initialValue = [ | ||
{ | ||
children: [ | ||
{ | ||
text: | ||
'This is editable text that you can search. As you search, it looks for matching strings of text, and adds ', | ||
}, | ||
{ text: 'decorations', bold: true }, | ||
{ text: ' to them in realtime.' }, | ||
], | ||
}, | ||
{ | ||
children: [ | ||
{ text: 'Try it out for yourself by typing in the search box above!' }, | ||
], | ||
}, | ||
]; |