turbo_boost-elements 0.0.3 → 0.0.4

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,4 +1,5 @@
1
1
  import TurboBoostElement from './turbo_boost_element'
2
+ import './toggle_target_focus'
2
3
 
3
4
  export default class ToggleTargetElement extends TurboBoostElement {
4
5
  connectedCallback () {
@@ -55,9 +56,7 @@ export default class ToggleTargetElement extends TurboBoostElement {
55
56
  focus () {
56
57
  clearTimeout(this.focusTimeout)
57
58
  this.focusTimeout = setTimeout(() => {
58
- if (!this.focusElement) return
59
- this.focusElement.focus()
60
- this.focusElement.scrollIntoView({ block: 'center', behavior: 'smooth' })
59
+ if (this.focusElement) this.focusElement.focus()
61
60
  }, 50)
62
61
  }
63
62
 
@@ -0,0 +1,77 @@
1
+ function deactivateTrixAttributes (editor) {
2
+ const attributes = [
3
+ 'bold',
4
+ 'bullet',
5
+ 'code',
6
+ 'heading1',
7
+ 'href',
8
+ 'italic',
9
+ 'number',
10
+ 'quote',
11
+ 'strike'
12
+ ]
13
+ attributes.forEach(name => editor.deactivateAttribute(name))
14
+ }
15
+
16
+ function focusTrixEditorElement (element) {
17
+ if (element.value.length === 0) return
18
+
19
+ const editor = element.editor
20
+
21
+ // move cursor to the end
22
+ let lastRange = []
23
+ while (
24
+ lastRange[0] !== editor.getSelectedRange()[0] &&
25
+ lastRange[1] !== editor.getSelectedRange()[1]
26
+ ) {
27
+ lastRange = editor.getSelectedRange()
28
+ editor.moveCursorInDirection('forward')
29
+ }
30
+
31
+ // insert an empty char and select it
32
+ editor.insertString(' ')
33
+ editor.moveCursorInDirection('forward')
34
+ editor.setSelectedRange([lastRange[1], editor.getSelectedRange()[1]])
35
+
36
+ // deactivate all trix features for the selection
37
+ deactivateTrixAttributes(editor)
38
+
39
+ // move cursor to end and collapse the selection
40
+ editor.setSelectedRange([
41
+ editor.getSelectedRange()[1],
42
+ editor.getSelectedRange()[1]
43
+ ])
44
+ }
45
+
46
+ function shouldEnhanceFocus (element) {
47
+ return (
48
+ element.closest('turbo-boost-toggle-target') &&
49
+ element.tagName.match(/^input|textarea|trix-editor$/i)
50
+ )
51
+ }
52
+
53
+ function enhanceFocus (element) {
54
+ const trixEditorElement = element.closest('trix-editor')
55
+
56
+ try {
57
+ if (trixEditorElement) {
58
+ focusTrixEditorElement(trixEditorElement)
59
+ } else {
60
+ element.selectionStart = element.selectionEnd = element.value.length
61
+ }
62
+ } finally {
63
+ setTimeout(
64
+ () => element.scrollIntoView({ block: 'center', behavior: 'smooth' }),
65
+ 100
66
+ )
67
+ }
68
+ }
69
+
70
+ addEventListener(
71
+ 'focus',
72
+ event => {
73
+ if (shouldEnhanceFocus(document.activeElement))
74
+ enhanceFocus(document.activeElement)
75
+ },
76
+ true
77
+ )
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TurboBoost
4
4
  module Elements
5
- VERSION = "0.0.3"
5
+ VERSION = "0.0.4"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbo_boost-elements
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Hopkins (hopsoft)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-06 00:00:00.000000000 Z
11
+ date: 2023-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -146,6 +146,7 @@ files:
146
146
  - app/javascript/devtools/toggle.js
147
147
  - app/javascript/elements/index.js
148
148
  - app/javascript/elements/toggle_target_element.js
149
+ - app/javascript/elements/toggle_target_focus.js
149
150
  - app/javascript/elements/toggle_trigger_element.js
150
151
  - app/javascript/elements/turbo_boost_element.js
151
152
  - app/javascript/index.js