Skip to content

Commit

Permalink
Make stickerHandler ts and add sticker interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvNC committed Jul 24, 2023
1 parent 90a682c commit d6bac2d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 22 deletions.
41 changes: 19 additions & 22 deletions src/utils/stickerHandler.js → src/utils/stickerHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const path = require('path');
const { keyboard, Key } = require('@nut-tree/nut-js');
const Jimp = require('jimp');

let clipboardEx;
type clipboard = typeof import('electron-clipboard-ex')
let clipboardEx: clipboard | null = null;

// if not linux
if (process.platform !== 'linux') {
Expand All @@ -13,10 +14,10 @@ if (process.platform !== 'linux') {

/**
* Reads the sticker packs directory and returns a map of sticker pack objects.
* @returns {Array} Array of sticker pack objects
*/
function getAllStickerPacks(stickerPacksDir) {
const stickerPacksMap = {};
function getAllStickerPacks(stickerPacksDir: string) {
const stickerPacksMap: Record<string, StickerPack> = {};

// check if sticker packs directory exists, if not create it
if (!fs.existsSync(stickerPacksDir)) {
fs.mkdirSync(stickerPacksDir);
Expand All @@ -31,7 +32,7 @@ function getAllStickerPacks(stickerPacksDir) {
if (!fs.existsSync(path.join(stickerPacksDir, pack, 'info.json'))) {
fs.writeFileSync(path.join(stickerPacksDir, pack, 'info.json'), '{}');
}
const stickerPackData = JSON.parse(
const stickerPackData: StickerPack = JSON.parse(
fs.readFileSync(path.join(stickerPacksDir, pack, 'info.json'))
);

Expand All @@ -52,35 +53,31 @@ function getAllStickerPacks(stickerPacksDir) {
// read the rest of the stickers
const stickers = fs
.readdirSync(path.join(stickerPacksDir, pack))
.filter((file) => file !== 'info.json' && file !== 'main.png');
.filter((file: string) => file !== 'info.json' && file !== 'main.png');

stickerPackData.stickers = {};
const stickersMap: Record<string, Sticker> = {};

// handle animated/popup stickers (dumbly assume a static version exists)
const specialStickers = [];
for (const sticker of stickers) {
if (sticker.endsWith('_animation.png') || sticker.endsWith('_popup.png')) {
specialStickers.push(sticker);
} else {
const stickerID = path.parse(sticker).name;
const stickerID: string = path.parse(sticker).name;
const filepath = path.join(stickerPacksDir, pack, sticker);
const type = 'static';
stickerPackData.stickers[stickerID] = { filepath, type, stickerPackID: pack };
stickersMap[stickerID] = { filepath, type, stickerPackID: pack, stickerID };
}
}
for (const sticker of specialStickers) {
const stickerID = sticker.split('_')[0];
const filepath = path.join(stickerPacksDir, pack, sticker);
stickerPackData.stickers[stickerID].specialPath = filepath;
stickersMap[stickerID].specialPath = filepath;
const type = path.parse(sticker).name.split('_')[1];
stickerPackData.stickers[stickerID].type = type;
stickersMap[stickerID].type = type;
}
// convert stickers object to array
stickerPackData.stickers = Object.entries(stickerPackData.stickers).map(
([stickerID, sticker]) => {
return { stickerID, ...sticker };
}
);
// convert stickersMap object to array
stickerPackData.stickers = Object.values(stickersMap);
stickerPacksMap[pack] = stickerPackData;
}
return stickerPacksMap;
Expand All @@ -93,9 +90,9 @@ function getAllStickerPacks(stickerPacksDir) {
* @param {*} closeWindowAfterSend
*/
async function pasteStickerFromPath(
stickerPath,
window,
{ closeWindowAfterSend = true, resizeWidth, title = '', author = '', stickerPackID = '' } = {}
stickerPath: any,
window: { minimize: () => void; setAlwaysOnTop: (arg0: boolean) => void; setFocusable: (arg0: boolean) => void; },
{ closeWindowAfterSend = true, resizeWidth = 160, title = '', author = '', stickerPackID = '' } = {}
) {
// check valid file path
if (!fs.existsSync(stickerPath)) {
Expand Down Expand Up @@ -136,7 +133,7 @@ async function pasteStickerFromPath(

// write sticker file to clipboard if not linux
if (process.platform !== 'linux') {
clipboardEx.writeFilePaths([tempStickerPath]);
clipboardEx?.writeFilePaths([tempStickerPath]);
} else {
// linux
clipboard.writeImage(tempStickerPath);
Expand Down Expand Up @@ -173,7 +170,7 @@ async function pasteStickerFromPath(
* @param {string} string
* @returns {string} String with illegal characters removed
*/
function stripIllegalCharacters(string) {
function stripIllegalCharacters(string: string) {
return string.replace(/[/\\?%*:|"<>]/g, '');
}

Expand Down
73 changes: 73 additions & 0 deletions sticker.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Interface for a sticker in a sticker pack.F
*/
interface Sticker {
stickerID: string;
filepath: string;
/**
* The optional special path for the sticker if it is a popup or animation sticker.
*/
specialPath?: string;
type: "static" | "animation" | "popup";
stickerPackID: string;
}

/**
* Interface for a sticker pack.
*/
interface StickerPack {
title: string;
storeURL: string;
author: string;
authorURL: string;
id: string;
mainIcon: string;
stickers: Sticker[];
}

// const stickerPackData: StickerPack = {
// title: "つり目獣耳スタンプ",
// storeURL: "https://store.line.me/stickershop/product/1265244/ja",
// author: "甘城なつき",
// authorURL: "https://store.line.me/stickershop/author/95033/ja",
// id: "1265244",
// mainIcon: "C:\\Users\\rotmi\\Pictures\\Stickers\\1265244\\main.png",
// stickers: [
// {
// stickerID: "10753776",
// filepath: "C:\\Users\\rotmi\\Pictures\\Stickers\\1265244\\10753776.png",
// type: "static",
// stickerPackID: "1265244",
// },
// {
// stickerID: "10753777",
// filepath: "C:\\Users\\rotmi\\Pictures\\Stickers\\1265244\\10753777.png",
// type: "static",
// stickerPackID: "1265244",
// },
// ],
// };
// const stickerPackData2: StickerPack = {
// title: "すきがあふれる!かまちょなジャージちゃん",
// storeURL: "https://store.line.me/stickershop/product/23517680/ja",
// author: "ジョイネット",
// authorURL: "https://store.line.me/stickershop/author/18378/ja",
// id: "23517680",
// mainIcon: "C:\\Users\\rotmi\\Pictures\\Stickers\\23517680\\main.png",
// stickers: [
// {
// stickerID: "598625750",
// filepath: "C:\\Users\\rotmi\\Pictures\\Stickers\\23517680\\598625750.png",
// type: "popup",
// stickerPackID: "23517680",
// specialPath: "C:\\Users\\rotmi\\Pictures\\Stickers\\23517680\\598625750_popup.png"
// },
// {
// stickerID: "598625751",
// filepath: "C:\\Users\\rotmi\\Pictures\\Stickers\\23517680\\598625751.png",
// type: "popup",
// stickerPackID: "23517680",
// specialPath: "C:\\Users\\rotmi\\Pictures\\Stickers\\23517680\\598625751_popup.png"
// },
// ]
// }

0 comments on commit d6bac2d

Please sign in to comment.