@pareto-engineering/design-system 4.0.0-alpha.42 → 4.0.0-alpha.43

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.
@@ -1,7 +1,12 @@
1
1
  /* eslint-disable import/no-extraneous-dependencies -- required here */
2
2
  import * as React from 'react'
3
3
 
4
- import { useEffect, useState, useCallback } from 'react'
4
+ import {
5
+ useEffect,
6
+ useState,
7
+ useCallback,
8
+ useRef,
9
+ } from 'react'
5
10
 
6
11
  import {
7
12
  $getSelection,
@@ -10,6 +15,8 @@ import {
10
15
  FORMAT_ELEMENT_COMMAND,
11
16
  UNDO_COMMAND,
12
17
  REDO_COMMAND,
18
+ COMMAND_PRIORITY_NORMAL,
19
+ createCommand,
13
20
  } from 'lexical'
14
21
  import {
15
22
  INSERT_ORDERED_LIST_COMMAND,
@@ -27,6 +34,9 @@ import {
27
34
  } from '@lexical/link'
28
35
  import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
29
36
  import { mergeRegister, $getNearestNodeOfType } from '@lexical/utils'
37
+
38
+ import { Popover } from 'ui'
39
+
30
40
  import styleNames from '@pareto-engineering/bem/exports'
31
41
 
32
42
  const baseClassName = styleNames.base
@@ -47,6 +57,19 @@ const getSelectedNode = (selection) => {
47
57
  return $isAtNodeEnd(anchor) ? focusNode : anchorNode
48
58
  }
49
59
 
60
+ const defaultColor = 'var(--paragraph)'
61
+
62
+ const colorOptions = [
63
+ 'red',
64
+ 'blue',
65
+ 'green',
66
+ 'yellow',
67
+ 'orange',
68
+ 'purple',
69
+ 'pink',
70
+ 'brown',
71
+ ]
72
+
50
73
  const Toolbar = () => {
51
74
  const [editor] = useLexicalComposerContext()
52
75
  const [isBold, setIsBold] = useState(false)
@@ -55,6 +78,9 @@ const Toolbar = () => {
55
78
  const [blockType, setBlockType] = useState('paragraph')
56
79
  const [isLink, setIsLink] = useState(false)
57
80
  const [isUnderline, setIsUnderline] = useState(false)
81
+ const [color, setColor] = useState(defaultColor)
82
+
83
+ const colorMenuRef = useRef(false)
58
84
 
59
85
  const formatBulletList = () => {
60
86
  if (blockType !== 'ul') {
@@ -99,6 +125,18 @@ const Toolbar = () => {
99
125
  setBlockType(element)
100
126
  }
101
127
 
128
+ // Check nodes for color
129
+ const nodes = selection.getNodes().filter((node) => node.getType() === 'text')
130
+ nodes.forEach((node) => {
131
+ const style = node.getStyle()
132
+ const colorProperty = style.match(/color: ([^;]+)/)
133
+ if (colorProperty) {
134
+ setColor(colorProperty[1])
135
+ return
136
+ }
137
+ setColor(false)
138
+ })
139
+
102
140
  // Check selection text styles
103
141
  setIsBold(selection.hasFormat('bold'))
104
142
  setIsItalic(selection.hasFormat('italic'))
@@ -117,6 +155,24 @@ const Toolbar = () => {
117
155
  }
118
156
  }, [editor])
119
157
 
158
+ const UPDATE_COLOR_COMMAND = createCommand()
159
+
160
+ editor.registerCommand(UPDATE_COLOR_COMMAND, (payload) => {
161
+ const selection = $getSelection()
162
+ const nodes = selection?.extract().filter((node) => node.getType() === 'text')
163
+ nodes?.forEach((node) => {
164
+ const style = node.getStyle()
165
+ const colorProperty = style?.match(/color: ([^;]+)/)
166
+ if (colorProperty && color !== payload) {
167
+ node.setStyle(style.replace(colorProperty[0], `color: ${payload}`))
168
+ } else if (colorProperty) {
169
+ node.setStyle(`color: ${defaultColor}`)
170
+ } else {
171
+ node.setStyle(`color: ${payload}`)
172
+ }
173
+ })
174
+ }, COMMAND_PRIORITY_NORMAL)
175
+
120
176
  useEffect(() => mergeRegister(
121
177
  editor.registerUpdateListener(({ editorState }) => {
122
178
  editorState.read(() => {
@@ -125,6 +181,11 @@ const Toolbar = () => {
125
181
  }),
126
182
  ), [updateToolbar, editor])
127
183
 
184
+ const dispatchUpdateColor = useCallback((e, payload) => {
185
+ e.stopPropagation()
186
+ editor.dispatchCommand(UPDATE_COLOR_COMMAND, payload)
187
+ }, [editor])
188
+
128
189
  return (
129
190
  <div className={`${baseClassName} ${componentClassName}`}>
130
191
  <div className="group">
@@ -164,6 +225,47 @@ const Toolbar = () => {
164
225
  ?
165
226
  </span>
166
227
  </button>
228
+ <button
229
+ type="button"
230
+ className={color && color !== defaultColor ? 'active color-menu-button' : 'color-menu-button'}
231
+ onClick={() => editor.dispatchCommand(UPDATE_COLOR_COMMAND,
232
+ color !== defaultColor ? defaultColor : color)}
233
+ ref={colorMenuRef}
234
+ style={{ position: 'relative' }}
235
+ >
236
+ <span
237
+ className="icon"
238
+ style={{
239
+ color,
240
+ }}
241
+ >
242
+ Q
243
+ </span>
244
+ <Popover
245
+ parentRef={colorMenuRef}
246
+ >
247
+ <div className="color-menu">
248
+ {
249
+ colorOptions.map((option) => (
250
+ <span
251
+ role="button"
252
+ className="icon color-option"
253
+ style={{
254
+ color:option,
255
+ }}
256
+ onClick={(e) => dispatchUpdateColor(e, option)}
257
+ onKeyDown={(e) => dispatchUpdateColor(e, option)}
258
+ tabIndex={0}
259
+ key={option}
260
+ >
261
+ o
262
+ </span>
263
+ ))
264
+ }
265
+ </div>
266
+ </Popover>
267
+ </button>
268
+
167
269
  <button
168
270
  type="button"
169
271
  className={isLink ? 'active' : undefined}
@@ -15,6 +15,7 @@ $active-background: var(--hard-background-inputs);
15
15
  $default-background: var(--background-inputs);
16
16
  $default-icon-color: var(--on-background-inputs);
17
17
  $disabled-background: var(--background-inputs-30);
18
+ $default-color-menu-padding: .5em .25em;
18
19
 
19
20
  .#{bem.$base}.editor-input {
20
21
  &.#{bem.$base}.input-wrapper {
@@ -62,6 +63,31 @@ $disabled-background: var(--background-inputs-30);
62
63
  }
63
64
  }
64
65
 
66
+ .color-menu-button {
67
+ &:hover {
68
+ > .#{bem.$base}.popover {
69
+ display: block;
70
+ }
71
+ }
72
+
73
+ > .#{bem.$base}.popover {
74
+ padding: $default-color-menu-padding;
75
+
76
+ .color-menu {
77
+ display: flex;
78
+ flex-wrap: wrap;
79
+ gap: calc($default-gap / 2);
80
+ justify-content: center;
81
+ max-width: 10em;
82
+ min-width: 5em;
83
+ }
84
+
85
+ .color-option:hover {
86
+ opacity: .5;
87
+ }
88
+ }
89
+ }
90
+
65
91
  > .content-editable {
66
92
  background: $default-background;
67
93
  border: $default-border;