Skip to content

Commit

Permalink
inline globals
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Feb 5, 2023
1 parent 7aee47a commit d8c71c6
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 77 deletions.
8 changes: 4 additions & 4 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"preset": "ts-jest",
"resetMocks": true,
"timers": "modern",
"moduleNameMapper": {
"blessing-skin": "<rootDir>/types.ts"
},
"transform": {
"^.+\\.svelte$": "@gplane/svelte-jest",
"^.+\\.tsx?$": "ts-jest"
},
"setupFilesAfterEnv": ["@testing-library/jest-dom/extend-expect"]
"setupFilesAfterEnv": [
"@testing-library/jest-dom/extend-expect",
"<rootDir>/setup-test.ts"
]
}
7 changes: 3 additions & 4 deletions plugins/config-generator/assets/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import hljs from 'highlight.js/lib/core'
import json from 'highlight.js/lib/languages/json'
import { site_name, base_url } from 'blessing-skin'

hljs.registerLanguage('json', json)

Expand All @@ -10,14 +9,14 @@ document
.querySelector('#download-extra-list')
?.addEventListener('click', () => {
const content = JSON.stringify({
name: site_name,
name: globalThis.blessing.site_name,
type: 'CustomSkinAPI',
root: `${base_url}/csl/`,
root: `${globalThis.blessing.base_url}/csl/`,
})

const a = document.createElement('a')
const blob = new Blob([content], { type: 'application/json' })
a.download = `${site_name}.json`
a.download = `${globalThis.blessing.site_name}.json`
a.href = URL.createObjectURL(blob)
a.click()
a.remove()
Expand Down
6 changes: 2 additions & 4 deletions plugins/disable-registration/assets/modifyButton.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { base_url, t } from 'blessing-skin'

const button = document.querySelector<HTMLAnchorElement>('.main-button')
if (button && button.href.endsWith('/auth/register')) {
button.textContent = t('auth.login')
const url = new URL(base_url)
button.textContent = globalThis.blessing.t('auth.login')
const url = new URL(globalThis.blessing.base_url)
url.pathname = '/auth/login'
button.href = url.toString()
}
9 changes: 3 additions & 6 deletions plugins/mojang-verification/assets/update-uuid.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { fetch, notify } from 'blessing-skin'

document.querySelector('#update-uuid')?.addEventListener('click', async () => {
const { code, message }: { code: null; message: string } = await fetch.post(
'/mojang/update-uuid',
)
const { toast } = notify
const { code, message }: { code: null; message: string } =
await globalThis.blessing.fetch.post('/mojang/update-uuid')
const { toast } = globalThis.blessing.notify
if (code === 0) {
toast.success(message)
} else {
Expand Down
17 changes: 8 additions & 9 deletions plugins/restricted-email-domains/assets/ConfigEditor.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<script lang="ts">
import { onMount } from 'svelte'
import { nanoid } from 'nanoid'
import { fetch, t, notify } from 'blessing-skin'
import DomainsList from './DomainsList.svelte'
const { fetch, t, notify } = globalThis.blessing
type Item = { id: string; value: string }
let isLoading = true
Expand All @@ -13,12 +14,8 @@
let isDenyListDirty = false
onMount(async () => {
const {
allow,
deny,
}: { allow: string[]; deny: string[] } = await fetch.get(
'/admin/restricted-email-domains',
)
const { allow, deny }: { allow: string[]; deny: string[] } =
await fetch.get('/admin/restricted-email-domains')
allowList = allow.map((value) => ({ id: nanoid(), value }))
denyList = deny.map((value) => ({ id: nanoid(), value }))
isLoading = false
Expand Down Expand Up @@ -80,7 +77,8 @@
on:edit={() => (isAllowListDirty = true)}
on:add={createAllowItem}
on:remove={(event) => removeAllowItem(event.detail)}
on:save={saveAllowList} />
on:save={saveAllowList}
/>
</div>
<div class="col-lg-6">
<DomainsList
Expand All @@ -92,6 +90,7 @@
on:edit={() => (isDenyListDirty = true)}
on:add={createDenyItem}
on:remove={(event) => removeDenyItem(event.detail)}
on:save={saveDenyList} />
on:save={saveDenyList}
/>
</div>
</div>
9 changes: 6 additions & 3 deletions plugins/restricted-email-domains/assets/DomainsList.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte'
import { t } from 'blessing-skin'
const { t } = globalThis.blessing
type Item = { id: string; value: string }
Expand Down Expand Up @@ -37,11 +38,13 @@
type="text"
class="form-control"
bind:value={item.value}
on:input={() => dispath('edit')} />
on:input={() => dispath('edit')}
/>
<div class="input-group-append">
<button
class="btn btn-secondary"
on:click={() => dispath('remove', item.id)}>
on:click={() => dispath('remove', item.id)}
>
<i class="fas fa-trash" />
</button>
</div>
Expand Down
27 changes: 14 additions & 13 deletions plugins/restricted-email-domains/assets/autocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { event } from 'blessing-skin'
globalThis.blessing.event.on(
'emailDomainsSuggestion',
(domains: Set<string>) => {
const allowListJSON = document.querySelector('#allowed-email-domains')
if (!allowListJSON) {
return
}

event.on('emailDomainsSuggestion', (domains: Set<string>) => {
const allowListJSON = document.querySelector('#allowed-email-domains')
if (!allowListJSON) {
return
}

const allowList: string[] = JSON.parse(allowListJSON.textContent ?? '[]')
if (allowList.length > 0) {
domains.clear()
allowList.forEach((domain) => domains.add(domain))
}
})
const allowList: string[] = JSON.parse(allowListJSON.textContent ?? '[]')
if (allowList.length > 0) {
domains.clear()
allowList.forEach((domain) => domains.add(domain))
}
},
)
13 changes: 7 additions & 6 deletions plugins/share-registration-link/assets/registration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { event } from 'blessing-skin'

event.on('beforeFetch', (request: { data: Record<string, string> }) => {
const search = new URLSearchParams(location.search)
request.data.share = search.get('share') || ''
})
globalThis.blessing.event.on(
'beforeFetch',
(request: { data: Record<string, string> }) => {
const search = new URLSearchParams(location.search)
request.data.share = search.get('share') || ''
},
)
46 changes: 25 additions & 21 deletions plugins/texture-description/assets/Description.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { onMount } from 'svelte'
import { fetch, t, notify } from 'blessing-skin'
const { fetch, t, notify } = globalThis.blessing
let description = ''
let raw = ''
Expand Down Expand Up @@ -51,23 +52,6 @@
}
</script>

<style>
description-content :global(h1),
description-content :global(h2) {
padding-bottom: 0.3rem;
border-bottom: 1px solid #eaecef;
}
description-content :global(blockquote) {
color: #aaa;
border-left: 0.3rem solid #aaa;
}
description-content :global(hr) {
border-top-width: 4px;
}
</style>

{#if canEdit || !isEmptyDescription}
<div class="card card-secondary">
<div class="card-header">
Expand All @@ -77,7 +61,8 @@
<button
class="btn btn-secondary btn-sm float-right"
title={t('texture-description.edit')}
on:click={editDescription}>
on:click={editDescription}
>
<i class="fas fa-edit" />
</button>
{/if}
Expand Down Expand Up @@ -107,7 +92,8 @@
<button
class="btn btn-primary"
disabled={isSubmitting || isLengthExceeded}
on:click={submitDescription}>
on:click={submitDescription}
>
{#if isSubmitting}
<span>
<i class="fas fa-sync fa-spin" />
Expand All @@ -117,11 +103,29 @@
<button
class="btn btn-secondary"
disabled={isSubmitting}
on:click={() => (isEditing = false)}>
on:click={() => (isEditing = false)}
>
{t('general.cancel')}
</button>
</div>
</div>
{/if}
</div>
{/if}

<style>
description-content :global(h1),
description-content :global(h2) {
padding-bottom: 0.3rem;
border-bottom: 1px solid #eaecef;
}
description-content :global(blockquote) {
color: #aaa;
border-left: 0.3rem solid #aaa;
}
description-content :global(hr) {
border-top-width: 4px;
}
</style>
3 changes: 2 additions & 1 deletion plugins/texture-description/assets/Description.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { render, fireEvent, waitFor } from '@testing-library/svelte'
import { tick } from 'svelte'
import { fetch, t } from 'blessing-skin'
import Description from './Description.svelte'

const { fetch, t } = globalThis.blessing

test('render description', async () => {
const spy = jest.spyOn(fetch, 'get').mockResolvedValue('<div id="md"></div>')
render(Description, { props: { tid: 1 } })
Expand Down
6 changes: 4 additions & 2 deletions plugins/texture-description/assets/UploadEditor.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { onMount, onDestroy } from 'svelte'
import { event, t } from 'blessing-skin'
const { event, t } = globalThis.blessing
let description = ''
export let maxLength = Infinity
Expand All @@ -18,7 +19,8 @@
id="description-editor"
class="form-control"
rows="9"
bind:value={description} />
bind:value={description}
/>
{#if description.length > maxLength}
<div class="alert alert-info mt-2">
{t('texture-description.exceeded', { max: maxLength })}
Expand Down
3 changes: 2 additions & 1 deletion plugins/texture-description/assets/UploadEditor.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { render, fireEvent } from '@testing-library/svelte'
import { tick } from 'svelte'
import { event, t } from 'blessing-skin'
import UploadEditor from './UploadEditor.svelte'

const { event, t } = globalThis.blessing

test('submit data', () => {
const { getByLabelText } = render(UploadEditor)
fireEvent.input(getByLabelText(t('texture-description.description')), {
Expand Down
3 changes: 1 addition & 2 deletions plugins/texture-description/assets/UploadEditor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { event } from 'blessing-skin'
import UploadEditor from './UploadEditor.svelte'

event.on('mounted', () => {
globalThis.blessing.event.on('mounted', () => {
const input = document.querySelector<HTMLInputElement>('#description-limit')
const maxLength = Number.parseInt(input?.value ?? '0') || Infinity

Expand Down
Loading

0 comments on commit d8c71c6

Please sign in to comment.