Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use typescript for (almost) all main site javascript #489

Merged
merged 97 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
ae123ea
refactor: Use typescript for main application file
Mitcheljager Nov 8, 2024
437f8aa
refactor: Use ts for copy and related utils
Mitcheljager Nov 9, 2024
54665d9
chore: set outDir in tsconfig
Mitcheljager Nov 9, 2024
d442502
refactor: Use ts for carousel-cards
Mitcheljager Nov 9, 2024
4fcc7ab
fix: Use event target to set carousel id
Mitcheljager Nov 9, 2024
00ca949
refactor: Use ts for carousel and related utils
Mitcheljager Nov 9, 2024
bbb8c77
fix: Use event directly and check instance of currentTarget
Mitcheljager Nov 9, 2024
a2f1e91
chore: Add .ts files to .jsconfig
Mitcheljager Nov 9, 2024
fa4f047
refactor: Convert for utils to ts
Mitcheljager Nov 9, 2024
e5f4814
refactor: Convert more utils to ts
Mitcheljager Nov 9, 2024
85d7866
refactor: Use ts for more utils
Mitcheljager Nov 9, 2024
37d1481
refactor: Use ts for more utils
Mitcheljager Nov 9, 2024
6f7ac51
refactor: Use ts for more utils
Mitcheljager Nov 9, 2024
ab9f3c5
chore: Remove unused file
Mitcheljager Nov 9, 2024
1053b14
refactor: Use ts for more utils
Mitcheljager Nov 9, 2024
9d9377a
refactor: Use ts for more utils
Mitcheljager Nov 9, 2024
674d96e
refactor: Use ts for more utils
Mitcheljager Nov 9, 2024
2348b01
refactor: Use ts for more utils
Mitcheljager Nov 9, 2024
ae41797
refactor: Use ts for turbolinks-prefetch
Mitcheljager Nov 9, 2024
eb7a212
chore: Use ts extension for editor entrypoint
Mitcheljager Nov 9, 2024
bca0986
fix: Only set image src if given without returning
Mitcheljager Nov 9, 2024
819c30f
Merge branch 'master' into refactor/typescript
Mitcheljager Nov 9, 2024
1d53292
test: Disable test while svelte components are not rendering correctly
Mitcheljager Nov 9, 2024
1cfe006
refactor: Use non-null assertions where relevant
Mitcheljager Nov 13, 2024
89cad23
refactor: Add types to debounceStore
Mitcheljager Nov 13, 2024
efaa436
chore: Move types to separate folder
Mitcheljager Nov 13, 2024
5ca0485
refactor: Add types to base editor util and stores
Mitcheljager Nov 13, 2024
763d226
chore: Update various imports to use proper alias
Mitcheljager Nov 13, 2024
f56d4e9
refactor: Add types to files util
Mitcheljager Nov 13, 2024
4576e4d
chore: Use ts extension for crawler util
Mitcheljager Nov 13, 2024
633034d
refactor: Add types to projectBackups util
Mitcheljager Nov 13, 2024
51fe441
refactor: Replace more utils with ts
Mitcheljager Nov 14, 2024
45d8aa3
chore: Replace imports with prop alias
Mitcheljager Nov 14, 2024
c759305
chore: Add return types to all typescript editor functions
Mitcheljager Nov 14, 2024
35d2901
refactor: Use ts for project utils
Mitcheljager Nov 14, 2024
41f84ba
chore: Update more imports to use direct alias
Mitcheljager Nov 14, 2024
f29833b
refactor: Use ts for operator and parse utils
Mitcheljager Nov 14, 2024
b351b4d
chore: Add missing return type
Mitcheljager Nov 14, 2024
db2b3eb
refactor: Use ts for indent util
Mitcheljager Nov 14, 2024
a7f7205
fix: Prevent error when switching items
Mitcheljager Nov 14, 2024
764f4d0
fix: Prevent re-opening of first item when updating currentProject
Mitcheljager Nov 14, 2024
4cd2068
fix: Incorrect tab indent on new enter
Mitcheljager Nov 14, 2024
f62b512
chore: Use string type for maps
Mitcheljager Nov 14, 2024
d97b8f5
refactor: Use TS for more stores and utils
Mitcheljager Nov 14, 2024
59e2ef0
refactor: Use ts for indentedLineWrap util
Mitcheljager Nov 14, 2024
baf9c40
chore: Replace imports with proper aliases
Mitcheljager Nov 14, 2024
64e74e1
chore: Correct type in TranslationKey
Mitcheljager Nov 14, 2024
a64a31d
chore: Add eslint ts config
Mitcheljager Nov 14, 2024
e0e60c7
Merge branch 'refactor/typescript' into refactor/ts-compiler
Mitcheljager Nov 14, 2024
27b6869
chore: Allow currentItem to be optional in destroyItem
Mitcheljager Nov 15, 2024
e36bba8
fix: Directly assign item rather than creating a new var
Mitcheljager Nov 15, 2024
0fdfe8a
test: Adjust currentItem empty value in tests
Mitcheljager Nov 15, 2024
64d9303
refactor: Use ts for comments
Mitcheljager Nov 16, 2024
3972022
chore: Migrate .eslintrc.yml to eslint.config.mjs
Mitcheljager Nov 16, 2024
4fd7c4d
chore: Run eslint --fix
Mitcheljager Nov 16, 2024
5401b34
refactor: Add return types to all non-arrow functions
Mitcheljager Nov 16, 2024
56e3006
chore: Remove unintended return values
Mitcheljager Nov 16, 2024
971940c
fix: Incorrect param in eslint.config
Mitcheljager Nov 16, 2024
ff2d04f
chore: Don't require return types in arrow functions
Mitcheljager Nov 16, 2024
1d4a233
chore: Remove console.log
Mitcheljager Nov 17, 2024
a665336
refactor: Use his directly
Mitcheljager Nov 17, 2024
3399b08
chore: Add more return types to functions
Mitcheljager Nov 17, 2024
2d24c48
chore: Add return types to turbolinks-prefetch
Mitcheljager Nov 17, 2024
76e9c6a
chore: Remove unused vars
Mitcheljager Nov 17, 2024
fec2b18
Merge branch 'refactor/typescript' into refactor/ts-compiler
Mitcheljager Nov 17, 2024
0467f2f
chore: Remove unused import
Mitcheljager Nov 17, 2024
d9665d1
chore: Adjust type in modal store
Mitcheljager Nov 17, 2024
f68c868
chore: Use single import
Mitcheljager Nov 17, 2024
82deadb
chore: Add return value to setCurrentItemById
Mitcheljager Nov 17, 2024
63df76b
chore: Remove console.log
Mitcheljager Nov 17, 2024
dde46a0
chore: Remove unused import
Mitcheljager Nov 17, 2024
e88c698
Merge branch 'refactor/ts-compiler' into refactor/ts-editor-compiler
Mitcheljager Nov 17, 2024
d526f15
refactor: Use ts for subroutines, translations, and mixins
Mitcheljager Nov 17, 2024
92f7807
docs: Avoid repeating types in jsdocs and ts
Mitcheljager Nov 17, 2024
4b06b79
refactor: Use ts for constants, for, and each
Mitcheljager Nov 17, 2024
20145d3
refactor: Add type for WorkshopConstant
Mitcheljager Nov 17, 2024
ec5fe9c
chore: Add comment to "each" regex
Mitcheljager Nov 17, 2024
eda8da4
refactor: Use ts for operators, conditionals, and variables
Mitcheljager Nov 17, 2024
4571e4b
refactor: Use ts for main compile file
Mitcheljager Nov 17, 2024
2ccb59e
refactor: Use ts for several editor lib utils
Mitcheljager Nov 17, 2024
07eb0cd
refactor: Use ts for linter
Mitcheljager Nov 17, 2024
de3d73a
refactor: Use ts for constant files
Mitcheljager Nov 17, 2024
aca1a14
refactor: Use ts for parameters editor action
Mitcheljager Nov 17, 2024
09bfc01
Merge branch 'master' into refactor/typescript
Mitcheljager Nov 17, 2024
c43e596
Merge branch 'refactor/typescript' into refactor/ts-compiler
Mitcheljager Nov 17, 2024
453b551
Merge branch 'refactor/ts-compiler' into refactor/ts-editor-compiler
Mitcheljager Nov 17, 2024
5f74c77
Merge branch 'refactor/ts-editor-compiler' into refactor/ts-lib
Mitcheljager Nov 17, 2024
eae307f
Merge branch 'master' into refactor/typescript
Mitcheljager Nov 21, 2024
e6097cf
Merge branch 'master' into refactor/typescript
Mitcheljager Nov 23, 2024
83052e7
Merge branch 'refactor/typescript' into refactor/ts-compiler
Mitcheljager Nov 23, 2024
d451c69
Merge branch 'refactor/ts-compiler' into refactor/ts-editor-compiler
Mitcheljager Nov 23, 2024
e44f6f5
Merge branch 'refactor/ts-editor-compiler' into refactor/ts-lib
Mitcheljager Nov 23, 2024
3b7fe8f
Merge pull request #492 from Mitcheljager/refactor/ts-lib
Mitcheljager Nov 23, 2024
68e0461
Merge pull request #491 from Mitcheljager/refactor/ts-editor-compiler
Mitcheljager Nov 23, 2024
7b6ac46
Merge pull request #490 from Mitcheljager/refactor/ts-compiler
Mitcheljager Nov 23, 2024
58d551d
chore: Add completion type to extraCompletions
Mitcheljager Nov 23, 2024
61737d7
refactor: Use ts for removeTrailingWhitespace
Mitcheljager Nov 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ import * as getPartial from "@src/get-partial"
import * as getReportsForm from "@src/get-reports-form"
import * as getSnippet from "@src/get-snippet"
import * as getVerifiedUsers from "@src/get-verified-users"
import * as imagePreview from "@src/image-preview"
import * as infiniteScroll from "@src/infinite-scroll"
import * as lazyVideo from "@src/lazy-video"
import * as microlight from "@src/microlight"
import * as modal from "@src/modal"
import * as navigation from "@src/navigation"
import * as numPlayersSlider from "@src/num-players-slider"
import * as ollieForm from "@src/ollie-form"
import * as revealByCheckbox from "@src/reveal-by-checkbox"
import * as revealBySelect from "@src/reveal-by-select"
import * as revealOnDifference from "@src/reveal-on-difference"
import * as scrollIndicator from "@src/scroll-indicator"
import * as scrollIntoViewOnLoad from "@src/scroll-into-view-on-load"
import * as sticky from "@src/sticky"
Expand All @@ -56,15 +53,12 @@ document.addEventListener("turbolinks:load", () => {
getReportsForm.bind()
getSnippet.bind()
getVerifiedUsers.bind()
imagePreview.bind()
infiniteScroll.bind()
lazyVideo.bind()
modal.bind()
navigation.bind()
ollieForm.bind()
revealByCheckbox.bind()
revealBySelect.bind()
revealOnDifference.bind()
scrollIndicator.bind()
sticky.bind()
tabs.bind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import * as favorite from "@src/favorite"
import * as getPostAnalytics from "@src/get-post-analytics"
import * as getUserAnalytics from "@src/get-user-analytics"
import * as ide from "@src/ide"
import * as imagePreview from "@src/image-preview"
import * as inscrybMde from "@src/inscryb-mde"
import * as linkedInput from "@src/linked-input"
import * as setCssVariable from "@src/set-css-variable"
import * as revealByCheckbox from "@src/reveal-by-checkbox"
import * as revealOnDifference from "@src/reveal-on-difference"

document.addEventListener("turbolinks:load", () => {
const svelteComponents = {
Expand Down Expand Up @@ -52,6 +55,9 @@ document.addEventListener("turbolinks:load", () => {
getUserAnalytics.bind()
setCssVariable.bind()
linkedInput.bind()
imagePreview.bind()
revealByCheckbox.bind()
revealOnDifference.bind()

chart.render()
inscrybMde.render()
Expand All @@ -60,11 +66,11 @@ document.addEventListener("turbolinks:load", () => {
document.addEventListener("turbolinks:before-cache", () => {
inscrybMde.destroy()

const svelteComponents = document.querySelectorAll("[data-svelte-component]")
svelteComponents.forEach(element => element.dataset.initialized = null)
const svelteComponents = Array.from(document.querySelectorAll("[data-svelte-component]")) as HTMLElement[]
svelteComponents.forEach(element => element.dataset.initialized = "null")
})

document.addEventListener("turbolinks:click", () => {
const svelteComponents = document.querySelectorAll("[data-svelte-component]")
svelteComponents.forEach(element => element.dataset.initialized = null)
const svelteComponents = Array.from(document.querySelectorAll("[data-svelte-component]")) as HTMLElement[]
svelteComponents.forEach(element => element.dataset.initialized = "null")
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export function bind() {
elements.forEach((element) => element.removeAndAddEventListener("input", applyCustomCSS))
}

function applyCustomCSS() {
const value = this.value
function applyCustomCSS({ currentTarget }: { currentTarget: HTMLFormElement }) {
const value = currentTarget.value
const styleTag = document.querySelector("#custom-css")

styleTag.textContent = value
if (styleTag) styleTag.textContent = value
}
42 changes: 25 additions & 17 deletions app/javascript/src/blocks.js → app/javascript/src/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ import LimitedCheckboxes from "@components/form/LimitedCheckboxes.svelte"
import FetchRails from "@src/fetch-rails"

export function bind() {
const element = document.querySelector("[data-role~='block-sortable']")
const element = document.querySelector("[data-role~='block-sortable']") as HTMLElement

if (element) buildBlockSortable(element)

const createBlockElements = document.querySelectorAll("[data-action~='create-block']")
createBlockElements.forEach(element => element.removeAndAddEventListener("click", createBlock))
}

function createBlock() {
if (this.dataset.disabled == "true") return
function createBlock({ currentTarget }: { currentTarget: HTMLElement }) {
if (currentTarget.dataset.disabled == "true") return

this.dataset.disabled = true
currentTarget.dataset.disabled = "true"

new FetchRails("/blocks", { block: { content_type: this.dataset.contentType, name: this.dataset.name } })
new FetchRails("/blocks", { block: { content_type: currentTarget.dataset.contentType, name: currentTarget.dataset.name } })
.post().then(data => {
new Promise((resolve) => new Function("resolve", data)(resolve))
renderSvelteComponents()
}).finally(() => {
this.dataset.disabled = false
currentTarget.dataset.disabled = "false"
})
}

function buildBlockSortable(element) {
function buildBlockSortable(element: HTMLElement) {
Sortable.create(element, {
draggable: "[data-sortable-block]",
animation: 50,
Expand All @@ -35,11 +35,12 @@ function buildBlockSortable(element) {
}

function updateBlockSortable() {
const blocks = document.querySelectorAll(".content-block")
const blocks = Array.from(document.querySelectorAll(".content-block")) as HTMLElement[]

const positions = []
const positions: { id: any[]; position: number }[] = []
blocks.forEach((block, i) => positions.push({ id: [block.dataset.id], position: i }))

// @ts-ignore
const progressBar = new Turbolinks.ProgressBar()
progressBar.setValue(0)
progressBar.show()
Expand All @@ -55,23 +56,30 @@ function renderSvelteComponents() {
initializeSvelteComponent("LimitedCheckboxes", LimitedCheckboxes)
}

export function insertBlockTemplate(event) {
export function insertBlockTemplate(event: MouseEvent) {
event.preventDefault()

const template = document.getElementById(`${this.dataset.template}`).content.cloneNode(true)
const targetElement = document.querySelector(`[data-template-target="${this.dataset.target}"]`)
const currentTarget = event.currentTarget as HTMLElement
const templateElement = document.getElementById(`${currentTarget.dataset.template}`) as HTMLFormElement

targetElement.append(template)
if (!templateElement) return

const template = templateElement.content.cloneNode(true)
const targetElement = document.querySelector(`[data-template-target="${currentTarget.dataset.target}"]`)

if (targetElement) targetElement.append(template)
}

export function removeBlockTemplate(event) {
export function removeBlockTemplate(event: MouseEvent) {
event.preventDefault()

const target = event.target.closest("[data-remove-target]")
target.remove()
const currentTarget = event.currentTarget as HTMLElement
const target = currentTarget.closest("[data-remove-target]")

if (target) target.remove()
}

export function buildInputSortable(element) {
export function buildInputSortable(element: HTMLElement) {
Sortable.create(element, {
animation: 50,
handle: "[data-role~='sortable-handle']"
Expand Down
62 changes: 0 additions & 62 deletions app/javascript/src/carousel-cards.js

This file was deleted.

67 changes: 67 additions & 0 deletions app/javascript/src/carousel-cards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Siema, { type SiemaOptions } from "siema"

let carouselCards: Siema[] = []

export function destroy() {
if (carouselCards.length == 0) return

const elements = document.querySelectorAll("[data-role='carousel-cards']")

elements.forEach(element => { element.classList.remove("initialised") })
carouselCards.forEach(carousel => carousel.destroy(true))

carouselCards = []
}

export function render() {
const elements = Array.from(document.querySelectorAll("[data-role='carousel-cards']")) as HTMLElement[]

if (elements.length == 0) return

elements.forEach(element => {
carouselCards = [...carouselCards, new Siema({
selector: element,
onInit: (() => element.classList.add("initialised")),
onChange: carouselCardsChanged,
perPage: { 450: 2, 768: 3 },
duration: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : 200
})]

element.dataset.carouselId = (carouselCards.length - 1).toString()
})

const previousElements = document.querySelectorAll("[data-action='carousel-previous']")
previousElements.forEach((element) => element.removeAndAddEventListener("click", carouselPrevious))

const nextElements = document.querySelectorAll("[data-action='carousel-next']")
nextElements.forEach((element) => element.removeAndAddEventListener("click", carouselNext))
}

function carouselCardsChanged(this: Siema & SiemaOptions) {
const selector = this.selector as HTMLElement
if (!selector) return

const parent = selector.closest(".card-carousel")
if (!parent) return

const nextElement = parent.querySelector("[data-action='carousel-next']")
const previousElement = parent.querySelector("[data-action='carousel-previous']")

if (previousElement) previousElement.classList.toggle("card-carousel__control--disabled", this.currentSlide == 0)
if (nextElement) nextElement.classList.toggle("card-carousel__control--disabled", this.currentSlide + this.perPage >= parseInt(selector?.dataset?.max || ''))
}

function carouselNext({ currentTarget }: { currentTarget: HTMLElement }) {
const carouselId = getCarouselId(currentTarget)
carouselCards[carouselId].next()
}

function carouselPrevious({ currentTarget }: { currentTarget: HTMLElement }) {
const carouselId = getCarouselId(currentTarget)
carouselCards[carouselId].prev()
}

function getCarouselId(element: HTMLElement): number {
const carouselElement = element.closest(".card-carousel")?.querySelector("[data-role='carousel-cards']") as HTMLElement
return parseInt(carouselElement.dataset.carouselId || '0')
}
Loading
Loading