12 changed files with 438 additions and 15 deletions
@ -0,0 +1,38 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2022 The Thingsboard Authors |
||||
|
|
||||
|
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
you may not use this file except in compliance with the License. |
||||
|
You may obtain a copy of the License at |
||||
|
|
||||
|
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
|
||||
|
Unless required by applicable law or agreed to in writing, software |
||||
|
distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
See the License for the specific language governing permissions and |
||||
|
limitations under the License. |
||||
|
|
||||
|
--> |
||||
|
<section class="tb-widget-settings" [formGroup]="markdownWidgetSettingsForm" fxLayout="column"> |
||||
|
<mat-slide-toggle class="mat-slide" formControlName="useMarkdownTextFunction"> |
||||
|
{{ 'widgets.markdown.use-markdown-text-function' | translate }} |
||||
|
</mat-slide-toggle> |
||||
|
<section fxLayout="column" [fxShow]="markdownWidgetSettingsForm.get('useMarkdownTextFunction').value"> |
||||
|
<tb-js-func formControlName="markdownTextFunction" |
||||
|
[globalVariables]="functionScopeVariables" |
||||
|
[functionArgs]="['data']" |
||||
|
functionTitle="{{ 'widgets.markdown.markdown-text-function' | translate }}" |
||||
|
helpId="widget/lib/markdown/markdown_text_fn"> |
||||
|
</tb-js-func> |
||||
|
</section> |
||||
|
<section fxLayout="column" [fxShow]="!markdownWidgetSettingsForm.get('useMarkdownTextFunction').value"> |
||||
|
<tb-markdown-editor formControlName="markdownTextPattern" |
||||
|
label="{{ 'widgets.markdown.markdown-text-pattern' | translate }}"> |
||||
|
</tb-markdown-editor> |
||||
|
</section> |
||||
|
<tb-css formControlName="markdownCss" |
||||
|
label="{{ 'widgets.markdown.markdown-css' | translate }}"> |
||||
|
</tb-css> |
||||
|
</section> |
||||
@ -0,0 +1,76 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2022 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { Component } from '@angular/core'; |
||||
|
import { WidgetSettings, WidgetSettingsComponent } from '@shared/models/widget.models'; |
||||
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
||||
|
import { Store } from '@ngrx/store'; |
||||
|
import { AppState } from '@core/core.state'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-markdown-widget-settings', |
||||
|
templateUrl: './markdown-widget-settings.component.html', |
||||
|
styleUrls: ['./widget-settings.scss'] |
||||
|
}) |
||||
|
export class MarkdownWidgetSettingsComponent extends WidgetSettingsComponent { |
||||
|
|
||||
|
markdownWidgetSettingsForm: FormGroup; |
||||
|
|
||||
|
constructor(protected store: Store<AppState>, |
||||
|
private fb: FormBuilder) { |
||||
|
super(store); |
||||
|
} |
||||
|
|
||||
|
protected settingsForm(): FormGroup { |
||||
|
return this.markdownWidgetSettingsForm; |
||||
|
} |
||||
|
|
||||
|
protected defaultSettings(): WidgetSettings { |
||||
|
return { |
||||
|
useMarkdownTextFunction: false, |
||||
|
markdownTextPattern: '# Markdown/HTML card \\n - **Current entity**: **${entityName}**. \\n - **Current value**: **${Random}**.', |
||||
|
markdownTextFunction: 'return \'# Some title\\\\n - Entity name: \' + data[0][\'entityName\'];', |
||||
|
markdownCss: '' |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
protected onSettingsSet(settings: WidgetSettings) { |
||||
|
this.markdownWidgetSettingsForm = this.fb.group({ |
||||
|
useMarkdownTextFunction: [settings.useMarkdownTextFunction, []], |
||||
|
markdownTextPattern: [settings.markdownTextPattern, []], |
||||
|
markdownTextFunction: [settings.qrCodeTextFunction, []], |
||||
|
markdownCss: [settings.markdownCss, []] |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
protected validatorTriggers(): string[] { |
||||
|
return ['useMarkdownTextFunction']; |
||||
|
} |
||||
|
|
||||
|
protected updateValidators(emitEvent: boolean) { |
||||
|
const useMarkdownTextFunction: boolean = this.markdownWidgetSettingsForm.get('useMarkdownTextFunction').value; |
||||
|
if (useMarkdownTextFunction) { |
||||
|
this.markdownWidgetSettingsForm.get('markdownTextPattern').disable(); |
||||
|
this.markdownWidgetSettingsForm.get('markdownTextFunction').enable(); |
||||
|
} else { |
||||
|
this.markdownWidgetSettingsForm.get('markdownTextPattern').enable(); |
||||
|
this.markdownWidgetSettingsForm.get('markdownTextFunction').disable(); |
||||
|
} |
||||
|
this.markdownWidgetSettingsForm.get('markdownTextPattern').updateValueAndValidity({emitEvent}); |
||||
|
this.markdownWidgetSettingsForm.get('markdownTextFunction').updateValueAndValidity({emitEvent}); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2022 The Thingsboard Authors |
||||
|
|
||||
|
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
you may not use this file except in compliance with the License. |
||||
|
You may obtain a copy of the License at |
||||
|
|
||||
|
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
|
||||
|
Unless required by applicable law or agreed to in writing, software |
||||
|
distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
See the License for the specific language governing permissions and |
||||
|
limitations under the License. |
||||
|
|
||||
|
--> |
||||
|
<div class="markdown-content" [ngClass]="{'tb-edit-mode': !readonly}" |
||||
|
tb-fullscreen [fullscreen]="fullscreen" (fullscreenChanged)="onFullscreen()"> |
||||
|
<div *ngIf="label" fxLayout="row" fxLayoutAlign="start center" style="height: 40px;"> |
||||
|
<label class="tb-title no-padding" [ngClass]="{'tb-required': required}">{{ label }}</label> |
||||
|
</div> |
||||
|
<div [fxShow]="!readonly && !disabled" class="markdown-content-editor"> |
||||
|
<div class="buttons-panel"> |
||||
|
<button [fxShow]="!editorMode" |
||||
|
class="edit-toggle" |
||||
|
type="button" |
||||
|
mat-button (click)="toggleEditMode()">{{ 'markdown.edit' | translate }}</button> |
||||
|
<button [fxShow]="editorMode" |
||||
|
class="edit-toggle" |
||||
|
type="button" |
||||
|
mat-button (click)="toggleEditMode()">{{ 'markdown.preview' | translate }}</button> |
||||
|
<button mat-button mat-icon-button (click)="fullscreen = !fullscreen" |
||||
|
matTooltip="{{(fullscreen ? 'fullscreen.exit' : 'fullscreen.expand') | translate}}" |
||||
|
matTooltipPosition="above"> |
||||
|
<mat-icon class="material-icons">{{ fullscreen ? 'fullscreen_exit' : 'fullscreen' }}</mat-icon> |
||||
|
</button> |
||||
|
</div> |
||||
|
<div [fxShow]="editorMode" #markdownEditor class="tb-markdown-editor"></div> |
||||
|
<div [fxShow]="!editorMode" class="tb-markdown-view-container"> |
||||
|
<tb-markdown [data]="renderValue" lineNumbers fallbackToPlainMarkdown></tb-markdown> |
||||
|
</div> |
||||
|
</div> |
||||
|
<div *ngIf="readonly || disabled" class="tb-markdown-view-container"> |
||||
|
<tb-markdown [data]="renderValue" lineNumbers fallbackToPlainMarkdown></tb-markdown> |
||||
|
</div> |
||||
|
</div> |
||||
@ -0,0 +1,77 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2022 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
.markdown-content { |
||||
|
min-width: 400px; |
||||
|
&.tb-edit-mode { |
||||
|
.tb-markdown-view-container { |
||||
|
border: 1px solid #c0c0c0; |
||||
|
} |
||||
|
.tb-markdown-view { |
||||
|
padding: 20px; |
||||
|
} |
||||
|
&:not(.tb-fullscreen) { |
||||
|
padding-bottom: 15px; |
||||
|
.markdown-content-editor { |
||||
|
min-height: 200px; |
||||
|
max-height: 200px; |
||||
|
height: 200px; |
||||
|
} |
||||
|
.tb-markdown-view-container { |
||||
|
min-height: 200px; |
||||
|
max-height: 200px; |
||||
|
height: 200px; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
&.tb-fullscreen { |
||||
|
background: #fff; |
||||
|
.markdown-content-editor { |
||||
|
height: calc(100% - 40px); |
||||
|
} |
||||
|
} |
||||
|
.markdown-content-editor { |
||||
|
position: relative; |
||||
|
height: 100%; |
||||
|
} |
||||
|
.tb-markdown-editor { |
||||
|
position: absolute; |
||||
|
top: 0; |
||||
|
left: 0; |
||||
|
right: 0; |
||||
|
bottom: 0; |
||||
|
border: 1px solid #c0c0c0; |
||||
|
} |
||||
|
.tb-markdown-view-container { |
||||
|
overflow: auto; |
||||
|
height: 100%; |
||||
|
} |
||||
|
.buttons-panel { |
||||
|
position: absolute; |
||||
|
top: 5px; |
||||
|
right: 24px; |
||||
|
z-index: 1; |
||||
|
button.edit-toggle { |
||||
|
min-width: 32px; |
||||
|
min-height: 15px; |
||||
|
padding: 4px; |
||||
|
margin: 0; |
||||
|
font-size: .8rem; |
||||
|
line-height: 15px; |
||||
|
color: #7b7b7b; |
||||
|
background: rgba(220, 220, 220, .35); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,154 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2022 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core'; |
||||
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; |
||||
|
import { Ace } from 'ace-builds'; |
||||
|
import { coerceBooleanProperty } from '@angular/cdk/coercion'; |
||||
|
import { getAce } from '@shared/models/ace/ace.models'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-markdown-editor', |
||||
|
templateUrl: './markdown-editor.component.html', |
||||
|
styleUrls: ['./markdown-editor.component.scss'], |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => MarkdownEditorComponent), |
||||
|
multi: true |
||||
|
} |
||||
|
] |
||||
|
}) |
||||
|
export class MarkdownEditorComponent implements OnInit, ControlValueAccessor { |
||||
|
|
||||
|
@Input() label: string; |
||||
|
|
||||
|
@Input() disabled: boolean; |
||||
|
|
||||
|
@Input() readonly: boolean; |
||||
|
|
||||
|
@ViewChild('markdownEditor', {static: true}) |
||||
|
markdownEditorElmRef: ElementRef; |
||||
|
|
||||
|
private markdownEditor: Ace.Editor; |
||||
|
|
||||
|
editorMode = true; |
||||
|
|
||||
|
fullscreen = false; |
||||
|
|
||||
|
markdownValue: string; |
||||
|
renderValue: string; |
||||
|
|
||||
|
ignoreChange = false; |
||||
|
|
||||
|
private propagateChange = null; |
||||
|
|
||||
|
private requiredValue: boolean; |
||||
|
|
||||
|
get required(): boolean { |
||||
|
return this.requiredValue; |
||||
|
} |
||||
|
|
||||
|
@Input() |
||||
|
set required(value: boolean) { |
||||
|
this.requiredValue = coerceBooleanProperty(value); |
||||
|
} |
||||
|
|
||||
|
constructor() { |
||||
|
} |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
if (!this.readonly) { |
||||
|
const editorElement = this.markdownEditorElmRef.nativeElement; |
||||
|
let editorOptions: Partial<Ace.EditorOptions> = { |
||||
|
mode: 'ace/mode/markdown', |
||||
|
showGutter: true, |
||||
|
showPrintMargin: false, |
||||
|
readOnly: false |
||||
|
}; |
||||
|
|
||||
|
const advancedOptions = { |
||||
|
enableSnippets: true, |
||||
|
enableBasicAutocompletion: true, |
||||
|
enableLiveAutocompletion: true |
||||
|
}; |
||||
|
|
||||
|
editorOptions = {...editorOptions, ...advancedOptions}; |
||||
|
|
||||
|
getAce().subscribe( |
||||
|
(ace) => { |
||||
|
this.markdownEditor = ace.edit(editorElement, editorOptions); |
||||
|
this.markdownEditor.session.setUseWrapMode(false); |
||||
|
this.markdownEditor.setValue(this.markdownValue ? this.markdownValue : '', -1); |
||||
|
this.markdownEditor.on('change', () => { |
||||
|
if (!this.ignoreChange) { |
||||
|
this.updateView(); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any): void { |
||||
|
} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean): void { |
||||
|
this.disabled = isDisabled; |
||||
|
} |
||||
|
|
||||
|
writeValue(value: string): void { |
||||
|
this.editorMode = true; |
||||
|
this.markdownValue = value; |
||||
|
this.renderValue = this.markdownValue ? this.markdownValue : ' '; |
||||
|
if (this.markdownEditor) { |
||||
|
this.ignoreChange = true; |
||||
|
this.markdownEditor.setValue(this.markdownValue ? this.markdownValue : '', -1); |
||||
|
this.ignoreChange = false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
updateView() { |
||||
|
const editorValue = this.markdownEditor.getValue(); |
||||
|
if (this.markdownValue !== editorValue) { |
||||
|
this.markdownValue = editorValue; |
||||
|
this.renderValue = this.markdownValue ? this.markdownValue : ' '; |
||||
|
this.propagateChange(this.markdownValue); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
onFullscreen() { |
||||
|
if (this.markdownEditor) { |
||||
|
setTimeout(() => { |
||||
|
this.markdownEditor.resize(); |
||||
|
}, 0); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
toggleEditMode() { |
||||
|
this.editorMode = !this.editorMode; |
||||
|
if (this.editorMode && this.markdownEditor) { |
||||
|
setTimeout(() => { |
||||
|
this.markdownEditor.resize(); |
||||
|
}, 0); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue