@mediafly/js-ui 50.2.0 → 50.2.1-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -99,7 +99,7 @@ let FontSelectorController = class FontSelectorController {
99
99
  this.popularFontsText = this.translateFactory.instant('JSUI.POPULAR_FONTS');
100
100
  this.requiredText = this.translateFactory.instant('JSUI.REQUIRED');
101
101
  this.whatAreGoogleFonts = this.translateFactory.instant('JSUI.WHAT_ARE_GOOGLE_FONTS');
102
- // init list of popylar fonts
102
+ // init list of popular fonts
103
103
  this.googleWebfontsApiService.getFontList('popularity')
104
104
  .then(popularList => {
105
105
  this.popularFonts = popularList.map(f => f.family);
@@ -1,7 +1,9 @@
1
1
  /// <reference types="angular-sanitize" />
2
2
  /// <reference types="angular-mocks" />
3
3
  import { sanitize } from 'angular';
4
- import { FontFamilyStyle } from '../font-selector/font-selector';
4
+ import { Range } from 'quill';
5
+ import { Delta, EmitterSource } from 'quill/core';
6
+ import type { TranslateFactory } from '../translate/translate-factory';
5
7
  export default class RichTextComponentController implements ng.IComponentController {
6
8
  private $element;
7
9
  private $rootScope;
@@ -11,24 +13,29 @@ export default class RichTextComponentController implements ng.IComponentControl
11
13
  allowColorSelection?: boolean;
12
14
  allowFontSelection?: boolean;
13
15
  isRequired: boolean;
14
- protected cachedSelection?: {
15
- index: number;
16
- length: number;
17
- };
18
- protected fontStyle?: FontFamilyStyle;
16
+ protected cachedSelection?: Range | null;
17
+ protected colorInputId: string;
18
+ protected editorSizeId: string;
19
19
  protected fontSelectorId: string;
20
20
  protected requiredText: string;
21
21
  protected selectedColor: string;
22
22
  protected selectedFont: string;
23
+ protected toolbarId: string;
23
24
  protected validationForm: ng.IFormController;
24
25
  private colorPicker;
25
26
  private editor;
26
27
  private ngModelCtrl;
27
- private debouncedOnTextChange;
28
+ private debouncedSanitizeAndValidate;
28
29
  private DEFAULT_COLOR;
29
30
  private DEFAULT_FONT;
30
31
  private DEFAULT_SIZE;
31
- constructor($element: ng.IAugmentedJQuery, $rootScope: JSUIRootScope, $sanitize: sanitize.ISanitizeService, $timeout: ng.ITimeoutService, translateFactory: any);
32
+ private HEADING_1_COLOR;
33
+ private HEADING_1_FONT;
34
+ private HEADING_1_SIZE;
35
+ private HEADING_2_COLOR;
36
+ private HEADING_2_FONT;
37
+ private HEADING_2_SIZE;
38
+ constructor($element: ng.IAugmentedJQuery, $rootScope: JSUIRootScope, $sanitize: sanitize.ISanitizeService, $timeout: ng.ITimeoutService, translateFactory: TranslateFactory);
32
39
  $onChanges(changes: {
33
40
  isRequired?: ng.IChangesObject<boolean>;
34
41
  }): void;
@@ -38,14 +45,39 @@ export default class RichTextComponentController implements ng.IComponentControl
38
45
  protected colorChanged(): void;
39
46
  protected colorInputOpened(): void;
40
47
  protected fontSelected(fontFamily: string): void;
41
- protected manageValidity(): void;
42
- protected onTextChange(_delta: any, _oldContents: any, source: any): void;
43
- protected selectionChange(range: any, oldRange: any, source: any): void;
44
- private applyColor;
45
- private cacheSelection;
46
- private sanitizeContents;
47
- private formatToDefaults;
48
- private getFontFromLeaf;
49
- private updateFontDisplay;
50
- private updateColorDisplay;
48
+ /** The user entered/deleted/modified some text. Handle any formatting issues, then store the model. */
49
+ protected onTextChange(delta: Delta, _oldContents: Delta, source: EmitterSource): void;
50
+ /** The user moved the cursor. Fix missing formats and update the toolbar to reflect the current selection. */
51
+ protected selectionChange(range: Range, _oldRange: Range, source: EmitterSource): void;
52
+ private checkValidity;
53
+ /**
54
+ * Quill's "Clear formatting" button leaves us with no font, size, or color, so the style is inherited from the CSS.
55
+ * We don't want that: we want everything to be fully defined, so that the user-entered text can be faithfully
56
+ * reproduced elsewhere.
57
+ */
58
+ private fixClearFormatting;
59
+ /**
60
+ * Quill's "Heading 1" and "Heading 2" just change the <p> to <h1> or <h2>. The inner <span> will still have a `style` on it,
61
+ * setting the font, size, and color. Quill prefers no `style` tags, so it expects the CSS to cover that. We use `style` for
62
+ * all our formatting, so we also need to define appropriate values for the Headings.
63
+ */
64
+ private fixHeadingFormat;
65
+ /**
66
+ * Quill likes to default to no formatting: an empty paragraph has no font, size, or color, so the style is inherited
67
+ * from the CSS.
68
+ * We don't want that: we want everything to be fully defined, so that the user-entered text can be faithfully
69
+ * reproduced elsewhere.
70
+ * We try to get the format from the previous line, or fall back to the default.
71
+ */
72
+ private fixMissingFormats;
73
+ private getColorFromFormat;
74
+ private getFontFromFormat;
75
+ private getSizeFromFormat;
76
+ private sanitizeAndValidate;
77
+ /** Updates the color shown in the toolbar. Does not change the color being used in the text. */
78
+ private updateColorPicker;
79
+ /** Updates the font shown in the toolbar. Does not change the font being used in the text. */
80
+ private updateFontPicker;
81
+ /** Updates the size shown in the toolbar. Does not change the size being used in the text. */
82
+ private updateSizePicker;
51
83
  }
@@ -1,4 +1,4 @@
1
- <div id="toolbar" class="c-rich-text">
1
+ <div id="{{ $ctrl.toolbarId }}" class="c-rich-text">
2
2
 
3
3
  <span ng-if="$ctrl.allowFontSelection" class="ql-formats c-rich-text-font-container">
4
4
  <mfly:font-selector
@@ -9,7 +9,7 @@
9
9
  </span>
10
10
 
11
11
  <span class="ql-formats">
12
- <select id="editorSize" class="ql-size">
12
+ <select id="{{ $ctrl.editorSizeId }}" class="ql-size">
13
13
  <option value="10px" data-value="10px">10px</option>
14
14
  <option value="12px" data-value="12px">12px</option>
15
15
  <option value="14px" data-value="14px">14px</option>
@@ -22,7 +22,7 @@
22
22
 
23
23
  <!-- ng show for event handler -->
24
24
  <span ng-show="$ctrl.allowColorSelection" class="ql-formats">
25
- <input id="rteColorInput"
25
+ <input id="{{ $ctrl.colorInputId }}"
26
26
  type="color"
27
27
  ng-model="$ctrl.selectedColor">
28
28
  </span>
@@ -1,10 +1,29 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
2
14
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
15
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
16
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
17
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
18
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
19
  };
20
+ var __importStar = (this && this.__importStar) || function (mod) {
21
+ if (mod && mod.__esModule) return mod;
22
+ var result = {};
23
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24
+ __setModuleDefault(result, mod);
25
+ return result;
26
+ };
8
27
  var __metadata = (this && this.__metadata) || function (k, v) {
9
28
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
29
  };
@@ -15,11 +34,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
15
34
  return (mod && mod.__esModule) ? mod : { "default": mod };
16
35
  };
17
36
  Object.defineProperty(exports, "__esModule", { value: true });
37
+ // tslint:disable: max-file-line-count
18
38
  const angular_1 = __importDefault(require("angular"));
19
39
  const lodash_1 = __importDefault(require("lodash"));
20
- const quill_1 = __importDefault(require("quill"));
21
- const jquery_1 = __importDefault(require("jquery"));
40
+ const quill_1 = __importStar(require("quill"));
22
41
  const decorators_1 = require("../decorators");
42
+ const uuid_1 = require("uuid");
23
43
  // text editor requires sanitization but also needs to allow for style attributes which are removed by default
24
44
  angular_1.default.module('app').config(['$sanitizeProvider', ($sanitizeProvider) => {
25
45
  $sanitizeProvider.addValidAttrs(['style']);
@@ -31,10 +51,16 @@ let RichTextComponentController = class RichTextComponentController {
31
51
  this.$sanitize = $sanitize;
32
52
  this.$timeout = $timeout;
33
53
  this.translateFactory = translateFactory;
34
- this.debouncedOnTextChange = lodash_1.default.debounce((delta, oldContents, source) => this.onTextChange(delta, oldContents, source), 250);
54
+ this.debouncedSanitizeAndValidate = lodash_1.default.debounce(() => this.sanitizeAndValidate(), 250);
35
55
  this.DEFAULT_COLOR = '#535353';
36
56
  this.DEFAULT_FONT = 'Roboto';
37
57
  this.DEFAULT_SIZE = '14px';
58
+ this.HEADING_1_COLOR = '#535353';
59
+ this.HEADING_1_FONT = 'Poppins';
60
+ this.HEADING_1_SIZE = '24px';
61
+ this.HEADING_2_COLOR = '#535353';
62
+ this.HEADING_2_FONT = 'Poppins';
63
+ this.HEADING_2_SIZE = '14px';
38
64
  }
39
65
  $onChanges(changes) {
40
66
  if (!changes.isRequired) {
@@ -43,177 +69,319 @@ let RichTextComponentController = class RichTextComponentController {
43
69
  if (changes.isRequired.currentValue === changes.isRequired.previousValue) {
44
70
  return;
45
71
  }
46
- this.manageValidity();
72
+ this.checkValidity();
47
73
  }
48
74
  $onDestroy() {
49
- this.editor.off('text-change', this.debouncedOnTextChange);
50
- this.colorPicker.removeEventListener('change', this.colorChanged.bind(this));
51
- this.colorPicker.removeEventListener('click', this.colorInputOpened.bind(this));
75
+ var _a, _b;
76
+ this.editor.off('text-change', (delta, oldContents, source) => { this.onTextChange(delta, oldContents, source); });
77
+ (_a = this.colorPicker) === null || _a === void 0 ? void 0 : _a.removeEventListener('change', this.colorChanged.bind(this));
78
+ (_b = this.colorPicker) === null || _b === void 0 ? void 0 : _b.removeEventListener('click', this.colorInputOpened.bind(this));
52
79
  }
53
80
  $onInit() {
54
81
  this.requiredText = this.translateFactory.instant('JSUI.REQUIRED');
55
- this.selectedColor = this.DEFAULT_COLOR;
56
- this.selectedFont = this.DEFAULT_FONT;
57
- this.fontSelectorId = 'rteFontSelector' + Math.floor(Math.random() * 1000 + 1);
58
- this.colorPicker = document.getElementById('rteColorInput');
59
- this.colorPicker.addEventListener('change', this.colorChanged.bind(this));
60
- this.colorPicker.addEventListener('click', this.colorInputOpened.bind(this));
61
- // programatically reset size
62
- jquery_1.default('.ql-size').val(this.DEFAULT_SIZE);
82
+ this.colorInputId = 'rteColorInput-' + uuid_1.v4();
83
+ this.editorSizeId = 'rteEditorSize-' + uuid_1.v4();
84
+ this.fontSelectorId = 'rteFontSelector-' + uuid_1.v4();
85
+ this.toolbarId = 'rteToolbar-' + uuid_1.v4();
86
+ this.$timeout(() => {
87
+ var _a, _b;
88
+ this.colorPicker = document.getElementById(this.colorInputId);
89
+ (_a = this.colorPicker) === null || _a === void 0 ? void 0 : _a.addEventListener('change', this.colorChanged.bind(this));
90
+ (_b = this.colorPicker) === null || _b === void 0 ? void 0 : _b.addEventListener('click', this.colorInputOpened.bind(this));
91
+ this.updateFontPicker(this.DEFAULT_FONT);
92
+ this.updateSizePicker(this.DEFAULT_SIZE);
93
+ this.updateColorPicker(this.DEFAULT_COLOR);
94
+ });
63
95
  }
64
96
  $postLink() {
65
97
  quill_1.default.register(quill_1.default.import('attributors/class/background'), true);
66
98
  quill_1.default.register(quill_1.default.import('attributors/style/color'), true);
67
99
  const fontStyle = quill_1.default.import('attributors/style/font');
68
- fontStyle.whitelist = null; // fonts not in the allow-list get dropped on other edits (CE-6314)
100
+ // Setting the whitelist to undefined allows all fonts. (We don't know what fonts Google Fonts will give us.)
101
+ fontStyle.whitelist = undefined;
69
102
  quill_1.default.register(fontStyle, true);
70
103
  const fontSizeArr = ['10px', '12px', '14px', '16px', '20px', '24px', '32px', '42px', '54px', '68px', '84px', '98px'];
71
- const Size = quill_1.default.import('attributors/style/size');
72
- Size.whitelist = fontSizeArr;
73
- quill_1.default.register(Size, true);
104
+ const size = quill_1.default.import('attributors/style/size');
105
+ size.whitelist = fontSizeArr;
106
+ quill_1.default.register(size, true);
74
107
  const options = {
75
108
  modules: {
76
- toolbar: '#toolbar'
109
+ toolbar: `#${this.toolbarId}`,
77
110
  },
78
111
  theme: 'snow',
79
112
  };
80
- this.editor = new quill_1.default(this.$element.find('.js-rich-text-target')[0], options);
81
- // Disable spellcheck because of a bad bug that deletes text (CE-4692).
82
- this.editor.root.setAttribute('spellcheck', false);
83
- this.editor.on('text-change', this.debouncedOnTextChange);
84
- this.editor.on('selection-change', this.selectionChange.bind(this));
113
+ this.$timeout(() => {
114
+ this.editor = new quill_1.default(this.$element.find('.js-rich-text-target')[0], options);
115
+ // Disable spellcheck because of a bad bug that deletes text (CE-4692).
116
+ this.editor.root.setAttribute('spellcheck', 'false');
117
+ this.editor.on('text-change', (delta, oldContents, source) => { this.onTextChange(delta, oldContents, source); });
118
+ this.editor.on('selection-change', this.selectionChange.bind(this));
119
+ });
85
120
  this.ngModelCtrl.$render = () => {
86
- if (!this.ngModelCtrl.$viewValue) {
87
- this.editor.setText('\n');
88
- return;
89
- }
90
- this.editor.root.innerHTML = this.ngModelCtrl.$viewValue;
121
+ this.$timeout(() => {
122
+ if (!this.ngModelCtrl.$viewValue) {
123
+ this.editor.setText('\n');
124
+ return;
125
+ }
126
+ this.editor.root.innerHTML = this.ngModelCtrl.$viewValue;
127
+ });
91
128
  };
92
129
  }
93
130
  colorChanged() {
94
- this.applyColor();
131
+ // Re-apply the text selection that was made before selecting a color,
132
+ // as the act of hiding some browsers' color inputs can change the selected text
133
+ if (this.cachedSelection) {
134
+ this.editor.setSelection(this.cachedSelection.index, this.cachedSelection.length);
135
+ }
136
+ this.editor.format('color', this.selectedColor);
137
+ this.debouncedSanitizeAndValidate();
95
138
  }
96
139
  colorInputOpened() {
97
- this.cacheSelection();
140
+ this.cachedSelection = this.editor.getSelection();
98
141
  }
99
142
  fontSelected(fontFamily) {
100
143
  if (!fontFamily) {
101
144
  return;
102
145
  }
103
146
  this.$rootScope.$broadcast('richTextToolbarContainer:closeContainer');
104
- const fontAtributor = quill_1.default.import('attributors/style/font');
105
- fontAtributor.whitelist = [fontFamily];
106
- this.updateFontDisplay(fontFamily);
107
- quill_1.default.register(fontAtributor, true);
108
147
  this.editor.format('font', fontFamily);
109
- this.sanitizeContents();
148
+ this.debouncedSanitizeAndValidate();
110
149
  }
111
- manageValidity() {
112
- const isValid = !this.isRequired || !!this.ngModelCtrl.$viewValue;
113
- this.ngModelCtrl.$setValidity('required', isValid);
150
+ /** The user entered/deleted/modified some text. Handle any formatting issues, then store the model. */
151
+ onTextChange(delta, _oldContents, source) {
152
+ if (source !== 'user') {
153
+ return;
154
+ }
155
+ this.$timeout(() => {
156
+ // When we choose "Heading 1" or "Heading 2", we also need to set the font, size, and color.
157
+ this.fixHeadingFormat(delta);
158
+ // Quill's "Clear formatting" button leaves us inheriting styles from the CSS. We want to keep things explicit.
159
+ this.fixClearFormatting(delta);
160
+ // Also, things like adding a line break can leave us with no style.
161
+ this.fixMissingFormats();
162
+ });
163
+ this.debouncedSanitizeAndValidate();
114
164
  }
115
- onTextChange(_delta, _oldContents, source) {
165
+ /** The user moved the cursor. Fix missing formats and update the toolbar to reflect the current selection. */
166
+ selectionChange(range, _oldRange, source) {
116
167
  if (source !== 'user') {
117
168
  return;
118
169
  }
119
- const userClearedEditor = (this.editor.root.innerHTML === `<p><br></p>`);
120
- if (userClearedEditor) {
121
- // reset the color picker to the default by passing an undefined range
122
- this.editor.root.innerHTML = '';
123
- this.updateColorDisplay(undefined);
124
- this.ngModelCtrl.$setViewValue(undefined);
125
- this.manageValidity();
126
- this.formatToDefaults();
170
+ if (!range) {
171
+ return;
172
+ }
173
+ this.fixMissingFormats();
174
+ this.$timeout(() => {
175
+ // Quill updates the toolbar to reflect most formatting (bold, italics, bullet points, etc.), but
176
+ // fonts, text size, and color are handled by plugins so we need to update those ourselves.
177
+ const format = this.editor.getFormat(range);
178
+ this.updateFontPicker(this.getFontFromFormat(format));
179
+ this.updateColorPicker(this.getColorFromFormat(format));
180
+ this.updateSizePicker(this.getSizeFromFormat(format));
181
+ });
182
+ }
183
+ checkValidity() {
184
+ const isValid = !this.isRequired || !!this.ngModelCtrl.$viewValue;
185
+ this.$timeout(() => {
186
+ this.ngModelCtrl.$setValidity('required', isValid);
187
+ });
188
+ }
189
+ /**
190
+ * Quill's "Clear formatting" button leaves us with no font, size, or color, so the style is inherited from the CSS.
191
+ * We don't want that: we want everything to be fully defined, so that the user-entered text can be faithfully
192
+ * reproduced elsewhere.
193
+ */
194
+ fixClearFormatting(delta) {
195
+ // Using the "Clear formatting" button creates a delta that sets font, size, and color to `null`. Detect any of those.
196
+ // (Usually you'd get all three together, but some might be missing from the delta if they were already clear,
197
+ // for example if we were editing an old Workspace Text Section that was created using an older version of this component.)
198
+ if (delta.ops.some((op) => {
199
+ var _a, _b, _c;
200
+ return op.retain && (((_a = op.attributes) === null || _a === void 0 ? void 0 : _a.font) === null || ((_b = op.attributes) === null || _b === void 0 ? void 0 : _b.size) === null || ((_c = op.attributes) === null || _c === void 0 ? void 0 : _c.color) === null);
201
+ })) {
202
+ this.editor.format('font', this.DEFAULT_FONT);
203
+ this.updateFontPicker(this.DEFAULT_FONT);
204
+ this.editor.format('size', this.DEFAULT_SIZE);
205
+ this.updateSizePicker(this.DEFAULT_SIZE);
206
+ this.editor.format('color', this.DEFAULT_COLOR);
207
+ this.updateColorPicker(this.DEFAULT_COLOR);
208
+ }
209
+ }
210
+ /**
211
+ * Quill's "Heading 1" and "Heading 2" just change the <p> to <h1> or <h2>. The inner <span> will still have a `style` on it,
212
+ * setting the font, size, and color. Quill prefers no `style` tags, so it expects the CSS to cover that. We use `style` for
213
+ * all our formatting, so we also need to define appropriate values for the Headings.
214
+ */
215
+ fixHeadingFormat(delta) {
216
+ var _a;
217
+ // Look for an operation that sets a heading style (header = 1 or 2), or clears it (header = null)
218
+ const headingOp = delta.ops.find((op) => { var _a; return ((_a = op.attributes) === null || _a === void 0 ? void 0 : _a.header) !== undefined; });
219
+ if (!headingOp) {
127
220
  return;
128
221
  }
129
- this.sanitizeContents();
130
222
  const selection = this.editor.getSelection();
131
223
  if (!selection) {
132
224
  return;
133
225
  }
134
- const format = this.editor.getFormat(selection.index, 0);
135
- // the following checks for an empty formatting
136
- if (lodash_1.default.isEmpty(format)) {
137
- // if format is empty then set back to default styles
138
- this.formatToDefaults();
226
+ // The selection might cover multiple paragraphs, so get them all
227
+ let lines = this.editor.getLines(selection);
228
+ if (lines.length === 0) {
229
+ // The selection has zero length, so get the current paragraph
230
+ const line = this.editor.getLine(selection.index);
231
+ // (Perversely, `line` is an array, containing the actual line followed by a number :shrug:)
232
+ if (!line || !line[0]) {
233
+ return;
234
+ }
235
+ lines = [line[0]];
236
+ }
237
+ let font = this.DEFAULT_FONT;
238
+ let size = this.DEFAULT_SIZE;
239
+ let color = this.DEFAULT_COLOR;
240
+ switch ((_a = headingOp.attributes) === null || _a === void 0 ? void 0 : _a.header) {
241
+ case 1:
242
+ font = this.HEADING_1_FONT;
243
+ size = this.HEADING_1_SIZE;
244
+ color = this.HEADING_1_COLOR;
245
+ break;
246
+ case 2:
247
+ font = this.HEADING_2_FONT;
248
+ size = this.HEADING_2_SIZE;
249
+ color = this.HEADING_2_COLOR;
250
+ break;
139
251
  }
252
+ for (const line of lines) {
253
+ const range = new quill_1.Range(line.offset(), line.length());
254
+ this.editor.formatText(range, 'font', font);
255
+ this.editor.formatText(range, 'size', size);
256
+ this.editor.formatText(range, 'color', color);
257
+ }
258
+ this.updateFontPicker(font);
259
+ this.updateSizePicker(size);
260
+ this.updateColorPicker(color);
140
261
  }
141
- selectionChange(range, oldRange, source) {
142
- if (source !== 'user') {
262
+ /**
263
+ * Quill likes to default to no formatting: an empty paragraph has no font, size, or color, so the style is inherited
264
+ * from the CSS.
265
+ * We don't want that: we want everything to be fully defined, so that the user-entered text can be faithfully
266
+ * reproduced elsewhere.
267
+ * We try to get the format from the previous line, or fall back to the default.
268
+ */
269
+ fixMissingFormats() {
270
+ // When debugging this, it can be useful to add this to your CSS to highlight problems:
271
+ // .ql-editor { color: #f0f; font-family: 'Wingdings', sans-serif; font-size: 9px; }
272
+ const selection = this.editor.getSelection();
273
+ if (!selection || selection.length) {
274
+ // Some text is selected; we don't want to change this
143
275
  return;
144
276
  }
145
- if (!range) {
277
+ const format = this.editor.getFormat();
278
+ if (format.font && format.size && format.color) {
279
+ // We are fully formatted; nothing more to do
146
280
  return;
147
281
  }
148
- const leaf = this.editor.getLeaf(range.index);
149
- const font = this.getFontFromLeaf(leaf);
150
- const format = this.editor.getFormat(range);
151
- this.updateColorDisplay(range);
152
- // if get format is empty we can switch to the defaults
153
- if (!format.size) {
154
- jquery_1.default('.ql-size').val(this.DEFAULT_SIZE);
155
- jquery_1.default('#editorSize .ql-picker-label').attr('data-value', this.DEFAULT_SIZE);
156
- jquery_1.default('#editorSize .ql-picker-label').attr('data-label', this.DEFAULT_SIZE);
282
+ // Copy missing format from the previous character (or the next, if we're at the start of the document)
283
+ const nearbyCharIndex = selection.index === 0 ? 1 : selection.index - 1;
284
+ const nearbyRange = {
285
+ index: nearbyCharIndex,
286
+ length: 0,
287
+ };
288
+ const nearbyFormat = this.editor.getFormat(nearbyRange);
289
+ this.editor.format('bold', nearbyFormat.bold);
290
+ this.editor.format('italic', nearbyFormat.italic);
291
+ this.editor.format('underline', nearbyFormat.underline);
292
+ if (!format.font) {
293
+ const font = this.getFontFromFormat(nearbyFormat);
294
+ this.editor.format('font', font);
295
+ this.updateFontPicker(font);
157
296
  }
158
- if (font) {
159
- this.updateFontDisplay(font.replace(/"/g, ''));
160
- return;
297
+ if (!format.size) {
298
+ const size = this.getSizeFromFormat(nearbyFormat);
299
+ this.editor.format('size', size);
300
+ this.updateSizePicker(size);
161
301
  }
162
- if (!format.font) {
163
- this.editor.format('font', this.DEFAULT_FONT);
302
+ if (!format.color) {
303
+ const color = this.getColorFromFormat(nearbyFormat);
304
+ this.editor.format('color', color);
305
+ this.updateColorPicker(color);
164
306
  }
165
307
  }
166
- applyColor() {
167
- // CE-3031: Re-apply the text selection that was made before selecting a color
168
- // as the act of hiding some browsers color inputs can change the selected text
169
- if (this.cachedSelection) {
170
- this.editor.setSelection(this.cachedSelection.index, this.cachedSelection.length);
308
+ getColorFromFormat(format) {
309
+ if (!format.color) {
310
+ // We get no color if the default color is selected; or if the selection contains multiple colors, one of which is the default.
311
+ return this.DEFAULT_COLOR;
171
312
  }
172
- this.editor.format('color', this.selectedColor);
173
- this.sanitizeContents();
174
- }
175
- cacheSelection() {
176
- this.cachedSelection = this.editor.getSelection();
313
+ if (typeof format.color === 'string') {
314
+ // The selected range is all the same color, which is not the default.
315
+ return format.color;
316
+ }
317
+ if (Array.isArray(format.color)) {
318
+ // The selected range contains multiple colors, none of which are the default. Return the default, to be consistent with other multi-color selections.
319
+ return this.DEFAULT_COLOR;
320
+ }
321
+ // Fall back to the default (should never get here)
322
+ return this.DEFAULT_COLOR;
177
323
  }
178
- sanitizeContents() {
179
- const html = this.$sanitize(this.editor.root.innerHTML);
180
- this.ngModelCtrl.$setViewValue(html);
181
- this.manageValidity();
324
+ getFontFromFormat(format) {
325
+ if (!format.font) {
326
+ // No font is defined for the selection, so show the default. This can happen if you're on a new empty line.
327
+ return this.DEFAULT_FONT;
328
+ }
329
+ if (typeof format.font === 'string') {
330
+ // The range uses a single font.
331
+ return format.font;
332
+ }
333
+ if (Array.isArray(format.font)) {
334
+ // The selected range contains multiple fonts, so show blank.
335
+ return '';
336
+ }
337
+ // Fall back to the default (should never get here)
338
+ return this.DEFAULT_FONT;
182
339
  }
183
- formatToDefaults() {
184
- this.updateFontDisplay(this.DEFAULT_FONT);
185
- this.editor.format('color', this.DEFAULT_COLOR);
186
- this.editor.format('size', this.DEFAULT_SIZE);
340
+ getSizeFromFormat(format) {
341
+ if (!format.size) {
342
+ // We get no size if the default size is selected; or if the selection contains multiple sizes, one of which is the default.
343
+ return this.DEFAULT_SIZE;
344
+ }
345
+ if (typeof format.size === 'string') {
346
+ // The selected range is all the same size, which is not the default.
347
+ return format.size;
348
+ }
349
+ if (Array.isArray(format.size)) {
350
+ // The selected range contains multiple sizes, none of which are the default. Return the first size in the array.
351
+ return format.size[0];
352
+ }
353
+ // Fall back to the default (should never get here)
354
+ return this.DEFAULT_FONT;
187
355
  }
188
- getFontFromLeaf(leaf) {
189
- if (!leaf) {
190
- return;
356
+ sanitizeAndValidate() {
357
+ let html;
358
+ // An empty Quill editor's html might be like:
359
+ // <p><span style="font-family: Roboto; font-size: 14px; color: rgb(83, 83, 83);"><span class="ql-cursor">&#65279;</span></span></p>
360
+ // Detect emptiness and return `undefined` instead, for easier "Required" validation.
361
+ if (!this.editor.getText().trim()) {
362
+ html = undefined;
191
363
  }
192
- if (!leaf[0].parent.domNode.style.fontFamily) {
193
- return;
364
+ else {
365
+ // Sometimes Quill's inner HTML includes its own cursor. We don't want to save that.
366
+ const withoutCursor = this.editor.root.innerHTML.replace(/\<span class="ql-cursor"\>.?\<\/span\>/, '');
367
+ html = this.$sanitize(withoutCursor);
194
368
  }
195
- return leaf[0].parent.domNode.style.fontFamily;
369
+ this.ngModelCtrl.$setViewValue(html);
370
+ this.checkValidity();
196
371
  }
197
- updateFontDisplay(fontFamily) {
198
- this.selectedFont = fontFamily;
199
- this.fontStyle = {
200
- 'font-family': fontFamily
201
- };
372
+ /** Updates the color shown in the toolbar. Does not change the color being used in the text. */
373
+ updateColorPicker(color) {
374
+ this.selectedColor = color;
202
375
  }
203
- updateColorDisplay(range) {
204
- this.$timeout(() => {
205
- const format = this.editor.getFormat(range);
206
- const color = format.color;
207
- if (!color || lodash_1.default.isEmpty(color)) {
208
- this.selectedColor = this.DEFAULT_COLOR;
209
- return;
210
- }
211
- if (lodash_1.default.isArray(color)) {
212
- this.selectedColor = color[0];
213
- return;
214
- }
215
- this.selectedColor = color;
216
- });
376
+ /** Updates the font shown in the toolbar. Does not change the font being used in the text. */
377
+ updateFontPicker(font) {
378
+ this.selectedFont = font;
379
+ }
380
+ /** Updates the size shown in the toolbar. Does not change the size being used in the text. */
381
+ updateSizePicker(size) {
382
+ this.$element.find('.ql-size').val(size);
383
+ this.$element.find(`#${this.editorSizeId} .ql-picker-label`).attr('data-value', size);
384
+ this.$element.find(`#${this.editorSizeId} .ql-picker-label`).attr('data-label', size);
217
385
  }
218
386
  };
219
387
  __decorate([