Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.

Commit 555131a

Browse files
committed
Add TypeScript declaration file for swapy module
1 parent fbe4978 commit 555131a

43 files changed

Lines changed: 874 additions & 272 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package-lock.json

Lines changed: 10 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@
5656
"dependencies": {
5757
"@toast-ui/editor": "^3.2.2",
5858
"beercss": "^3.12.13",
59+
"easy-qrcode": "^0.1.0",
5960
"firebase": "^12.6.0",
6061
"flatpickr": "^4.6.13",
6162
"fuse.js": "^7.1.0",
6263
"idb": "^8.0.3",
63-
"easy-qrcode": "^0.1.0",
6464
"material-dynamic-colors": "^1.1.2",
65-
"material-file-icons": "^2.4.0"
65+
"material-file-icons": "^2.4.0",
66+
"swapy": "^1.0.5"
6667
},
6768
"devDependencies": {
6869
"@types/firebase": "^3.2.3",
@@ -81,4 +82,4 @@
8182
"webpack-dev-server": "^5.2.2",
8283
"workbox-webpack-plugin": "^7.3.0"
8384
}
84-
}
85+
}

src/api/worksheets-api.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { IWorksheet } from "@models/worksheet";
2+
3+
const WORKSHEET_API_ENDPOINT = "https://hbnitv.net/api/kuriki/worksheets";
4+
5+
export interface WorksheetRecord {
6+
data: IWorksheet;
7+
outcomes: string[];
8+
}
9+
10+
export interface WorksheetSingleResponse {
11+
status: string;
12+
data: WorksheetRecord;
13+
}
14+
15+
export interface WorksheetAllResponse {
16+
status: string;
17+
data: Record<number, WorksheetRecord>;
18+
}
19+
20+
export class WorksheetsAPI {
21+
static async getAll(): Promise<WorksheetAllResponse> {
22+
const res = await fetch(WORKSHEET_API_ENDPOINT);
23+
if (!res.ok) throw new Error(`WorksheetAPI GET all failed: ${res.statusText}`);
24+
return res.json();
25+
}
26+
27+
static async getById(id: number): Promise<WorksheetSingleResponse> {
28+
const res = await fetch(`${WORKSHEET_API_ENDPOINT}?id=${id}`);
29+
if (!res.ok) throw new Error(`WorksheetAPI GET by ID failed: ${res.statusText}`);
30+
return res.json();
31+
}
32+
33+
static async getByOutcome(outcomeId: string): Promise<WorksheetAllResponse> {
34+
const res = await fetch(`${WORKSHEET_API_ENDPOINT}?outcomeId=${encodeURIComponent(outcomeId)}`);
35+
if (!res.ok) throw new Error(`WorksheetAPI GET by outcomeId failed: ${res.statusText}`);
36+
return res.json();
37+
}
38+
39+
static async post(idKey: number, data: IWorksheet, outcomes: string[]) {
40+
const payload = { idKey, data, outcomes };
41+
42+
const res = await fetch(WORKSHEET_API_ENDPOINT, {
43+
method: "POST",
44+
headers: { "Content-Type": "application/json" },
45+
body: JSON.stringify(payload),
46+
});
47+
48+
if (!res.ok) throw new Error(`WorksheetAPI POST failed: ${res.statusText}`);
49+
return res.json();
50+
}
51+
52+
static async delete(idKey: number) {
53+
const url = `${WORKSHEET_API_ENDPOINT}?id=${idKey}`;
54+
const res = await fetch(url, { method: "DELETE" });
55+
56+
if (!res.ok) throw new Error(`WorksheetAPI DELETE failed: ${res.statusText}`);
57+
return res.json();
58+
}
59+
}

src/components/swapy/swapy-item.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export interface ISwapySlot {
2+
id: string;
3+
element: HTMLElement;
4+
}
5+
6+
export interface ISwapyItem {
7+
id: string;
8+
element: HTMLElement;
9+
}
10+
11+
12+
export class SwapyItem implements ISwapyItem {
13+
id: string;
14+
element: HTMLElement;
15+
16+
constructor(id: string, content: HTMLElement) {
17+
this.id = id;
18+
19+
const el = document.createElement("div");
20+
21+
el.setAttribute("data-swapy-item", `item-${id}`);
22+
el.appendChild(content);
23+
24+
this.element = el;
25+
}
26+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { createSwapy } from "swapy";
2+
import { SwapySlot } from "@components/swapy/swapy-slot";
3+
import { SwapyItem } from "@components/swapy/swapy-item";
4+
5+
export class SwapyManager {
6+
private static instance: SwapyManager | null = null;
7+
8+
private container: HTMLElement;
9+
private swapy: any;
10+
11+
private constructor(container: HTMLElement) {
12+
this.container = container;
13+
14+
this.swapy = createSwapy(this.container, {
15+
animation: "spring",
16+
swapMode: "hover",
17+
autoScrollOnDrag: true,
18+
});
19+
}
20+
21+
static init(container: HTMLElement) {
22+
if (!SwapyManager.instance) {
23+
SwapyManager.instance = new SwapyManager(container);
24+
}
25+
return SwapyManager.instance;
26+
}
27+
28+
static get() {
29+
if (!SwapyManager.instance) {
30+
throw new Error("SwapyManager not initialized yet.");
31+
}
32+
return SwapyManager.instance;
33+
}
34+
35+
createSlotWithItem(id: string, content: HTMLElement) {
36+
const item = new SwapyItem(id, content);
37+
const slot = new SwapySlot(id, item);
38+
39+
this.container.appendChild(slot.element);
40+
this.update();
41+
42+
return { slot, item };
43+
}
44+
45+
update() {
46+
queueMicrotask(() => {
47+
if (this.swapy && !this.swapy.isDragging) {
48+
this.swapy.update();
49+
}
50+
});
51+
}
52+
}

src/components/swapy/swapy-slot.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ISwapySlot, SwapyItem } from "@components/swapy/swapy-item";
2+
3+
export class SwapySlot implements ISwapySlot {
4+
id: string;
5+
element: HTMLElement;
6+
7+
constructor(id: string, item: SwapyItem) {
8+
this.id = id;
9+
10+
const slot = document.createElement("div");
11+
slot.classList.add("slot")
12+
slot.setAttribute("data-swapy-slot", `slot-${id}`);
13+
14+
slot.appendChild(item.element);
15+
16+
this.element = slot;
17+
}
18+
19+
setItem(item: SwapyItem) {
20+
this.element.innerHTML = "";
21+
this.element.appendChild(item.element);
22+
}
23+
}

0 commit comments

Comments
 (0)