Skip to content

Commit dde5466

Browse files
committed
Make Highlights works
1 parent 6eca398 commit dde5466

19 files changed

+371
-212
lines changed

angular.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,5 +721,8 @@
721721
"@nrwl/angular:library": {
722722
"unitTestRunner": "jest"
723723
}
724+
},
725+
"cli": {
726+
"analytics": false
724727
}
725728
}

apps/codelab/src/app/admin/content/presentation-editor/preview/slide-preview/dynamic-renderer/dynamic-renderer.component.ts

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { CodelabCodeDemoFilePathEditorComponent } from '../../../wrappers/custom
1111
import { CodelabCodeDemoConsoleEditorComponent } from '../../../wrappers/custom-component-editors/codelab-code-demo-console-editor/codelab-code-demo-console-editor.component';
1212
import { CodeDemoEditorEditorComponent } from '../../../wrappers/custom-component-editors/codelab-code-demo-editor-editor/code-demo-editor.component';
1313
import { CodelabImageEditorComponent } from '../../../wrappers/custom-component-editors/codelab-image-editor/codelab-image-editor.component';
14-
import { BlankComponent } from '../blank/blank.component';
1514
import { CodelabCodeDemoPreviewComponent } from '../../../wrappers/custom-component-previews/codelab-code-demo-preview/codelab-code-demo-preview.component';
1615

1716
import { NgxdResolver } from '@ngxd/core';
1817
import { CodelabExerciseEditorComponent } from '../../../wrappers/custom-component-editors/codelab-exercise-preview-editor/codelab-exercise-editor.component';
1918
import { TitleSlideComponent } from '../../../../../../components/slides/title-slide/title-slide.component';
19+
import { CodelabCodeDemoConsoleViewerComponent } from '../../../wrappers/custom-components-viewers/codelab-code-demo-console-viewer.component';
2020

2121
@Injectable({ providedIn: 'root' })
2222
export class PreviewDynamicComponentResolver extends NgxdResolver<string, any> {
@@ -52,7 +52,13 @@ export class PreviewDynamicComponentResolver extends NgxdResolver<string, any> {
5252
@Injectable({ providedIn: 'root' })
5353
export class ViewDynamicComponentResolver extends NgxdResolver<string, any> {
5454
constructor() {
55-
super([{ type: 'codelab-title-slide', component: TitleSlideComponent }]);
55+
super([
56+
{ type: 'codelab-title-slide', component: TitleSlideComponent },
57+
{
58+
type: 'codelab-code-demo-console-editor',
59+
component: CodelabCodeDemoConsoleViewerComponent,
60+
},
61+
]);
5662
}
5763
}
5864

@@ -127,51 +133,5 @@ export class DynamicRendererComponent implements OnChanges {
127133
break;
128134
}
129135
}
130-
131-
// async ngOnInit() {
132-
// const componentMap = {
133-
// preview: {
134-
// 'codelab-title-slide': TitleSlideComponent,
135-
// 'codelab-code-demo-console-editor': CodelabCodeDemoPreviewComponent,
136-
// 'codelab-exercise-preview': CodelabCodeDemoPreviewComponent,
137-
// 'codelab-exercise': CodelabCodeDemoPreviewComponent,
138-
// 'codelab-closing-slide': CodelabTitleSlideEditorComponent,
139-
// 'codelab-code-demo-file-path': CodelabCodeDemoPreviewComponent,
140-
// 'codelab-code-demo-editor': CodelabCodeDemoPreviewComponent,
141-
// 'codelab-image': CodelabImageEditorComponent
142-
// },
143-
// view: {
144-
// 'codelab-title-slide': BlankComponent
145-
// },
146-
// edit: {
147-
// 'codelab-title-slide': CodelabTitleSlideEditorComponent,
148-
// 'codelab-code-demo-console-editor': CodelabCodeDemoConsoleEditorComponent,
149-
// 'codelab-exercise-preview': CodelabExerciseComponent,
150-
// 'codelab-exercise': CodelabExerciseComponent,
151-
// 'codelab-closing-slide': CodelabTitleSlideEditorComponent,
152-
// 'codelab-code-demo-file-path': CodelabCodeDemoFilePathEditorComponent,
153-
// 'codelab-code-demo-editor': CodeDemoEditorEditorComponent,
154-
// 'codelab-image': CodelabImageEditorComponent
155-
// }
156-
// };
157-
158-
// const componentType = componentMap[this.mode][this.block.tag];
159-
// console.log(this.block);
160-
// console.log(componentType, this.block.props);
161-
// console.assert(componentType, this.block.tag);
162-
// const cf = this.componentFactoryResolver.resolveComponentFactory(
163-
// componentType
164-
// );
165-
// this.vcr.clear();
166-
// const componentRef = this.vcr.createComponent(cf);
167-
168-
// for (const prop in this.block.props) {
169-
// if (this.block.props.hasOwnProperty(prop)) {
170-
// componentRef.instance[prop] = this.block.props[prop];
171-
// }
172-
// }
173-
174-
// componentRef.changeDetectorRef.detectChanges();
175-
// }
176136
}
177137
}

apps/codelab/src/app/admin/content/presentation-editor/services/content.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class ContentService implements OnDestroy {
7070
// TODO: There should be a better way
7171
// this.goToSlide(router.routerState.snapshot.root.firstChild.firstChild.params.id || 0);
7272
this.state$
73-
.pipe(auditTime(5000), takeUntil(this.onDestroy$))
73+
.pipe(auditTime(2000), takeUntil(this.onDestroy$))
7474
.subscribe((presentations) => {
7575
this.presentations.doc('presentations').set({ presentations });
7676
});

apps/codelab/src/app/admin/content/presentation-editor/wrappers/custom-component-editors/codelab-code-demo-console-editor/codelab-code-demo-console-editor.component.html

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,6 @@
1010
<button mat-icon-button (click)="deleteFile(file.name)">
1111
<mat-icon>cancel</mat-icon>
1212
</button>
13-
<div class="highlights">
14-
<div class="highlights-header">
15-
<h3>Highlights</h3>
16-
<ng-container *ngIf="selection$ | async as selection">
17-
<button
18-
mat-button
19-
*ngIf="selection.file === file.name"
20-
(click)="addHighlight(file, selection)"
21-
>
22-
(add selected)
23-
</button>
24-
</ng-container>
25-
<ng-container *ngIf="!(selection$ | async)">
26-
<button mat-button (click)="addRegexHighlight(file)">
27-
(Add regex)
28-
</button>
29-
</ng-container>
30-
</div>
31-
32-
<div
33-
class="highlight"
34-
*ngFor="let highlight of file.highlights; let highlightIndex = index"
35-
>
36-
<ng-container
37-
*ngIf="
38-
highlight.selection?.selectionStartLineNumber !== undefined;
39-
else highlightInput
40-
"
41-
>
42-
{{ highlight.text | slice: 0:20 }}
43-
</ng-container>
44-
<ng-template #highlightInput>
45-
<mat-form-field>
46-
<mat-label>Regex</mat-label>
47-
48-
<input
49-
[(ngModel)]="highlight.selection"
50-
[validateInputMatchesText]="code[file.name]"
51-
matInput
52-
#model="ngModel"
53-
(ngModelChange)="model.valid && update()"
54-
/>
55-
56-
<mat-error *ngIf="model.errors?.invalidRegex"
57-
>Invalid regex</mat-error
58-
>
59-
<mat-error *ngIf="model.errors?.regexDoesNotMatch"
60-
>Regex doesn't match</mat-error
61-
>
62-
</mat-form-field>
63-
</ng-template>
64-
<button
65-
mat-icon-button
66-
(click)="deleteHighlight(file, highlightIndex)"
67-
>
68-
<mat-icon>cancel</mat-icon>
69-
</button>
70-
</div>
71-
</div>
7213
</div>
7314
<button
7415
mat-button
@@ -89,12 +30,15 @@ <h3>Highlights</h3>
8930
</mat-checkbox>
9031
</div>
9132
</div>
92-
33+
<slides-highlight-button
34+
[(ngModel)]="highlights2"
35+
(ngModelChange)="updateHighlights($event)"
36+
></slides-highlight-button>
9337
<code-demo
9438
[showPreview]="showPreview"
9539
[allowSwitchingFiles]="allowSwitchingFiles"
96-
[highlights]="highlights"
9740
[(ngModel)]="code"
41+
[highlights]="highlights2"
9842
[displayFileName]="!allowSwitchingFiles"
9943
(ngModelChange)="update()"
10044
[files]="openFiles"

apps/codelab/src/app/admin/content/presentation-editor/wrappers/custom-component-editors/codelab-code-demo-console-editor/codelab-code-demo-console-editor.component.ts

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { Component, Input, OnInit } from '@angular/core';
22
import { ContentService } from '../../../services/content.service';
33
import { ContentSlide, CustomBlock } from '../../../types';
44
import { MonacoConfigService } from '@codelab/code-demos';
5-
import { Observable } from 'rxjs';
6-
import { Selection } from 'monaco-editor';
5+
import { IRange, Selection } from 'monaco-editor';
6+
import { assert } from '@codelab/code-demos/src/lib/shared/utils';
7+
import { rangeToJSON } from './highlight-button/highlight-button.component';
78

89
interface SelectableFile {
10+
highlights2: IRange[];
911
selected: boolean;
1012
name: string;
11-
highlights: Highlight[];
13+
highlights: IRange[];
1214
}
1315

1416
interface Highlight {
@@ -18,6 +20,8 @@ interface Highlight {
1820
text: string;
1921
}
2022

23+
type Highlights2 = Record<string, IRange[]>;
24+
2125
@Component({
2226
selector: 'codelab-code-demo-console-editor',
2327
templateUrl: './codelab-code-demo-console-editor.component.html',
@@ -36,35 +40,8 @@ export class CodelabCodeDemoConsoleEditorComponent implements OnInit {
3640
@Input() displayFileName = false;
3741
readonly defaultNewFileName = 'new.ts';
3842

39-
readonly selection$ = new Observable<Highlight | undefined>((subscriber) => {
40-
// TODO: Unsubscribe
41-
MonacoConfigService.monacoReady.then((monaco: any) => {
42-
monaco.editor.onDidCreateEditor((editor) => {
43-
const subscription = editor.onDidChangeCursorPosition(() => {
44-
const selection = editor.getSelection();
45-
const text = editor.getModel().getValueInRange(selection);
46-
const match = editor
47-
.getModel()
48-
.uri.path.match(/prefix\/(.+?)\/(.*)$/);
49-
if (text === '') {
50-
subscriber.next();
51-
return;
52-
}
53-
if (!match) {
54-
return;
55-
}
56-
const [, prefix, file] = match;
57-
58-
subscriber.next({ file, prefix, selection, text });
59-
});
60-
61-
editor.onDidDispose(() => subscription.dispose());
62-
});
63-
});
64-
});
65-
6643
openFiles: string[] = [];
67-
highlights: Record<string, Selection>;
44+
highlights2: Highlights2 = {};
6845

6946
async ngOnInit() {
7047
this.inferVars();
@@ -75,7 +52,7 @@ export class CodelabCodeDemoConsoleEditorComponent implements OnInit {
7552
private readonly contentService: ContentService
7653
) {}
7754

78-
update(e?: any) {
55+
update() {
7956
this.inferVars();
8057

8158
this.contentService.updateBlock(this.presentationId, this.slide.id, {
@@ -98,21 +75,12 @@ export class CodelabCodeDemoConsoleEditorComponent implements OnInit {
9875
name,
9976
selected: i === 0,
10077
highlights: [],
78+
highlights2: [],
10179
}))
10280
: this.selectedFiles;
10381

104-
this.highlights = this.selectedFiles.reduce((result, file) => {
105-
result[file.name] = file.highlights.map((highlight) => {
106-
if (typeof highlight.selection === 'string') {
107-
try {
108-
return new RegExp(highlight.selection);
109-
} catch (e) {
110-
console.error('Invalid regexp', highlight.selection);
111-
}
112-
}
113-
114-
return highlight.selection;
115-
});
82+
this.highlights2 = this.selectedFiles.reduce((result, file) => {
83+
result[file.name] = file.highlights2 ?? [];
11684
return result;
11785
}, {});
11886

@@ -137,6 +105,7 @@ export class CodelabCodeDemoConsoleEditorComponent implements OnInit {
137105
name: this.defaultNewFileName,
138106
selected: false,
139107
highlights: [],
108+
highlights2: [],
140109
});
141110
this.code[this.defaultNewFileName] = '// code';
142111
}
@@ -149,23 +118,13 @@ export class CodelabCodeDemoConsoleEditorComponent implements OnInit {
149118
);
150119
}
151120

152-
addHighlight(file: SelectableFile, highlight: Highlight) {
153-
file.highlights.push(JSON.parse(JSON.stringify(highlight)));
154-
this.update();
155-
}
156-
157-
deleteHighlight(file: SelectableFile, highlightIndex: number) {
158-
file.highlights.splice(highlightIndex, 1);
159-
this.update();
160-
}
121+
updateHighlights(hightlighs: Highlights2) {
122+
const filenames = Object.keys(this.highlights2);
123+
for (const file of filenames) {
124+
assert(this.selectedFiles.find((a) => a.name === file)).highlights2 =
125+
hightlighs[file].map(rangeToJSON);
126+
}
161127

162-
addRegexHighlight(file: SelectableFile) {
163-
file.highlights.push({
164-
text: 'hi',
165-
prefix: 'hi',
166-
file: 'dsd',
167-
selection: '/dsdasdsad/',
168-
});
169128
this.update();
170129
}
171130
}

apps/codelab/src/app/admin/content/presentation-editor/wrappers/custom-component-editors/codelab-code-demo-console-editor/highlight-button/highlight-button.component.css

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<button
2+
mat-button
3+
color="primary"
4+
*ngIf="selection$ | async as sel; else dis"
5+
(click)="addHighlight(sel)"
6+
>
7+
Highlight
8+
</button>
9+
<ng-template #dis>
10+
<button mat-button color="primary" [disabled]="true">Highlight</button>
11+
</ng-template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { HighlightButtonComponent } from './highlight-button.component';
4+
5+
describe('HighlightButtonComponent', () => {
6+
let component: HighlightButtonComponent;
7+
let fixture: ComponentFixture<HighlightButtonComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [HighlightButtonComponent],
12+
}).compileComponents();
13+
14+
fixture = TestBed.createComponent(HighlightButtonComponent);
15+
component = fixture.componentInstance;
16+
fixture.detectChanges();
17+
});
18+
19+
it('should create', () => {
20+
expect(component).toBeTruthy();
21+
});
22+
});

0 commit comments

Comments
 (0)