Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 28f9d5e

Browse files
feat: highlight code lines
1 parent cf45925 commit 28f9d5e

File tree

4 files changed

+86
-7
lines changed

4 files changed

+86
-7
lines changed

studio/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

studio/src/app/popovers/editor/app-code/app-code.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
app-code {
2+
3+
ion-list.list-ios {
4+
margin-bottom: 0;
5+
padding-top: 8px;
6+
padding-bottom: 8px;
7+
}
8+
29
ion-item {
310
--border-width: 0;
411
--inner-border-width: 0;

studio/src/app/popovers/editor/app-code/app-code.tsx

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export class AppCode {
3939
@State()
4040
private codeColor: string;
4141

42+
@State()
43+
private highlightLines: string;
44+
45+
@State()
46+
private highlightColor: string;
47+
4248
constructor() {
4349
this.prismService = PrismService.getInstance();
4450
}
@@ -47,20 +53,31 @@ export class AppCode {
4753
this.languages = await this.prismService.getLanguages();
4854

4955
await this.initCurrentLanguage();
56+
await this.initCurrentHiglight();
5057
}
5158

5259
private initCurrentLanguage(): Promise<void> {
5360
return new Promise<void>(async (resolve) => {
5461
this.currentLanguage = this.selectedElement && this.selectedElement.getAttribute('language') ? this.selectedElement.getAttribute('language') : 'javascript';
5562
this.codeColor = await this.initColor();
5663

57-
console.log(this.codeColor);
64+
resolve();
65+
});
66+
}
67+
68+
private initCurrentHiglight(): Promise<void> {
69+
return new Promise<void>(async (resolve) => {
70+
this.highlightLines = this.selectedElement && this.selectedElement.getAttribute('highlight-lines') ? this.selectedElement.getAttribute('highlight-lines') : null;
71+
72+
if (this.selectedElement && this.selectedElement.style.getPropertyValue('--deckgo-highlight-code-line-background')) {
73+
this.highlightColor = this.selectedElement.style.getPropertyValue('--deckgo-highlight-code-line-background');
74+
}
5875

5976
resolve();
6077
});
6178
}
6279

63-
private selectColor($event): Promise<void> {
80+
private selectColor($event, colorFunction: Function): Promise<void> {
6481
return new Promise<void>(async (resolve) => {
6582
if (!this.selectedElement || !this.selectedElement.parentElement) {
6683
resolve();
@@ -74,15 +91,24 @@ export class AppCode {
7491

7592
await this.privateHideShowPopover();
7693

77-
this.codeColor = $event.target.value;
78-
this.selectedElement.style.setProperty(this.getStyle(), $event.target.value);
94+
colorFunction($event);
7995

8096
this.emitCodeDidChange();
8197

8298
resolve();
8399
});
84100
}
85101

102+
private setCodeColor = ($event) => {
103+
this.codeColor = $event.target.value;
104+
this.selectedElement.style.setProperty(this.getStyle(), $event.target.value);
105+
};
106+
107+
private setHighlightColor = ($event) => {
108+
this.highlightColor = $event.target.value;
109+
this.selectedElement.style.setProperty('--deckgo-highlight-code-line-background', $event.target.value);
110+
};
111+
86112
private privateHideShowPopover(): Promise<void> {
87113
return new Promise<void>((resolve) => {
88114
const popover: HTMLIonPopoverElement = this.el.closest('ion-popover');
@@ -196,6 +222,37 @@ export class AppCode {
196222
});
197223
}
198224

225+
private handleInput($event: CustomEvent<KeyboardEvent>) {
226+
this.highlightLines = ($event.target as InputTargetEvent).value;
227+
}
228+
229+
private highlightSelectedLines(): Promise<void> {
230+
return new Promise<void>(async (resolve) => {
231+
if (!this.selectedElement) {
232+
resolve();
233+
return;
234+
}
235+
236+
const currentHighlight: string = this.selectedElement.getAttribute('highlight-lines');
237+
238+
console.log(currentHighlight, this.highlightLines);
239+
240+
if (currentHighlight === this.highlightLines) {
241+
resolve();
242+
return;
243+
}
244+
245+
this.selectedElement.setAttribute('highlight-lines', this.highlightLines);
246+
247+
// Reload component with new lines to highlight
248+
await (this.selectedElement as any).load();
249+
250+
this.emitCodeDidChange();
251+
252+
resolve();
253+
});
254+
}
255+
199256
render() {
200257
return <ion-list>
201258
<ion-item-divider><ion-label>Language</ion-label></ion-item-divider>
@@ -226,7 +283,22 @@ export class AppCode {
226283

227284
<ion-item>
228285
<ion-label>Color</ion-label>
229-
<input type="color" value={this.codeColor} onChange={(e) => this.selectColor(e)}></input>
286+
<input type="color" value={this.codeColor} onChange={(e) => this.selectColor(e, this.setCodeColor)}></input>
287+
</ion-item>
288+
289+
<ion-item-divider class="ion-padding-top">
290+
<ion-label>Highlight lines</ion-label>
291+
</ion-item-divider>
292+
293+
<ion-item>
294+
<ion-input value={this.highlightLines} placeholder="Coma separated lines" debounce={500}
295+
onIonInput={(e: CustomEvent<KeyboardEvent>) => this.handleInput(e)}
296+
onIonChange={() => this.highlightSelectedLines()}></ion-input>
297+
</ion-item>
298+
299+
<ion-item>
300+
<ion-label>Color</ion-label>
301+
<input type="color" value={this.highlightColor} onChange={(e) => this.selectColor(e, this.setHighlightColor)}></input>
230302
</ion-item>
231303
</ion-list>
232304
}

webcomponents/highlight-code/src/components/highlight-code/deckdeckgo-highlight-code.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class DeckdeckgoHighlightCode {
2424

2525
@Prop({reflectToAttr: true}) language: string = 'javascript';
2626

27-
@Prop() highlightLines: string;
27+
@Prop({reflectToAttr: true}) highlightLines: string;
2828

2929
@Prop() editable: boolean = false;
3030

0 commit comments

Comments
 (0)