Skip to content

Commit

Permalink
feat: change the way to modify the font
Browse files Browse the repository at this point in the history
  • Loading branch information
biuuu committed Jul 24, 2022
1 parent 6cdfb22 commit ffa22b6
Show file tree
Hide file tree
Showing 20 changed files with 47 additions and 115 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shinycolors-zh",
"version": "1.9.6",
"version": "1.10.1",
"main": "main.js",
"repository": "[email protected]:biuuu/ShinyColors.git",
"author": "biuuu",
Expand Down
21 changes: 3 additions & 18 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,9 @@ const config = {

const defaultConfig = Object.assign({}, config)

const fontList = ['yuanti', 'heiti', 'yuanti2']

const FONT = {
HEITI_JA: 'UDKakugo_SmallPr6-B',
HEITI_TRANS: `sczh-heiti,UDKakugo_SmallPr6-B`,
YUAN_JA: 'HummingStd-E',
YUAN_TRANS: `sczh-yuanti,HummingStd-E`
}

const _keys = ['origin', 'font1', 'font2', 'timeout', 'story', 'auto', 'bgm', 'dev']
const keys = DEV ? _keys.slice(1, _keys.length) : _keys

const setFont = () => {
FONT.HEITI_TRANS = `${fontList.includes(config.font2) ? 'sczh-' : ''}${config.font2},${FONT.HEITI_JA}`
FONT.YUAN_TRANS = `${fontList.includes(config.font1) ? 'sczh-' : ''}${config.font1},${FONT.YUAN_JA}`
}

const getLocalConfig = () => {
const str = localStorage.getItem('sczh:setting')
let setting = JSON.parse(str)
Expand All @@ -55,7 +41,6 @@ const getLocalConfig = () => {
}
})

setFont()
if (DEV & ENVIRONMENT === 'development') {
config.origin = 'http://localhost:15944'
}
Expand Down Expand Up @@ -96,7 +81,7 @@ const getConfigFromHash = () => {

const menuCommand = {
auto: {
on: '关闭机翻', off: '开启机翻', id: 0,
on: '关闭机翻', off: '开启机翻', id: 0,
callback: () => {
config.auto = config.auto !== 'off' ? 'off' : 'on'
}
Expand All @@ -117,7 +102,7 @@ const menuCommand = {
}
},
bgm: {
on: '关闭BGM后台播放', off: '开启BGM后台播放', id: 0,
on: '关闭BGM后台播放', off: '开启BGM后台播放', id: 0,
callback: () => {
config.bgm = config.bgm !== 'off' ? 'off' : 'on'
}
Expand Down Expand Up @@ -188,5 +173,5 @@ setAllGMMenuCommand()

window.addEventListener('hashchange', getConfigFromHash)

export { FONT, PREVIEW_COUNT, saveConfig }
export { PREVIEW_COUNT, saveConfig }
export default config
3 changes: 1 addition & 2 deletions src/modules/album/profile.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import getProfile from '../../store/profile'
import { getIdolName } from '../../store/name'
import { router } from '../request'
import tagText from '../../utils/tagText'

const transName = async (data) => {
const idolName = await getIdolName()
if (idolName.has(data.name)) {
data.name = tagText(idolName.get(data.name))
data.name = idolName.get(data.name)
}
if (idolName.has(data.firstName)) {
data.firstName = idolName.get(data.firstName)
Expand Down
1 change: 0 additions & 1 deletion src/modules/album/title.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { replaceWrap, log } from '../../utils/index'
import isString from 'lodash/isString'
import config from '../../config'
import { router } from '../request'
import tagText from '../../utils/tagText'

let titleMaps
let nameMap
Expand Down
3 changes: 1 addition & 2 deletions src/modules/api-comm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { replaceItem } from '../utils/replaceText'
import { log, makePromise } from '../utils/index'
import tagText from '../utils/tagText'
import { router } from './request'
import getCommApiData from '../store/api-comm'

Expand Down Expand Up @@ -65,7 +64,7 @@ const transApi = (type, ensureMoreData) => {
})
let text = arr.join('/')
if (text !== item[key]) {
item[key] = tagText(text, true)
item[key] = text
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/modules/comic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { router } from './request'
import tagText from '../utils/tagText'

let comicMap = null
const getInfo = async () => {
Expand Down Expand Up @@ -39,7 +38,7 @@ const transComicTitle = async (data) => {
if (comicMap.has(id)) {
const title = comicMap.get(id).title
if (title) {
item.title = tagText(title)
item.title = title
}
}
})
Expand Down
3 changes: 1 addition & 2 deletions src/modules/item.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { replaceItem } from '../utils/replaceText'
import tagText from '../utils/tagText'
import transApi from './api-comm'

const { api, getTransItem, ensureData } = transApi('item')
Expand All @@ -12,7 +11,7 @@ const transDesc = getTransItem((item, key, data) => {
})
let text = arr.join('\n')
if (text !== item[key]) {
item[key] = tagText(text, true)
item[key] = text
}
}
})
Expand Down
3 changes: 1 addition & 2 deletions src/modules/phrase.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import getPhrase from '../store/phrase'
import CSV from 'papaparse/papaparse.min'
import { replaceWrap, log } from '../utils/index'
import tagText from '../utils/tagText'
import { getModule } from './get-module'
let phraseMap = null

Expand Down Expand Up @@ -32,6 +31,6 @@ export default async function transPhrase () {
// }
phraseMap = await getPhrase()
for (let [key, value] of phraseMap) {
obj[key] = specialKey.includes(key) ? value : tagText(value)
obj[key] = value
}
}
5 changes: 2 additions & 3 deletions src/modules/produce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import getName from '../store/name'
import tagText from '../utils/tagText'
import { ensureTitle, saveTitle, transTitle } from './album/title'
import { router } from './request'

Expand All @@ -8,10 +7,10 @@ const produceIdolName = async (data) => {
if (data.userProduceIdol) {
const char = data.userProduceIdol.userIdol.idol.character
if (nameMap.has(char.name)) {
char.name = tagText(nameMap.get(char.name))
char.name = nameMap.get(char.name)
}
if (nameMap.has(char.firstName)) {
char.firstName = tagText(nameMap.get(char.firstName))
char.firstName = nameMap.get(char.firstName)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/modules/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { storyTitle } from './album/title'
import transSpeaker from './story/speaker'
import autoTrans from '../utils/translation'
import { requestLog } from './request'
import tagText from '../utils/tagText'
import { getModule } from './get-module'

const storyCache = {
Expand Down Expand Up @@ -84,7 +83,7 @@ const startTrans = (data, storyMap, commMap) => {
} else if (id && storyMap.has(`${id}`)) {
item.text = storyMap.get(`${id}`)
} else if (commMap.has(text)) {
item.text = tagText(commMap.get(text))
item.text = commMap.get(text)
}
}
if (item.select) {
Expand All @@ -93,7 +92,7 @@ const startTrans = (data, storyMap, commMap) => {
if (storyMap.has(sKey)) {
item.select = storyMap.get(sKey)
} else if (commMap.has(select)) {
item.select = tagText(commMap.get(item.select))
item.select = commMap.get(item.select)
}
}
})
Expand Down
8 changes: 3 additions & 5 deletions src/modules/story/speaker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { getModule } from '../get-module'
import getSpeakerIcon from '../../store/speakerIcon'
import getName from '../../store/name'
import { trim } from '../../utils/'
import tagText from '../../utils/tagText'

let namePromise = null
let iconMap = new Map()
Expand Down Expand Up @@ -34,7 +32,7 @@ Object.keys = new Proxy(originObjKeys, {
apply (target, self, args) {
if (checkSpeakerData(args, 'idol') || checkSpeakerData(args, 'staff')) {
for (let [id, name] of iconMap) {
let _name = tagText(name)
let _name = name
if (Array.isArray(args[0][id]) && !args[0][id].includes(_name)) {
args[0][id].push(_name)
}
Expand Down Expand Up @@ -67,14 +65,14 @@ const transSpeaker = async (item) => {
const nameMap = await ensureName()
let text = trim(item.speaker)
if (nameMap.has(text)) {
return item.speaker = tagText(nameMap.get(text))
return item.speaker = nameMap.get(text)
}
const sepList = ['&', '&']
sepList.forEach(sep => {
text = splitText(text, sep, nameMap)
})
if (text !== item.speaker) {
item.speaker = tagText(text)
item.speaker = text
}
}
}
Expand Down
45 changes: 9 additions & 36 deletions src/modules/text.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import isString from 'lodash/isString'
import getCommMap from '../store/commText'
import { FONT } from '../config'
import { log, fixWrap } from '../utils/index'
import getTypeTextMap from '../store/typeText'
import { getModule } from './get-module'
Expand All @@ -26,51 +24,26 @@ const isTyping = (text) => {
return typing
}

const replaceFont = (style) => {
if (style && style.fontFamily) {
if (style.fontFamily === FONT.HEITI_JA) {
Reflect.set(style, 'fontFamily', FONT.HEITI_TRANS)
} else if (style.fontFamily === FONT.YUAN_JA) {
Reflect.set(style, 'fontFamily', FONT.YUAN_TRANS)
}
}
}

const restoreFont = (style) => {
if (style && style.fontFamily) {
if (style.fontFamily === FONT.HEITI_TRANS) {
Reflect.set(style, 'fontFamily', FONT.HEITI_JA)
} else if (style.fontFamily === FONT.YUAN_TRANS) {
Reflect.set(style, 'fontFamily', FONT.YUAN_JA)
}
}
}

const textInMap = (text, map, style) => {
const replaceText = (text, map) => {
let _text = text
let key = fixWrap(text)
if (map.has(key)) {
_text = '\u200b' + map.get(key)
replaceFont(style)
} else if (!text.startsWith('\u200b')) {
restoreFont(style)
_text = map.get(key)
}
return _text
}

const fontCheck = (text, style, isType = false) => {
if (!isString(text)) return text
const checkText = (text, isType = false) => {
if (!typeof text !== 'string') return text
let _text = text
if (text.startsWith('\u200b')) {
replaceFont(style)
} else if (text.trim()) {
if (text.trim()) {
if (isType) {
_text = textInMap(text, typeTextMap, style)
_text = replaceText(text, typeTextMap)
if (_text === text) {
setTypeText(text)
}
} else if (!isTyping(text) && !text.startsWith('\u200c')) {
_text = textInMap(text, commMap, style)
_text = replaceText(text, commMap)
}
}
return _text
Expand All @@ -89,15 +62,15 @@ export default async function watchText () {
aoba.Text.prototype.typeText = function (...args) {
const text = args[0]
if (SHOW_UPDATE_TEXT) log('type text', ...args)
args[0] = fontCheck(text, this.style, true)
args[0] = checkText(text, true)
return originTypeText.apply(this, args)
}

const originUpdateText = aoba.Text.prototype.updateText
aoba.Text.prototype.updateText = function (t) {
if (this.localStyleID !== this._style.styleID && (this.dirty = !0,this._style.styleID),this.dirty || !t) {
if (config.dev && SHOW_UPDATE_TEXT) log('update text', this._text)
const value = fontCheck(this._text, this._style)
const value = checkText(this._text)
Reflect.set(this, '_text', value)
return originUpdateText.call(this, t)
}
Expand Down
5 changes: 2 additions & 3 deletions src/store/profile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getList } from './index'
import { trimWrap, trim } from '../utils/index'
import tagText from '../utils/tagText'

const profileMap = new Map()
let loaded = false
Expand All @@ -17,11 +16,11 @@ const getProfile = async () => {
const text = trimWrap(item.nameKana)
const arr = text.split('|')
profileMap.set(id, new Map(keys.map((key, idx) => {
return [key, tagText(trim(arr[idx]))]
return [key, trim(arr[idx])]
})))
} else {
for (let key in item) {
item[key] = tagText(trimWrap(item[key]))
item[key] = trimWrap(item[key])
}
profileMap.set(id, item)
}
Expand Down
7 changes: 3 additions & 4 deletions src/store/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fetchData from '../utils/fetch'
import parseCsv from '../utils/parseCsv'
import { getList, getLocalData, setLocalData } from './index'
import { trimWrap, trim, uniqueStoryId } from '../utils/index'
import tagText from '../utils/tagText'

const storyTemp = new Map()
const commStoryMap = new Map()
Expand All @@ -20,12 +19,12 @@ const getStoryMap = (csv) => {
const name = trim(item.name)
if (text && trans) {
if (id && !/^0+$/.test(id) && id !== 'select') {
storyMap.set(id, tagText(trans))
storyMap.set(id, trans)
} else {
if (id === 'select') {
storyMap.set(`${text}-select`, tagText(trans))
storyMap.set(`${text}-select`, trans)
} else {
storyMap.set(text, tagText(trans))
storyMap.set(text, trans)
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/utils/fontFace.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ const addFont = async () => {
const { hashes } = await getHash
tag.innerHTML = `
@font-face {
font-family: "sczh-heiti";
src: url("${config.origin}${getPath('heiti', hashes)}");
font-family: "UDKakugo_SmallPr6-B";
src: url("${config.origin}${getPath('heiti', hashes)}"),
url("/assets/fonts/primula-UDKakugo_SmallPr6-B.woff2?v=5b98f6814db5b7ff2e73b0bbbd48ebb3");
}
@font-face {
font-family: "sczh-yuanti";
src: url("${config.origin}${getPath('yuanti', hashes)}");
font-family: "HummingStd-E";
src: url("${config.origin}${getPath('yuanti', hashes)}"),
url("/assets/fonts/primula-HummingStd-E.woff2?v=57f0e9684ac32d5ed54c567c001eaaaa");
}
::-webkit-scrollbar {
display: none;
Expand All @@ -36,7 +38,7 @@ const addFont = async () => {
preload(`${config.origin}${getPath('heiti', hashes)}`)
}

document.head.appendChild(tag)
document.body.appendChild(tag)
}

export default addFont
Loading

0 comments on commit ffa22b6

Please sign in to comment.