-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: rewrite with mjs, refactor doc page
- Loading branch information
1 parent
cee5ed0
commit 843efb0
Showing
10 changed files
with
172 additions
and
97 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,34 @@ | ||
const data = require('./data.json') | ||
import data from './data.json' | ||
|
||
function get(id) { | ||
export { data } | ||
|
||
export function getFaceById(id) { | ||
id = String(id) | ||
return data.find((el) => el.QSid === id) | ||
} | ||
|
||
function getByText(input) { | ||
export function findFaceByInput(input) { | ||
if (input[0] === '/') input = input.substring(1) | ||
const match = data.find((el) => el.QDes.substring(1) === input) | ||
|| data.find((el) => el.Input.includes(input)) | ||
|| data.find((el) => el.Input.some((i) => i.startsWith(input))) | ||
const match = | ||
data.find((el) => el.QDes.substring(1) === input) || | ||
data.find((el) => el.Input.includes(input)) || | ||
data.find((el) => el.Input.some((i) => i.startsWith(input))) | ||
return match ? match.QSid : null | ||
} | ||
|
||
function getUrl(id, base = 'https://koishi.js.org/QFace') { | ||
const item = get(id) | ||
export function getFaceUrl(id, baseURL = 'https://koishi.js.org/QFace/') { | ||
const item = getFaceById(id) | ||
if (!item) return '' | ||
return base + (item.isStatic ? `/static/s${id}.png` : `/gif/s${id}.gif`) | ||
const url = new URL( | ||
item.isStatic ? `static/s${id}.png` : `gif/s${id}.gif`, | ||
baseURL | ||
) | ||
return url.href | ||
} | ||
|
||
module.exports = { | ||
data, | ||
get, | ||
getUrl, | ||
getByText, | ||
// alias | ||
export { | ||
getFaceById as get, | ||
findFaceByInput as getByText, | ||
getFaceUrl as getUrl, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
;(async () => { | ||
/** @type {HTMLElement} */ | ||
const container = document.getElementById('face-container') | ||
/** @type {HTMLElement} */ | ||
const placeholder = document.getElementById('placeholder') | ||
/** @type {import('../lib').Face[]} */ | ||
const list = await fetch('./lib/data.json').then((res) => res.json()) | ||
|
||
placeholder.style.display = 'none' | ||
|
||
list | ||
.sort((a, b) => Number(a.QSid) - Number(b.QSid)) | ||
.forEach((item) => { | ||
const { QSid, QDes, isStatic } = item | ||
|
||
const src = isStatic ? `static/s${QSid}.png` : `gif/s${QSid}.gif` | ||
const faceImg = document.createElement('img') | ||
faceImg.src = src | ||
faceImg.className = 'face-img' | ||
faceImg.loading = 'lazy' | ||
faceImg.onerror = function () { | ||
const div = document.createElement('div') | ||
div.className = 'face-img' | ||
div.textContent = '?' | ||
this.replaceWith(div) | ||
} | ||
|
||
const faceId = document.createElement('div') | ||
faceId.className = 'face-id' | ||
faceId.textContent = QSid | ||
|
||
const faceDesc = document.createElement('div') | ||
faceDesc.className = 'face-desc' | ||
faceDesc.textContent = QDes.replace(/^\//, '') | ||
|
||
const link = document.createElement('a') | ||
link.href = src | ||
link.target = '_blank' | ||
link.className = 'face' | ||
link.append(faceImg, faceDesc, faceId) | ||
|
||
Object.entries(item, ([key, val]) => { | ||
link.setAttribute(`data-${key}`, val) | ||
}) | ||
|
||
container.append(link) | ||
}) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
html, | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
font-size: 14px; | ||
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, | ||
Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; | ||
color: #0f172a; | ||
} | ||
|
||
h1 { | ||
text-align: center; | ||
} | ||
|
||
#container { | ||
padding: 1rem; | ||
max-width: 1200px; | ||
margin: 0 auto; | ||
} | ||
|
||
#face-container { | ||
display: grid; | ||
grid-template-columns: repeat(auto-fill, minmax(6rem, 1fr)); | ||
gap: 1rem; | ||
} | ||
|
||
#face-container .face { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
border: 1px solid #e28a00; | ||
background-color: #fefefe; | ||
width: 6rem; | ||
padding: 0.5rem; | ||
border-radius: 0.5rem; | ||
transition: all 0.25s; | ||
cursor: pointer; | ||
color: #0f172a; | ||
text-decoration: none; | ||
} | ||
|
||
#face-container .face:hover { | ||
box-shadow: 0 0 0 4px #feda83; | ||
} | ||
|
||
#face-container .face .face-img { | ||
width: 4rem; | ||
height: 4rem; | ||
line-height: 0; | ||
font-size: 4rem; | ||
display: inline-flex; | ||
justify-content: center; | ||
align-items: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { readFile, writeFile } from 'node:fs/promises' | ||
import { resolve } from 'node:path' | ||
|
||
const dataJsonPath = resolve(import.meta.dirname, '../lib/data.json') | ||
|
||
/** @type {import('../lib').Face[]} */ | ||
const data = JSON.parse((await readFile(dataJsonPath)).toString('utf-8')) | ||
|
||
writeFile( | ||
dataJsonPath, | ||
JSON.stringify( | ||
data.sort((a, b) => ~~a.QSid - ~~b.QSid), | ||
null, | ||
2 | ||
) | ||
) |