From f49e477f5579106464a4a4ab83ec22ad0e0df503 Mon Sep 17 00:00:00 2001 From: Sibiraj Date: Mon, 26 Feb 2018 14:55:11 +0530 Subject: [PATCH] feat: option to set font-size closes #31 --- extras/docs/toolbar.md | 1 + package.json | 1 - .../ngx-editor/common/ngx-editor.defaults.ts | 2 +- .../services/command-executor.service.ts | 66 ++++++++++++++++++- .../ngx-editor-toolbar.component.html | 23 ++++++- .../ngx-editor-toolbar.component.ts | 11 ++++ 6 files changed, 99 insertions(+), 5 deletions(-) diff --git a/extras/docs/toolbar.md b/extras/docs/toolbar.md index 9fae31e0..66fb0142 100644 --- a/extras/docs/toolbar.md +++ b/extras/docs/toolbar.md @@ -5,6 +5,7 @@ Toolbar option is an array of arrays. The default is ```JSON [ ["bold", "italic", "underline", "strikeThrough", "superscript", "subscript"], + ["fontSize", "color"], ["justifyLeft", "justifyCenter", "justifyRight", "justifyFull", "indent", "outdent"], ["cut", "copy", "delete", "removeFormat", "undo", "redo"], ["paragraph", "blockquote", "removeBlockquote", "horizontalLine", "orderedList", "unorderedList"], diff --git a/package.json b/package.json index 3cbe78b1..6661a8bb 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,6 @@ "karma-jasmine-html-reporter": "^0.2.2", "ng-packagr": "^2.1.0", "ngx-bootstrap": "^2.0.2", - "node-sass": "^4.7.2", "protractor": "~5.1.2", "rxjs": "^5.5.6", "ts-node": "~4.1.0", diff --git a/src/app/ngx-editor/common/ngx-editor.defaults.ts b/src/app/ngx-editor/common/ngx-editor.defaults.ts index fd5d19d4..98051271 100644 --- a/src/app/ngx-editor/common/ngx-editor.defaults.ts +++ b/src/app/ngx-editor/common/ngx-editor.defaults.ts @@ -15,7 +15,7 @@ export const ngxEditorConfig = { imageEndPoint: '', toolbar: [ ['bold', 'italic', 'underline', 'strikeThrough', 'superscript', 'subscript'], - ['color'], + ['fontSize', 'color'], ['justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull', 'indent', 'outdent'], ['cut', 'copy', 'delete', 'removeFormat', 'undo', 'redo'], ['paragraph', 'blockquote', 'removeBlockquote', 'horizontalLine', 'orderedList', 'unorderedList'], diff --git a/src/app/ngx-editor/common/services/command-executor.service.ts b/src/app/ngx-editor/common/services/command-executor.service.ts index 88c5ca53..f766b555 100644 --- a/src/app/ngx-editor/common/services/command-executor.service.ts +++ b/src/app/ngx-editor/common/services/command-executor.service.ts @@ -110,7 +110,7 @@ export class CommandExecutorService { if (document.getSelection().type !== 'Range') { const restored = Utils.restoreSelection(this.savedSelection); if (restored) { - document.execCommand('insertHTML', false, newUrl); + this.insertHtml(newUrl); } } else { throw new Error('Only new links can be inserted. You cannot edit URL`s'); @@ -150,4 +150,68 @@ export class CommandExecutorService { return; } + /** set font size for text */ + setFontSize(fontSize: string): void { + + if (this.savedSelection) { + const deletedValue = this.deleteAndGetElement(); + + if (deletedValue) { + + const restored = Utils.restoreSelection(this.savedSelection); + + if (restored) { + if (this.isNumeric(fontSize)) { + const fontPx = '' + deletedValue + ''; + this.insertHtml(fontPx); + } else { + const font = '' + deletedValue + ''; + this.insertHtml(font); + } + } + } else { + + } + + } else { + throw new Error('Range out of the editor'); + } + } + + /** insert HTML */ + private insertHtml(html: string): void { + + const isHTMLInserted = document.execCommand('insertHTML', false, html); + + if (!isHTMLInserted) { + throw new Error('Unable to perform the operation'); + } + + return; + } + + /** + * check whether the value is a number or string + * if number return true + * else return false + */ + private isNumeric(value: any): boolean { + return /^-{0,1}\d+$/.test(value); + } + + /** delete the text at selected range and return the value */ + private deleteAndGetElement(): any { + + let slectedText; + + if (this.savedSelection) { + slectedText = this.savedSelection.toString(); + this.savedSelection.deleteContents(); + return slectedText; + } + + return false; + + } + } diff --git a/src/app/ngx-editor/ngx-editor-toolbar/ngx-editor-toolbar.component.html b/src/app/ngx-editor/ngx-editor-toolbar/ngx-editor-toolbar.component.html index e40d65f4..6349c5ea 100644 --- a/src/app/ngx-editor/ngx-editor-toolbar/ngx-editor-toolbar.component.html +++ b/src/app/ngx-editor/ngx-editor-toolbar/ngx-editor-toolbar.component.html @@ -26,6 +26,10 @@
+ +
+ + + +
+
+
+ + +
+ +
+
+
diff --git a/src/app/ngx-editor/ngx-editor-toolbar/ngx-editor-toolbar.component.ts b/src/app/ngx-editor/ngx-editor-toolbar/ngx-editor-toolbar.component.ts index c64aa999..73e0ed80 100644 --- a/src/app/ngx-editor/ngx-editor-toolbar/ngx-editor-toolbar.component.ts +++ b/src/app/ngx-editor/ngx-editor-toolbar/ngx-editor-toolbar.component.ts @@ -178,6 +178,17 @@ export class NgxEditorToolbarComponent implements OnInit { return; } + /** set font size */ + setFontSize(fontSize: string): void { + + try { + this._commandExecutorService.setFontSize(fontSize); + } catch (error) { + this._messageService.sendMessage(error.message); + } + return; + } + ngOnInit() { this.buildUrlForm(); this.buildInsertImageForm();