Skip to content

Commit 3459800

Browse files
authored
Merge pull request #55 from blocknotes/api/quill-editor-query-functions
API: Quill editor query functions
2 parents 328ee0d + af901f4 commit 3459800

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ Consider that this is just a basic example: images are uploaded as soon as they
105105
the *upload_admin_post_path*) and it doesn't provide a way to remove images (just deleting them from
106106
the editor will not destroy them, you'll need to implement a purge logic for that).
107107

108+
## Javascript API
109+
110+
Some methods are provided for advanced use cases:
111+
112+
- `window.getQuillEditors()`: returns all the available Quill editors instances;
113+
- `window.getQuillEditorByIndex(n)`: returns the N-th Quill editor instance;
114+
- `window.getQuillEditorByElementId(id)`: returns the Quill editor instance related to the specified element id (e.g. _article_description_).
115+
108116
## Development
109117

110118
Project created by [Mattia Roccoberton](http://blocknot.es), thanks also to the good guys that opened issues and pull requests from time to time.

app/assets/javascripts/activeadmin/quill_editor_input.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-undef */
12
(function () {
23
'use strict'
34

@@ -87,6 +88,27 @@
8788
}
8889
}
8990

91+
// --- public functions --------------------------------------------------------
92+
window.getQuillEditors = function() {
93+
const editors = document.querySelectorAll('[data-aa-quill-editor]')
94+
let list = []
95+
96+
editors.forEach(function(editor) { list.push(editor['_quill-editor']) })
97+
return list
98+
}
99+
100+
window.getQuillEditorByIndex = function(index) {
101+
const editors = document.querySelectorAll('[data-aa-quill-editor]')
102+
103+
return (index >= 0 && index < editors.length) ? editors[index]['_quill-editor'] : null
104+
}
105+
106+
window.getQuillEditorByElementId = function(id) {
107+
const editor = document.querySelector(`[data-aa-quill-editor]#${id}`)
108+
109+
return editor ? editor['_quill-editor'] : null
110+
}
111+
90112
// --- events ------------------------------------------------------------------
91113
$(document).ready(initQuillEditors)
92114
$(document).on('has_many_add:after', '.has_many_container', initQuillEditors)

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@
99
"files": [
1010
"app/**/*",
1111
"index.js"
12-
]
12+
],
13+
"devDependencies": {
14+
"@eslint/js": "^9.22.0",
15+
"eslint": "^9.22.0",
16+
"globals": "^16.0.0"
17+
}
1318
}

spec/system/quill_js_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,59 @@
77
expect(page.evaluate_script('typeof Quill')).to eq 'function'
88
expect(page.evaluate_script('Quill.version')).to eq(ActiveAdmin::QuillEditor::QUILL_VERSION)
99
end
10+
11+
describe '.getQuillEditors' do
12+
let(:author) { Author.create!(email: '[email protected]', name: 'John Doe', age: 30) }
13+
let!(:post) { Post.create!(title: 'Test', author: author, description: '') }
14+
15+
before do
16+
path = edit_admin_post_path(post)
17+
Admin::Posts::EditPage.new(path: path).load
18+
end
19+
20+
it "returns the available editors", :aggregate_failures do
21+
editors_count = page.evaluate_script('window.getQuillEditors().length')
22+
expect(editors_count).to eq 2
23+
24+
expected_element = find('#post_summary > .ql-container')
25+
first_editor = page.evaluate_script('window.getQuillEditors()[0].container')
26+
expect(first_editor).to eq expected_element
27+
28+
expected_element = find('#post_description > .ql-container')
29+
first_editor = page.evaluate_script('window.getQuillEditors()[1].container')
30+
expect(first_editor).to eq expected_element
31+
end
32+
end
33+
34+
describe '.getQuillEditorByIndex' do
35+
let(:author) { Author.create!(email: '[email protected]', name: 'John Doe', age: 30) }
36+
let!(:post) { Post.create!(title: 'Test', author: author, description: '') }
37+
38+
before do
39+
path = edit_admin_post_path(post)
40+
Admin::Posts::EditPage.new(path: path).load
41+
end
42+
43+
it "returns the expected editor instance" do
44+
expected_element = find('#post_description > .ql-container')
45+
editor = page.evaluate_script('window.getQuillEditorByIndex(1).container')
46+
expect(editor).to eq expected_element
47+
end
48+
end
49+
50+
describe '.getQuillEditorByElementId' do
51+
let(:author) { Author.create!(email: '[email protected]', name: 'John Doe', age: 30) }
52+
let!(:post) { Post.create!(title: 'Test', author: author, description: '') }
53+
54+
before do
55+
path = edit_admin_post_path(post)
56+
Admin::Posts::EditPage.new(path: path).load
57+
end
58+
59+
it "returns the expected editor instance" do
60+
expected_element = find('#post_description > .ql-container')
61+
editor = page.evaluate_script('window.getQuillEditorByElementId("post_description").container')
62+
expect(editor).to eq expected_element
63+
end
64+
end
1065
end

0 commit comments

Comments
 (0)