@mediafly/js-ui 51.4.2 → 51.5.0-0

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.
Files changed (39) hide show
  1. package/lib/font-selector/font-selector.d.ts +7 -3
  2. package/lib/font-selector/font-selector.html +7 -7
  3. package/lib/font-selector/font-selector.js +27 -12
  4. package/lib/font-selector/font-selector.js.map +1 -1
  5. package/lib/font-selector/google-webfonts-api.service.d.ts +4 -1
  6. package/lib/font-selector/google-webfonts-api.service.js +17 -3
  7. package/lib/font-selector/google-webfonts-api.service.js.map +1 -1
  8. package/lib/font-selector/webfont-loader.service.d.ts +1 -0
  9. package/lib/font-selector/webfont-loader.service.js +9 -0
  10. package/lib/font-selector/webfont-loader.service.js.map +1 -1
  11. package/lib/font-weight-selector/font-weight-selector.d.ts +30 -0
  12. package/lib/font-weight-selector/font-weight-selector.html +26 -0
  13. package/lib/font-weight-selector/font-weight-selector.js +162 -0
  14. package/lib/font-weight-selector/font-weight-selector.js.map +1 -0
  15. package/lib/font-weight-selector/index.d.ts +1 -0
  16. package/lib/font-weight-selector/index.js +4 -0
  17. package/lib/font-weight-selector/index.js.map +1 -0
  18. package/lib/index.d.ts +1 -0
  19. package/lib/index.js +1 -0
  20. package/lib/index.js.map +1 -1
  21. package/lib/rich-text/rich-text.component.d.ts +9 -2
  22. package/lib/rich-text/rich-text.component.html +9 -2
  23. package/lib/rich-text/rich-text.component.js +45 -7
  24. package/lib/rich-text/rich-text.component.js.map +1 -1
  25. package/lib/tsconfig.deploy.tsbuildinfo +42 -10
  26. package/package.json +1 -1
  27. package/src/components/font-selector/font-selector.html +7 -7
  28. package/src/components/font-selector/font-selector.ts +31 -16
  29. package/src/components/font-selector/google-webfonts-api.service.ts +14 -0
  30. package/src/components/font-selector/webfont-loader.service.ts +12 -0
  31. package/src/components/font-weight-selector/font-weight-selector.html +26 -0
  32. package/src/components/font-weight-selector/font-weight-selector.less +48 -0
  33. package/src/components/font-weight-selector/font-weight-selector.ts +147 -0
  34. package/src/components/font-weight-selector/index.ts +1 -0
  35. package/src/components/index.less +1 -0
  36. package/src/components/index.ts +1 -0
  37. package/src/components/rich-text/rich-text.component.html +9 -2
  38. package/src/components/rich-text/rich-text.component.less +1 -0
  39. package/src/components/rich-text/rich-text.component.ts +49 -3
@@ -0,0 +1,48 @@
1
+ @import '../less/variables/colors.less';
2
+
3
+ // Avoid alignment issues with the Quill styles
4
+ mfly\:font-weight-selector {
5
+ display: block;
6
+ float: left;
7
+ height: 24px;
8
+ }
9
+
10
+ .ql-snow.ql-toolbar button.c-font-weight-selector__select {
11
+ padding: 1px;
12
+ }
13
+
14
+ .c-font-weight-selector__select__icon {
15
+ padding-top: 0;
16
+ }
17
+
18
+ .c-font-weight-selector__dropdown {
19
+ background-color: #fff;
20
+ border: 1px solid @color-gray-x-light;
21
+ border-radius: 4px;
22
+ box-shadow: @dropdown-box-shadow;
23
+ display: none;
24
+ max-height: 300px;
25
+ position: absolute;
26
+ top: 36px;
27
+ width: 100px;
28
+ z-index: 2;
29
+
30
+ &.is-visible {
31
+ display: block;
32
+ }
33
+ }
34
+
35
+ .c-font-weight-selector__dropdown__matches {
36
+ max-height: 250px;
37
+ overflow-y: auto;
38
+ }
39
+
40
+ .c-font-weight-selector__match-row {
41
+ cursor: pointer;
42
+ padding: 5px 10px;
43
+
44
+ &.with-mouse:hover,
45
+ &.with-keyboard.is-selected {
46
+ background-color: @color-gray-xx-light;
47
+ }
48
+ }
@@ -0,0 +1,147 @@
1
+ import { Component, Inject, Input, Output } from '../decorators'
2
+ import WebfontLoaderService from '../font-selector/webfont-loader.service'
3
+ import { IComponentController } from 'angular'
4
+
5
+ @Component({
6
+ selector: 'mflyFontWeightSelector',
7
+ template: require('./font-weight-selector.html')
8
+ })
9
+ export default class FontWeightSelectorController implements IComponentController {
10
+
11
+ @Input() fontFamily?: string
12
+ @Input() fontWeight?: string
13
+ @Input() fontWeightOptions?: string[]
14
+
15
+ @Output('&?') onFontWeightSelect?: (param: { fontWeight: string }) => void
16
+
17
+ protected interactionMode: 'mouse' | 'keyboard' = 'keyboard'
18
+ protected isDropdownVisible: boolean = false
19
+ protected kbSelectedIndex = -1
20
+ protected keyEventElement: HTMLElement | null = null
21
+ private observer: MutationObserver
22
+
23
+ constructor (
24
+ @Inject('$document') private $document: ng.IDocumentService,
25
+ @Inject('$scope') private $scope: ng.IScope,
26
+ @Inject('$element') private $element: ng.IAugmentedJQuery,
27
+ @Inject('$timeout') private $timeout: ng.ITimeoutService,
28
+ @Inject('webfontLoaderService') private webfontLoaderService: WebfontLoaderService,
29
+ ) {}
30
+
31
+ $onDestroy() {
32
+ if (this.keyEventElement) {
33
+ this.keyEventElement.removeEventListener('keydown', this.onKeyDown)
34
+ }
35
+
36
+ const body = (this.$document[0] as Document).body
37
+ body.removeEventListener('click', this.onClickElsewhere)
38
+
39
+ this.observer.disconnect()
40
+ }
41
+
42
+ $onInit() {
43
+
44
+ const body = (this.$document[0] as Document).body
45
+ body.addEventListener('click', this.onClickElsewhere)
46
+
47
+ this.kbSelectedIndex = this.fontWeightOptions?.indexOf(this.fontWeight || '') || -1
48
+
49
+ // Load preview fonts when the font family or weight options change
50
+ this.$scope.$watchCollection(() => [this.fontFamily, this.fontWeightOptions], () => {
51
+ if (this.fontFamily && this.fontWeightOptions?.length) {
52
+ this.webfontLoaderService.loadFontVariants(this.fontFamily, this.fontWeightOptions)
53
+ }
54
+ })
55
+
56
+ this.observer = new MutationObserver(() => {
57
+ this.keyEventElement = this.$element.parent().find('.c-font-weight-selector__dropdown__matches')[0]
58
+ if (this.keyEventElement) {
59
+ this.keyEventElement.addEventListener('keydown', this.onKeyDown)
60
+ this.observer.disconnect()
61
+ }
62
+ })
63
+
64
+ this.observer.observe(body, { attributes: true, childList: true, characterData: false, subtree: true })
65
+ }
66
+
67
+ protected clickSelect($event): void {
68
+ this.isDropdownVisible = !this.isDropdownVisible
69
+ if (this.isDropdownVisible) {
70
+ this.$timeout(() => {
71
+ this.keyEventElement?.focus()
72
+ }, 250)
73
+ }
74
+
75
+ $event.stopPropagation()
76
+ }
77
+
78
+ // When the user mouses over the dropdown, we should remove any keyboard
79
+ // highlighting, since the mouse hover provides it too.
80
+ dropdownWasMousedOver() {
81
+ this.interactionMode = 'mouse'
82
+ this.kbSelectedIndex = -1
83
+ }
84
+
85
+ protected variantWasSelected(variant: string) {
86
+ if (!this.fontFamily) { throw new Error('Font family is unexpectedly undefined.') }
87
+
88
+ this.isDropdownVisible = false
89
+ this.onFontWeightSelect?.({ fontWeight: variant })
90
+ }
91
+
92
+ private onClickElsewhere = ($event: MouseEvent) => {
93
+
94
+ this.$timeout(() => {
95
+ this.isDropdownVisible = false
96
+ })
97
+ }
98
+
99
+ private onKeyDown = ($event: KeyboardEvent) => this.$timeout(() => {
100
+
101
+ if (!this.fontFamily || !this.fontWeightOptions?.length) { return }
102
+
103
+ this.interactionMode = 'keyboard'
104
+
105
+ // Enter with no selection: let the $event through
106
+ if ($event.key === 'Enter' && this.kbSelectedIndex === -1) { return }
107
+
108
+ // If the dropdown is closed, all we can do is arrow down to open the thing
109
+ if (this.fontWeightOptions.length === 0 && $event.key !== 'ArrowDown') { return }
110
+
111
+ // Escape: close the dropdown
112
+ if ($event.key === 'Escape') {
113
+ this.isDropdownVisible = false
114
+ }
115
+
116
+ // Arrow keys with dropdown open: move the selection highlight
117
+ if ($event.key === 'ArrowDown') {
118
+ const idx = Math.min(this.kbSelectedIndex + 1, this.fontWeightOptions.length - 1)
119
+ this.kbSelectedIndex = idx
120
+ this.scrollToSelection(idx)
121
+ }
122
+
123
+ if ($event.key === 'ArrowUp') {
124
+ const idx = Math.max(this.kbSelectedIndex - 1, 0)
125
+ this.kbSelectedIndex = idx
126
+ this.scrollToSelection(idx)
127
+ }
128
+
129
+ // Enter with a selection: set the selection to the currently highlighted match
130
+ if ($event.key === 'Enter' && this.kbSelectedIndex > -1) {
131
+ const selectedFontWeight = this.fontWeightOptions[this.kbSelectedIndex]
132
+ this.onFontWeightSelect?.({ fontWeight: selectedFontWeight })
133
+ this.isDropdownVisible = false
134
+ $event.preventDefault()
135
+ }
136
+
137
+ $event.stopPropagation()
138
+ })
139
+
140
+ private scrollToSelection(index: number) {
141
+
142
+ const row = this.$element.find('.c-font-weight-selector__match-row')[index] as HTMLDivElement
143
+ row?.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
144
+
145
+ }
146
+
147
+ }
@@ -0,0 +1 @@
1
+ import './font-weight-selector'
@@ -10,6 +10,7 @@
10
10
  @import './feedback/feedback';
11
11
  @import './first-time-motivator/first-time-motivator';
12
12
  @import './font-selector/font-selector';
13
+ @import './font-weight-selector/font-weight-selector';
13
14
  @import './footer/footer';
14
15
  @import './hierarchy/hierarchy';
15
16
  @import './hover-tooltip/hover-tooltip';
@@ -14,6 +14,7 @@ import './dropdown'
14
14
  import './feedback'
15
15
  import './first-time-motivator'
16
16
  import './font-selector'
17
+ import './font-weight-selector'
17
18
  import './footer'
18
19
  import './form-validation-utils'
19
20
  import './full-screen'
@@ -4,7 +4,7 @@
4
4
  <mfly:font-selector
5
5
  input-id="$ctrl.fontSelectorId"
6
6
  font-family="$ctrl.selectedFont"
7
- on-font-select="$ctrl.fontSelected(fontFamily)">
7
+ on-font-select="$ctrl.fontSelected(fontFamily, fontWeightOptions)">
8
8
  </mfly:font-selector>
9
9
  </span>
10
10
 
@@ -39,7 +39,14 @@
39
39
  </span>
40
40
 
41
41
  <span class="ql-formats">
42
- <button class="ql-bold"></button>
42
+ <button class="ql-bold" ng-show="!$ctrl.selectedFontAvailableWeights || $ctrl.selectedFontAvailableWeights.length <= 1 || !$ctrl.allowFontSelection"></button>
43
+ <mfly:font-weight-selector
44
+ ng-if="$ctrl.selectedFontAvailableWeights && $ctrl.selectedFontAvailableWeights.length > 1 && $ctrl.allowFontSelection"
45
+ font-family="$ctrl.selectedFont"
46
+ font-weight="$ctrl.selectedFontWeight"
47
+ font-weight-options="$ctrl.selectedFontAvailableWeights"
48
+ on-font-weight-select="$ctrl.fontWeightSelected(fontWeight)"
49
+ ></mfly:font-weight-selector>
43
50
  <button class="ql-italic"></button>
44
51
  <button class="ql-underline"></button>
45
52
  </span>
@@ -144,6 +144,7 @@ rich-text {
144
144
  .ql-bg-\#3d1466 { background-color: #3d1466; }
145
145
 
146
146
  // Size option overrides
147
+ .ql-size { max-width: 60px; }
147
148
  .ql-size .ql-picker-item:nth-child(1) { font-size: 6px; }
148
149
  .ql-size .ql-picker-item:nth-child(2) { font-size: 7px; }
149
150
  .ql-size .ql-picker-item:nth-child(3) { font-size: 8px; }
@@ -7,6 +7,8 @@ import type { TranslateFactory } from '../translate/translate-factory'
7
7
  import { v4 as uuidv4 } from 'uuid'
8
8
  import RichTextService from './rich-text.service'
9
9
  import { IComponentController } from 'angular'
10
+ import { StyleAttributor, Scope } from 'parchment'
11
+ import GoogleWebfontsApiService from '../font-selector/google-webfonts-api.service'
10
12
 
11
13
  @Component({
12
14
  selector: 'richText',
@@ -29,6 +31,8 @@ export default class RichTextComponentController implements IComponentController
29
31
  protected requiredText: string
30
32
  protected selectedColor: string
31
33
  protected selectedFont: string
34
+ protected selectedFontAvailableWeights: number[]
35
+ protected selectedFontWeight: number
32
36
  protected toolbarId: string
33
37
  protected validationForm: ng.IFormController
34
38
 
@@ -41,6 +45,7 @@ export default class RichTextComponentController implements IComponentController
41
45
  private DEFAULT_COLOR = '#535353'
42
46
  private DEFAULT_FONT = 'Roboto'
43
47
  private DEFAULT_SIZE = '14px'
48
+ private DEFAULT_WEIGHT = 400
44
49
 
45
50
  private HEADING_1_COLOR = '#535353'
46
51
  private HEADING_1_FONT = 'Poppins'
@@ -54,6 +59,7 @@ export default class RichTextComponentController implements IComponentController
54
59
  @Inject('$element') private $element: ng.IAugmentedJQuery,
55
60
  @Inject('$rootScope') private $rootScope: JSUIRootScope,
56
61
  @Inject('$timeout') private $timeout: ng.ITimeoutService,
62
+ @Inject('googleWebfontsApiService') private googleWebfontsApiService: GoogleWebfontsApiService,
57
63
  @Inject('richTextService') private richTextService: RichTextService,
58
64
  @Inject('translateFactory') private translateFactory: TranslateFactory,
59
65
  ) { }
@@ -82,6 +88,13 @@ export default class RichTextComponentController implements IComponentController
82
88
  this.fontSelectorId = 'rteFontSelector-' + uuidv4()
83
89
  this.toolbarId = 'rteToolbar-' + uuidv4()
84
90
 
91
+ // Populate the available font weights for the default font
92
+ if (this.allowFontSelection) {
93
+ this.googleWebfontsApiService.getFontVariants(this.DEFAULT_FONT)
94
+ .then(variants => this.googleWebfontsApiService.sortFontWeightVariants(variants))
95
+ .then(weights => this.selectedFontAvailableWeights = weights)
96
+ }
97
+
85
98
  this.$timeout(() => {
86
99
  this.colorPicker = document.getElementById(this.colorInputId)
87
100
  this.colorPicker?.addEventListener('change', this.colorChanged.bind(this))
@@ -97,6 +110,7 @@ export default class RichTextComponentController implements IComponentController
97
110
 
98
111
  Quill.register(Quill.import('attributors/class/background') as Parchment.Attributor, true)
99
112
  Quill.register(Quill.import('attributors/style/color') as Parchment.Attributor, true)
113
+ Quill.register(new StyleAttributor('font-weight', 'font-weight', { scope: Scope.INLINE }), true)
100
114
 
101
115
  const fontStyle = Quill.import('attributors/style/font') as Parchment.Attributor
102
116
  // Setting the whitelist to undefined allows all fonts. (We don't know what fonts Google Fonts will give us.)
@@ -153,16 +167,27 @@ export default class RichTextComponentController implements IComponentController
153
167
  this.cachedSelection = this.editor.getSelection()
154
168
  }
155
169
 
156
- protected fontSelected(fontFamily: string): void {
170
+ protected fontSelected(fontFamily: string, fontWeightOptions?: number[]): void {
157
171
 
158
172
  if (!fontFamily) { return }
159
173
 
160
174
  this.$rootScope.$broadcast('richTextToolbarContainer:closeContainer')
161
175
  this.editor.format('font', `"${fontFamily}"`)
162
176
 
177
+ this.selectedFontAvailableWeights = fontWeightOptions || []
178
+ if (!fontWeightOptions?.includes(this.selectedFontWeight)) {
179
+ this.selectedFontWeight = this.DEFAULT_WEIGHT
180
+ this.editor.format('font-weight', undefined)
181
+ }
182
+
163
183
  this.debouncedSanitizeAndValidate()
164
184
  }
165
185
 
186
+ protected fontWeightSelected(fontWeight: number): void {
187
+ this.selectedFontWeight = fontWeight
188
+ this.editor.format('font-weight', fontWeight === this.DEFAULT_WEIGHT ? undefined : fontWeight)
189
+ }
190
+
166
191
  /** The user entered/deleted/modified some text. Handle any formatting issues, then store the model. */
167
192
  protected onTextChange(delta: Delta, oldContents: Delta, source: EmitterSource): void {
168
193
 
@@ -236,6 +261,7 @@ export default class RichTextComponentController implements IComponentController
236
261
  this.updateSizePicker(this.DEFAULT_SIZE)
237
262
  this.editor.format('color', this.DEFAULT_COLOR)
238
263
  this.updateColorPicker(this.DEFAULT_COLOR)
264
+ this.editor.format('font-weight', undefined)
239
265
  }
240
266
  }
241
267
 
@@ -367,7 +393,7 @@ export default class RichTextComponentController implements IComponentController
367
393
  }
368
394
 
369
395
  const format = this.editor.getFormat()
370
- if (format.font && format.size && format.color) {
396
+ if (format.font && format.size && format.color && format['font-weight']) {
371
397
  // We are fully formatted; nothing more to do
372
398
  return
373
399
  }
@@ -406,6 +432,11 @@ export default class RichTextComponentController implements IComponentController
406
432
  this.editor.format('color', color)
407
433
  this.updateColorPicker(color)
408
434
  }
435
+
436
+ if (!format['font-weight']) {
437
+ const fontWeight = this.getFontWeightFromFormat(nearbyFormat)
438
+ this.editor.format('font-weight', fontWeight === this.DEFAULT_WEIGHT ? undefined : fontWeight)
439
+ }
409
440
  }
410
441
 
411
442
  private getColorFromFormat(format: QuillFormat): string {
@@ -450,6 +481,15 @@ export default class RichTextComponentController implements IComponentController
450
481
  return this.DEFAULT_FONT
451
482
  }
452
483
 
484
+ private getFontWeightFromFormat(format: QuillFormat): number {
485
+
486
+ if (typeof format['font-weight'] === 'string') {
487
+ return parseInt(format['font-weight'], 10)
488
+ }
489
+
490
+ return this.DEFAULT_WEIGHT
491
+ }
492
+
453
493
  /**
454
494
  * Scans the entire text and returns an array of objects denoting which font is used for each range.
455
495
  */
@@ -534,7 +574,7 @@ export default class RichTextComponentController implements IComponentController
534
574
  }
535
575
 
536
576
  // Fall back to the default (should never get here)
537
- return this.DEFAULT_FONT
577
+ return this.DEFAULT_SIZE
538
578
  }
539
579
 
540
580
  /**
@@ -599,6 +639,12 @@ export default class RichTextComponentController implements IComponentController
599
639
  private updateFontPicker(font: string): void {
600
640
 
601
641
  this.selectedFont = font
642
+
643
+ this.googleWebfontsApiService.getFontVariants(font)
644
+ .then(variants => this.googleWebfontsApiService.sortFontWeightVariants(variants))
645
+ .then(weights => {
646
+ this.selectedFontAvailableWeights = weights
647
+ })
602
648
  }
603
649
 
604
650
  /** Updates the size shown in the toolbar. Does not change the size being used in the text. */