diff --git a/src/shortcodeList.js b/src/shortcodeList.js
index a6e43c6..5ab0d7a 100644
--- a/src/shortcodeList.js
+++ b/src/shortcodeList.js
@@ -27,6 +27,11 @@ tinymce.PluginManager.add('shortcodeList', function (editor, url) {
code: '[index]',
description: 'Insert an index to the article'
},
+ {
+ title: 'Trivia',
+ code: '[trivia id="1"]',
+ description: 'Insert a trivia'
+ },
{
title: 'Widgetbay',
code: '[widgetbay id="optional" link="optional"]',
diff --git a/src/trivia.js b/src/trivia.js
new file mode 100644
index 0000000..fceba4b
--- /dev/null
+++ b/src/trivia.js
@@ -0,0 +1,177 @@
+/* global tinymce */
+
+tinymce.PluginManager.add('trivia', function (editor, url) {
+ const content = `
+
+
+
+
+ `
+
+ const customStyles = `
+
+`
+
+ editor.ui.registry.addButton('trivia', {
+ icon: 'trivia',
+ tooltip: 'Add trivia',
+ onAction: function () {
+ tinymce.activeEditor.windowManager.open({
+ title: 'Trivia',
+ body: {
+ type: 'panel',
+ items: [
+ {
+ type: 'htmlpanel',
+ html: customStyles + content
+ }
+ ]
+ }
+ })
+ // Get last trivia
+ lastTrivia()
+
+ // Search Image
+ searchTrivia()
+
+ // Insert into editor
+ insetDataTrivia(editor)
+ }
+ })
+
+ /* Add a icon */
+ editor.ui.registry.addIcon('trivia', '')
+ return {
+ getMetadata: function () {
+ return {
+ name: 'trivia Shortcode',
+ url: 'https://github.com/The-3Labs-Team/tinymce-laravel-shortcode-plus'
+ }
+ }
+ }
+})
+
+// === FUNCTIONS === //
+
+async function lastTrivia() {
+ const lastTrivia = await getTrivia()
+ printTrivia(lastTrivia)
+}
+
+async function getTrivia(query = '') {
+ try {
+ let url = '/trivia/search'
+ if (query !== '') {
+ url = `/trivia/search?query=${query}`
+ }
+ const response = await fetch(url, {
+ method: 'GET',
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ });
+
+ if (!response.ok) {
+ throw new Error('Error in Nova API Request');
+ }
+
+ const lastTrivia = await response.json();
+ return lastTrivia.data;
+ } catch (error) {
+ console.error(error);
+ return { success: false, data: error.message };
+ }
+}
+
+function printTrivia(trivias) {
+ const lastTriviaContainer = document.querySelector('#last-trivia-container')
+
+ // Reset container
+ lastTriviaContainer.innerHTML = ''
+
+ if(trivias.length > 0) {
+ trivias.forEach((trivia, index) => {
+ lastTriviaContainer.innerHTML += `
+
+
+
+
+ ${trivia.topic}
+ |
+
+ `
+ })
+ } else {
+ lastTriviaContainer.innerHTML = 'No trivia found'
+ }
+}
+
+function searchTrivia(query) {
+ const formQuery = document.querySelector('.tox-dialog__content-js form#queryTrivia')
+ const container = document.querySelector('#last-trivia-container')
+
+ formQuery.addEventListener('submit', async function (e) {
+ e.preventDefault()
+ container.innerHTML = 'Loading trivia...'
+
+ // Get query from Form
+ const formData = new FormData(formQuery)
+ const query = formData.get('query')
+
+ // Search in Nova
+ const trivia = query.length > 0 ? await getTrivia(query) : []
+
+ printTrivia(trivia)
+ })
+
+}
+
+function insetDataTrivia(editor) {
+ const formTrivia = document.querySelector('.tox-dialog__content-js form#trivia-data')
+
+ formTrivia.addEventListener('submit', function (e) {
+ e.preventDefault()
+
+ const formData = new FormData(formTrivia)
+ const id = formData.get('trivia')
+
+ const result = `[trivia id="${id}"]`
+ editor.insertContent(result)
+ tinymce.activeEditor.windowManager.close()
+ })
+}