athena-ui-js 1.0.0 → 1.1.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.
package/README.md CHANGED
@@ -5,7 +5,7 @@ layout (header, menu, content wrapper, footer) from a single `init()` call, load
5
5
  and theme, and ships the building blocks of a multipurpose application: forms, tables, charts,
6
6
  feedback, navigation, a router and more.
7
7
 
8
- Current version: **1.0.0** (first stable release)
8
+ Current version: **1.1.0**
9
9
 
10
10
  [Quick start](#usage) · [API](#api) · [Components](#charts) · [Themes](#themes) · [Tests](#tests) · [Release notes](#releases)
11
11
 
@@ -31,7 +31,7 @@ Known limits are listed below, so you can judge whether it fits your project:
31
31
  - Buttons, toasts, alerts, spinner, progress bar, alert / confirm / prompt dialogs
32
32
  - Tabs, accordion, dropdown, sidebar, breadcrumbs, pagination and a hash / history router
33
33
  - Cards, grid, badges, avatars, KPI tiles, tooltips, links, sections
34
- - File upload with progress, stepper (wizard), date range picker, rich text editor
34
+ - File upload with progress, drag-and-drop area, stepper (wizard), date range picker, rich text and WYSIWYG editor
35
35
  - Menus with submenus
36
36
  - Session handling (login state in a cookie), change password form
37
37
  - Default and dark theme (switchable at runtime), responsive, translations (English, Hungarian), keyboard and ARIA support
@@ -120,7 +120,7 @@ the theme are loaded and the layout is generated.
120
120
  | `header_title_text` | `"Athena UI"` | Title in the header |
121
121
  | `header_img_url` | | Header logo URL |
122
122
  | `copyright` | placeholder text | Footer text, the version is appended |
123
- | `version` | `"1.0.0"` | Version shown in the footer |
123
+ | `version` | `"1.1.0"` | Version shown in the footer |
124
124
  | `bordered` | `true` | Draw a border around the layout blocks |
125
125
  | `generateHeader`, `generateFooter` | `true` | Generate the header / footer block |
126
126
  | `generateHeadercontent`, `generateFooterContent`, `generateWrappercontent` | `true` | Generate the content of the block |
@@ -159,7 +159,7 @@ Everything is exported from `athena-ui.js`:
159
159
  | `generateTabs`, `generateBreadcrumbs`, `generatePagination`, `generateAccordion`, `generateDropdown`, `generateSidebar` | See [Navigation](#navigation) |
160
160
  | `createRouter(options)` | See [Router](#router) |
161
161
  | `generateCard`, `generateSection`, `generateGrid`, `generateBadge`, `generateAvatar`, `generateStat`, `generateLink`, `generateTooltip` | See [Components](#components) |
162
- | `generateAutocomplete`, `generateMultiSelect`, `generateFileUpload`, `generateStepper`, `generateDateRange`, `generateRichText` | See [Inputs](#inputs) |
162
+ | `generateAutocomplete`, `generateMultiSelect`, `generateFileUpload`, `generateDropZone`, `generateStepper`, `generateDateRange`, `generateRichText`, `generateWysiwyg` | See [Inputs](#inputs) |
163
163
  | `sanitizeHtml(html)`, `htmlToText(html)`, `isSafeUrl(url)` | The allow-list sanitizer behind the rich text editor |
164
164
  | `setSession`, `getSession`, `isLoggedIn`, `logout`, `onSessionChange` | See [Session](#session) |
165
165
  | `translate`, `setLocale`, `getLocale`, `addMessages` | See [Translations](#translations) |
@@ -234,7 +234,7 @@ const form = generateForm({
234
234
 
235
235
  Field types: `text`, `password`, `email`, `number`, `date`, `datetime-local`, `time`, `month`, `week`, `url`, `tel`,
236
236
  `search`, `color`, `range`, `file`, `hidden`, `textarea`, `select` (`multiple`), `checkbox`, `switch`, `radio`,
237
- `checkboxes`, `autocomplete`, `multiselect`, `daterange` (value `{ from, to }`), `richtext` (value: safe HTML).
237
+ `checkboxes`, `autocomplete`, `multiselect`, `daterange` (value `{ from, to }`), `richtext` and `wysiwyg` (value: safe HTML).
238
238
  The date, time and color types use the native pickers of the browser.
239
239
 
240
240
  Rules (`required`, `email`, `minLength`, `maxLength`, `pattern`, `min`, `max`, `match`, `validate(value, values)`) can be
@@ -366,6 +366,17 @@ strike, lists, quotes, code, links). Everything else (scripts, styles, iframes,
366
366
  `quote`, `ul`, `ol`, `link`, `unlink`, `clear`, `undo`, `redo`. The editor is built on `contenteditable` and
367
367
  `document.execCommand`, which every browser still supports; sanitize on the server as well, as with any HTML you accept.
368
368
 
369
+ ```js
370
+ generateDropZone({ parent: "wrapper", accept: "image/*", maxSize: 2 * 1024 * 1024, maxFiles: 3,
371
+ onDrop: files => {}, onReject: rejected => rejected.forEach(({ file, reason }) => {}) });
372
+ const wysiwyg = generateWysiwyg({ parent: "wrapper", label: "Body", value: "<p>Hello</p>" }); // rich text + HTML source view
373
+ ```
374
+
375
+ `generateDropZone` is a bare drag-and-drop area (also reachable with Enter / Space and the file dialog). Dropped files are
376
+ checked against `accept`, `maxSize` and `maxFiles`; valid ones go to `onDrop`, the others to `onReject` with a reason. It
377
+ reads and sends nothing, and `setDisabled(true)` turns it off. `generateWysiwyg` is the rich text editor plus a `source`
378
+ toolbar button that shows the sanitized HTML; what is typed there is sanitized again when you switch back.
379
+
369
380
  Files are sent as a multipart POST (`fieldName`, `headers` are configurable). Without `url` the files are only collected,
370
381
  read them with `getFiles()`.
371
382
 
@@ -470,6 +481,13 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
470
481
 
471
482
  ## Releases
472
483
 
484
+ ### 1.1.0
485
+ #### Added:
486
+ 1. `generateDropZone` / `createDropZone`: drag-and-drop area with file type, size and count checks
487
+ 2. `generateWysiwyg` / `createWysiwyg` and the `wysiwyg` form field: rich text editor with a sanitized HTML source view
488
+ #### Changed:
489
+ 1. `generateFileUpload` is built on the drop zone; dragging over child elements no longer flickers
490
+
473
491
  ### 1.0.0
474
492
  #### Added:
475
493
  1. Stable public API (semantic versioning from this release on)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "athena-ui-js",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A small jQuery based UI framework: generated page layout, menus, login / registration / lost password forms, charts, tables, modals and themes, written as ES modules.",
5
5
  "type": "module",
6
6
  "main": "./src/athena-ui.js",
package/src/athena-ui.js CHANGED
@@ -2,7 +2,7 @@
2
2
  //Author: Csaba Tóth https://github.com/csabatibortoth/
3
3
  //LICENCE: MIT
4
4
  //Repository: https://github.com/csabatibortoth/athena-ui
5
- //Version: 1.0.0
5
+ //Version: 1.1.0
6
6
 
7
7
  /* global $ */
8
8
 
@@ -26,6 +26,7 @@ import * as dialog from "./libs/hephaestus/dialogGenerator/dialogGenerator.js";
26
26
  import * as navigation from "./libs/hephaestus/navigationGenerator/navigationGenerator.js";
27
27
  import * as components from "./libs/hephaestus/componentGenerator/componentGenerator.js";
28
28
  import * as inputs from "./libs/hephaestus/inputGenerator/inputGenerator.js";
29
+ import * as dropZone from "./libs/hephaestus/inputGenerator/dropZoneGenerator.js";
29
30
  import * as richText from "./libs/hephaestus/inputGenerator/richTextGenerator.js";
30
31
  import * as sanitize from "./libs/core/sanitize.js";
31
32
  import * as router from "./libs/hephaestus/router/router.js";
@@ -99,6 +100,7 @@ const AthenaUI = {
99
100
  ...navigation,
100
101
  ...components,
101
102
  ...inputs,
103
+ ...dropZone,
102
104
  ...richText,
103
105
  ...sanitize,
104
106
  ...router,
@@ -134,6 +136,7 @@ export * from "./libs/hephaestus/dialogGenerator/dialogGenerator.js";
134
136
  export * from "./libs/hephaestus/navigationGenerator/navigationGenerator.js";
135
137
  export * from "./libs/hephaestus/componentGenerator/componentGenerator.js";
136
138
  export * from "./libs/hephaestus/inputGenerator/inputGenerator.js";
139
+ export * from "./libs/hephaestus/inputGenerator/dropZoneGenerator.js";
137
140
  export * from "./libs/hephaestus/inputGenerator/richTextGenerator.js";
138
141
  export * from "./libs/core/sanitize.js";
139
142
  export * from "./libs/hephaestus/router/router.js";
@@ -6,7 +6,7 @@
6
6
  export const BORDERED_CLASS = "default-cointainer-bordered";
7
7
 
8
8
  export const defaults = Object.freeze({
9
- version: "1.0.0",
9
+ version: "1.1.0",
10
10
  container_class: "container",
11
11
  header_class: "header-container",
12
12
  header_menu_class: "header-menu menu",
@@ -44,6 +44,7 @@ const messages = {
44
44
  "validation.match": "The values do not match.",
45
45
  "validation.invalid": "The value is invalid.",
46
46
  "validation.fileSize": "{name} is larger than {max}.",
47
+ "validation.maxFiles": "At most {max} files are accepted.",
47
48
  "validation.url": "Enter a valid address (https://, mailto: or tel:).",
48
49
  "validation.dateRange": "The end date must not be before the start date.",
49
50
  "validation.dateMin": "The date must be {min} or later.",
@@ -68,7 +69,8 @@ const messages = {
68
69
  "rt.clear": "Clear formatting",
69
70
  "rt.undo": "Undo",
70
71
  "rt.redo": "Redo",
71
- "rt.linkPrompt": "Link address"
72
+ "rt.linkPrompt": "Link address",
73
+ "rt.source": "HTML source"
72
74
  },
73
75
  hu: {
74
76
  username: "Felhasználónév",
@@ -107,6 +109,7 @@ const messages = {
107
109
  "validation.match": "Az értékek nem egyeznek.",
108
110
  "validation.invalid": "Érvénytelen érték.",
109
111
  "validation.fileSize": "{name} mérete meghaladja a {max} értéket.",
112
+ "validation.maxFiles": "Legfeljebb {max} fájl fogadható el.",
110
113
  "validation.url": "Adjon meg érvényes címet (https://, mailto: vagy tel:).",
111
114
  "validation.dateRange": "A záró dátum nem lehet korábbi a kezdő dátumnál.",
112
115
  "validation.dateMin": "A dátum legkorábban {min} lehet.",
@@ -131,7 +134,8 @@ const messages = {
131
134
  "rt.clear": "Formázás törlése",
132
135
  "rt.undo": "Visszavonás",
133
136
  "rt.redo": "Újra",
134
- "rt.linkPrompt": "Hivatkozás címe"
137
+ "rt.linkPrompt": "Hivatkozás címe",
138
+ "rt.source": "HTML forrás"
135
139
  }
136
140
  };
137
141
 
@@ -8,7 +8,7 @@ import { createDateRange } from "../../core/dateRange.js";
8
8
  import { cx, el, mount, uid } from "../../core/dom.js";
9
9
  import { translate } from "../../core/i18n.js";
10
10
  import { validateDateRange, validateValue } from "../../core/validation.js";
11
- import { createRichText } from "../inputGenerator/richTextGenerator.js";
11
+ import { createRichText, createWysiwyg } from "../inputGenerator/richTextGenerator.js";
12
12
 
13
13
  const INPUT_TYPES = new Set([
14
14
  "text", "password", "email", "number", "date", "datetime-local", "time", "month", "week",
@@ -94,7 +94,7 @@ function choiceList(wrapper, kind, name, choices, value) {
94
94
  *
95
95
  * @param {object} options
96
96
  * @param {string} options.name key of the field in the form values
97
- * @param {"text"|"password"|"email"|"number"|"date"|"datetime-local"|"time"|"url"|"tel"|"search"|"color"|"range"|"file"|"hidden"|"textarea"|"select"|"checkbox"|"switch"|"radio"|"checkboxes"|"autocomplete"|"multiselect"|"daterange"|"richtext"} [options.type="text"]
97
+ * @param {"text"|"password"|"email"|"number"|"date"|"datetime-local"|"time"|"url"|"tel"|"search"|"color"|"range"|"file"|"hidden"|"textarea"|"select"|"checkbox"|"switch"|"radio"|"checkboxes"|"autocomplete"|"multiselect"|"daterange"|"richtext"|"wysiwyg"} [options.type="text"]
98
98
  * @param {string} [options.label]
99
99
  * @param {string} [options.help] help text under the control
100
100
  * @param {*} [options.value] initial value (boolean for checkbox / switch, array for checkboxes / multiple select)
@@ -186,8 +186,8 @@ export function generateField(options = {}) {
186
186
  controlNodes = [range.element];
187
187
  getValue = range.getValue;
188
188
  setValue = range.setValue;
189
- } else if (type === "richtext") {
190
- const rich = createRichText({
189
+ } else if (type === "richtext" || type === "wysiwyg") {
190
+ const rich = (type === "wysiwyg" ? createWysiwyg : createRichText)({
191
191
  id, value: options.value, placeholder, minHeight: options.minHeight, toolbar: options.toolbar, disabled: options.disabled
192
192
  });
193
193
  rich.editor.setAttribute("aria-labelledby", `${id}-label`);
@@ -0,0 +1,118 @@
1
+ //dropZoneGenerator for Athena UI
2
+ //Author: Csaba Tóth https://github.com/csabatibortoth/
3
+ //LICENCE: MIT
4
+ //Repository: https://github.com/csabatibortoth/athena-ui
5
+
6
+ import { cx, el, mount, setContent, uid } from "../../core/dom.js";
7
+ import { formatBytes, matchesAccept } from "../../core/format.js";
8
+ import { translate } from "../../core/i18n.js";
9
+
10
+ /**
11
+ * Generates a drag-and-drop area. Files dropped on it (or picked with the browser dialog after a click / Enter / Space)
12
+ * are checked against `accept`, `maxSize` and `maxFiles`; the valid ones are passed to `onDrop`, the rest to `onReject`.
13
+ * Nothing is read or sent: what to do with the files is up to the caller (generateFileUpload builds on this).
14
+ *
15
+ * @param {object} options
16
+ * @param {string|Element} [options.parent]
17
+ * @param {string} [options.id]
18
+ * @param {string} [options.class]
19
+ * @param {*} [options.content] text, Node, array or function; the default is the localized "Drop files here" text
20
+ * @param {string} [options.accept] like ".png,image/*"
21
+ * @param {boolean} [options.multiple=true]
22
+ * @param {number} [options.maxSize] bytes per file
23
+ * @param {number} [options.maxFiles] files accepted per drop
24
+ * @param {boolean} [options.clickable=true] click / Enter / Space opens the file dialog
25
+ * @param {boolean} [options.disabled=false]
26
+ * @param {(files: File[]) => void} [options.onDrop] valid files
27
+ * @param {(rejected: {file: File, reason: string}[]) => void} [options.onReject]
28
+ * @param {(over: boolean) => void} [options.onDragChange] a drag entered / left the area
29
+ * @returns {{element: HTMLElement, input: HTMLInputElement, handleFiles: (files: Iterable<File>) => void,
30
+ * setDisabled: (disabled: boolean) => void, open: () => void}}
31
+ */
32
+ export function createDropZone(options = {}) {
33
+ const { accept, multiple = true, maxSize, maxFiles, clickable = true } = options;
34
+ const hintId = uid("dropzone-hint");
35
+ const input = el("input", { class: "dropzone-input", attrs: { type: "file", accept, multiple, tabindex: "-1", "aria-hidden": "true" } });
36
+ const hint = el("div", { class: "dropzone-content", id: hintId });
37
+ setContent(hint, options.content ?? translate("dropFiles"));
38
+ const element = el("div", {
39
+ id: options.id,
40
+ class: cx("dropzone", options.class),
41
+ attrs: { role: clickable ? "button" : "group", tabindex: clickable ? "0" : null, "aria-describedby": hintId }
42
+ }, hint, input);
43
+ let depth = 0;
44
+ let disabled = false;
45
+
46
+ const setOver = over => {
47
+ element.classList.toggle("dropzone-over", over);
48
+ options.onDragChange?.(over);
49
+ };
50
+ const hasFiles = event => [...(event.dataTransfer?.types ?? [])].includes("Files");
51
+
52
+ function handleFiles(files) {
53
+ const valid = [];
54
+ const rejected = [];
55
+ let list = [...files];
56
+ if (!multiple) list = list.slice(0, 1);
57
+ for (const file of list) {
58
+ if (!matchesAccept(file, accept)) rejected.push({ file, reason: `${translate("validation.pattern")} (${file.name})` });
59
+ else if (maxSize != null && file.size > maxSize) rejected.push({ file, reason: translate("validation.fileSize", { name: file.name, max: formatBytes(maxSize) }) });
60
+ else if (maxFiles != null && valid.length >= maxFiles) rejected.push({ file, reason: translate("validation.maxFiles", { max: maxFiles }) });
61
+ else valid.push(file);
62
+ }
63
+ if (valid.length) options.onDrop?.(valid);
64
+ if (rejected.length) options.onReject?.(rejected);
65
+ }
66
+ const open = () => { if (!disabled) input.click(); };
67
+ function setDisabled(value) {
68
+ disabled = Boolean(value);
69
+ element.classList.toggle("dropzone-disabled", disabled);
70
+ element.setAttribute("aria-disabled", String(disabled));
71
+ if (clickable) element.tabIndex = disabled ? -1 : 0;
72
+ }
73
+
74
+ if (clickable) {
75
+ element.addEventListener("click", open);
76
+ element.addEventListener("keydown", event => {
77
+ if (event.key === "Enter" || event.key === " ") {
78
+ event.preventDefault();
79
+ open();
80
+ }
81
+ });
82
+ }
83
+ input.addEventListener("change", () => {
84
+ handleFiles(input.files);
85
+ input.value = "";
86
+ });
87
+ // The enter / leave pair fires for every child element, so the nesting depth is counted to avoid flicker.
88
+ element.addEventListener("dragenter", event => {
89
+ if (disabled || !hasFiles(event)) return;
90
+ event.preventDefault();
91
+ if (++depth === 1) setOver(true);
92
+ });
93
+ element.addEventListener("dragover", event => {
94
+ if (disabled || !hasFiles(event)) return;
95
+ event.preventDefault();
96
+ event.dataTransfer.dropEffect = "copy";
97
+ });
98
+ element.addEventListener("dragleave", () => {
99
+ if (depth > 0 && --depth === 0) setOver(false);
100
+ });
101
+ element.addEventListener("drop", event => {
102
+ event.preventDefault();
103
+ const wasOver = depth > 0;
104
+ depth = 0;
105
+ if (wasOver) setOver(false);
106
+ if (!disabled && hasFiles(event)) handleFiles(event.dataTransfer.files);
107
+ });
108
+
109
+ setDisabled(options.disabled);
110
+ return { element, input, handleFiles, setDisabled, open };
111
+ }
112
+
113
+ /** Generates a drag-and-drop area into `parent`, see createDropZone. */
114
+ export function generateDropZone(options = {}) {
115
+ const zone = createDropZone(options);
116
+ mount(options.parent, zone.element);
117
+ return zone;
118
+ }
@@ -10,6 +10,7 @@ import { formatBytes, matchesAccept } from "../../core/format.js";
10
10
  import { translate } from "../../core/i18n.js";
11
11
  import { generateButton } from "../formGenerator/formBuilder.js";
12
12
  import { generateProgress } from "../feedbackGenerator/feedbackGenerator.js";
13
+ import { createDropZone } from "./dropZoneGenerator.js";
13
14
 
14
15
  /* ------------------COMBOBOX (autocomplete, multi select)------------------- */
15
16
 
@@ -98,11 +99,14 @@ export function generateDateRange(options = {}) {
98
99
  export function generateFileUpload(options = {}) {
99
100
  const { accept, multiple = true, maxSize, url, fieldName = "file", autoUpload = true } = options;
100
101
  const entries = [];
101
- const input = el("input", { class: "dropzone-input", attrs: { type: "file", accept, multiple, tabindex: "-1", "aria-hidden": "true" } });
102
- const zone = el("div", { class: "dropzone", attrs: { role: "button", tabindex: "0" } }, el("span", { text: translate("dropFiles") }));
102
+ const zone = createDropZone({
103
+ accept, multiple, maxSize,
104
+ onDrop: files => add(files),
105
+ onReject: rejected => { errors.textContent = rejected.map(item => item.reason).join(" "); }
106
+ });
103
107
  const list = el("ul", { class: "file-list" });
104
108
  const errors = el("div", { class: "field-error", attrs: { role: "alert" } });
105
- const element = el("div", { id: options.id, class: cx("file-upload", options.class) }, zone, input, errors, list);
109
+ const element = el("div", { id: options.id, class: cx("file-upload", options.class) }, zone.element, errors, list);
106
110
 
107
111
  function render(entry) {
108
112
  entry.node ??= el("li", { class: "file-item" });
@@ -191,25 +195,6 @@ export function generateFileUpload(options = {}) {
191
195
  }
192
196
  }
193
197
 
194
- zone.addEventListener("click", () => input.click());
195
- zone.addEventListener("keydown", event => {
196
- if (event.key === "Enter" || event.key === " ") {
197
- event.preventDefault();
198
- input.click();
199
- }
200
- });
201
- input.addEventListener("change", () => {
202
- add(input.files);
203
- input.value = "";
204
- });
205
- zone.addEventListener("dragover", event => { event.preventDefault(); zone.classList.add("dropzone-over"); });
206
- zone.addEventListener("dragleave", () => zone.classList.remove("dropzone-over"));
207
- zone.addEventListener("drop", event => {
208
- event.preventDefault();
209
- zone.classList.remove("dropzone-over");
210
- add(event.dataTransfer.files);
211
- });
212
-
213
198
  mount(options.parent, element);
214
199
  return {
215
200
  element,
@@ -30,9 +30,11 @@ const BUTTONS = {
30
30
  unlink: { label: "rt.unlink", text: "⛓", run: () => exec("unlink") },
31
31
  clear: { label: "rt.clear", text: "⌫", run: () => { exec("removeFormat"); exec("formatBlock", "p"); } },
32
32
  undo: { label: "rt.undo", text: "↶", run: () => exec("undo") },
33
- redo: { label: "rt.redo", text: "↷", run: () => exec("redo") }
33
+ redo: { label: "rt.redo", text: "↷", run: () => exec("redo") },
34
+ source: { label: "rt.source", text: "</>", run: "source", active: () => false, class: "rt-source" }
34
35
  };
35
36
  const DEFAULT_TOOLBAR = ["bold", "italic", "underline", "strike", "|", "h2", "h3", "quote", "|", "ul", "ol", "|", "link", "unlink", "|", "clear", "undo", "redo"];
37
+ const WYSIWYG_TOOLBAR = [...DEFAULT_TOOLBAR, "|", "source"];
36
38
 
37
39
  /**
38
40
  * Generates a rich text editor. The content is HTML restricted to a safe set of tags (paragraphs, headings,
@@ -46,7 +48,8 @@ const DEFAULT_TOOLBAR = ["bold", "italic", "underline", "strike", "|", "h2", "h3
46
48
  * @param {string} [options.value] initial HTML
47
49
  * @param {string} [options.placeholder]
48
50
  * @param {string[]} [options.toolbar] button names (bold, italic, underline, strike, h2, h3, quote, ul, ol, link,
49
- * unlink, clear, undo, redo) and "|" separators
51
+ * unlink, clear, undo, redo, source) and "|" separators. `source` switches between the visual editor and
52
+ * the sanitized HTML source.
50
53
  * @param {number} [options.minHeight=140] px
51
54
  * @param {boolean} [options.disabled]
52
55
  * @param {(html: string) => void} [options.onChange]
@@ -66,7 +69,8 @@ export function createRichText(options = {}) {
66
69
  }
67
70
  });
68
71
  editor.style.minHeight = `${options.minHeight ?? 140}px`;
69
- const root = el("div", { class: cx("richtext", options.disabled && "rt-disabled", options.class) }, toolbar, editor);
72
+ const source = el("textarea", { class: "rt-source-view", attrs: { hidden: true, spellcheck: "false", "aria-label": translate("rt.source") } });
73
+ const root = el("div", { class: cx("richtext", options.disabled && "rt-disabled", options.class) }, toolbar, editor, source);
70
74
  const buttons = [];
71
75
  let savedRange = null;
72
76
 
@@ -112,6 +116,26 @@ export function createRichText(options = {}) {
112
116
  changed();
113
117
  }
114
118
 
119
+ // Source view: the textarea shows the sanitized HTML; leaving it sanitizes what was typed.
120
+ function toggleSource() {
121
+ const open = source.hidden;
122
+ if (open) {
123
+ source.value = getValue();
124
+ source.style.minHeight = editor.style.minHeight;
125
+ } else {
126
+ setValue(source.value);
127
+ changed();
128
+ }
129
+ source.hidden = !open;
130
+ editor.hidden = open;
131
+ root.classList.toggle("rt-source-open", open);
132
+ for (const { button, spec } of buttons) {
133
+ if (spec.run === "source") button.setAttribute("aria-pressed", String(open));
134
+ else button.disabled = open || Boolean(options.disabled);
135
+ }
136
+ (open ? source : editor).focus();
137
+ }
138
+
115
139
  names.forEach(name => {
116
140
  if (name === "|") {
117
141
  toolbar.append(el("span", { class: "rt-separator", attrs: { role: "separator", "aria-hidden": "true" } }));
@@ -127,6 +151,7 @@ export function createRichText(options = {}) {
127
151
  button.addEventListener("mousedown", event => event.preventDefault());
128
152
  button.addEventListener("click", async () => {
129
153
  if (spec.run === "link") await addLink();
154
+ else if (spec.run === "source") toggleSource();
130
155
  else {
131
156
  editor.focus();
132
157
  spec.run();
@@ -151,7 +176,7 @@ export function createRichText(options = {}) {
151
176
 
152
177
  function refresh() {
153
178
  for (const { button, spec } of buttons) {
154
- if (spec.active) button.setAttribute("aria-pressed", String(Boolean(spec.active())));
179
+ if (spec.active && spec.run !== "source") button.setAttribute("aria-pressed", String(Boolean(spec.active())));
155
180
  }
156
181
  }
157
182
  const onSelection = () => {
@@ -186,6 +211,7 @@ export function createRichText(options = {}) {
186
211
  function setValue(html) {
187
212
  editor.innerHTML = sanitizeHtml(html);
188
213
  syncEmpty();
214
+ if (!source.hidden) source.value = getValue();
189
215
  }
190
216
  // <p> instead of <div> for new lines
191
217
  try {
@@ -220,3 +246,12 @@ export function generateRichText(options = {}) {
220
246
  mount(options.parent, wrapper);
221
247
  return rich;
222
248
  }
249
+
250
+ /**
251
+ * Generates a WYSIWYG editor: the rich text editor with the full toolbar plus a `source` button that switches to
252
+ * the (sanitized) HTML source. Takes the same options as createRichText.
253
+ */
254
+ export const createWysiwyg = (options = {}) => createRichText({ ...options, toolbar: options.toolbar ?? WYSIWYG_TOOLBAR });
255
+
256
+ /** Generates a labelled WYSIWYG editor, see createWysiwyg. */
257
+ export const generateWysiwyg = (options = {}) => generateRichText({ ...options, toolbar: options.toolbar ?? WYSIWYG_TOOLBAR });
@@ -992,6 +992,16 @@ textarea {
992
992
  display: none;
993
993
  }
994
994
 
995
+ .dropzone-disabled {
996
+ opacity: 0.5;
997
+ cursor: not-allowed;
998
+ }
999
+
1000
+ .dropzone-disabled:hover {
1001
+ border-color: var(--athena-line);
1002
+ background-color: transparent;
1003
+ }
1004
+
995
1005
  .file-list {
996
1006
  list-style: none;
997
1007
  padding: 0;
@@ -1443,6 +1453,24 @@ textarea {
1443
1453
  margin: 0 0 8px;
1444
1454
  }
1445
1455
 
1456
+ .rt-source-view {
1457
+ display: block;
1458
+ box-sizing: border-box;
1459
+ width: 100%;
1460
+ padding: 8px 10px;
1461
+ border: 0;
1462
+ outline: none;
1463
+ resize: vertical;
1464
+ background: transparent;
1465
+ color: var(--athena-fg);
1466
+ font-family: monospace;
1467
+ }
1468
+
1469
+ .rt-source-view[hidden],
1470
+ .rt-editor[hidden] {
1471
+ display: none;
1472
+ }
1473
+
1446
1474
  .rt-disabled .rt-editor {
1447
1475
  opacity: 0.6;
1448
1476
  }