Skip to content

Commit

Permalink
Editor Titleize button.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Sep 17, 2024
1 parent 4f80f49 commit 5ba250a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
11 changes: 10 additions & 1 deletion assets/js/editor-artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
youtube as validateYouTube,
number as validateNumber,
} from "./artifact-validate.mjs";
import { clipValue, getElmById } from "./helper.mjs";
import { clipValue, getElmById, titleize } from "./helper.mjs";

(() => {
"use strict";
Expand Down Expand Up @@ -283,6 +283,15 @@ import { clipValue, getElmById } from "./helper.mjs";
title.value = titleU[0].value;
title.classList.add("is-valid");
});
const titleizeBtn = document.getElementById("artifact-editor-titleize");
if (titleizeBtn.length === 0) {
throw new Error("The titleize button is missing.");
}
titleizeBtn.addEventListener("click", () => {
title.value = titleize(title.value);
let event = new Event("keyup");
title.dispatchEvent(event);
});

const ct = document.getElementById("artifact-editor-credit-text");
if (ct === null) {
Expand Down
34 changes: 34 additions & 0 deletions assets/js/helper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@
* This module provides functions for handling common tasks.
*/

/**
* Titleizes a string of text but keeps common adverbs in lowercase.
*
* @param {string} text - The input text to be titleized.
* @returns {string} - The titleized text.
*/
export function titleize(text) {
const commonAdverbs = [
"a",
"an",
"and",
"as",
"but",
"for",
"if",
"of",
"or",
"so",
"the",
"to",
];
return text
.split(" ")
.map((word, index) => {
// Capitalize the first word and any word not in the common adverbs list
if (index === 0 || !commonAdverbs.includes(word.toLowerCase())) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
// Keep common adverbs in lowercase
return word.toLowerCase();
})
.join(" ");
}

/**
* Copies the text content of an HTML element to the clipboard.
* @async
Expand Down
2 changes: 1 addition & 1 deletion public/js/editor-artifact.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/editor-assets.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/js/readme.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/js/uploader.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions view/app/artifactedit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ aria-labelledby="artifact-delete-forever-modal-label" aria-hidden="true">
hx-include="[name='artifact-editor-key'],
[name='artifact-editor-titleundo']">
Reset<small class="link-dark"> title to <q id="artifact-editor-title-quote">{{$title}}</q></small></button>
<button class="btn btn-link" type="button" id="artifact-editor-titleize">Titleize</button>
</div>
</div>
{{/* Comments */}}
Expand Down

0 comments on commit 5ba250a

Please sign in to comment.