From 100a13191340399abc50f4c597f81af4cbf971b4 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sat, 30 Jan 2021 10:59:05 +0900 Subject: pages refactoring, fix bug (#7066) * pages refactoring * pages: fix if block * fix code format * remove passing of the page parameter * remove comment * fix indent * replace with unref * fix conditions of isVarBlock() * Update src/client/scripts/hpml/block.ts use includes() instead of find() Co-authored-by: syuilo Co-authored-by: syuilo --- src/client/scripts/hpml/block.ts | 109 ++++++++++++++++++++++++++++++++ src/client/scripts/hpml/evaluator.ts | 89 ++++++++++++++------------ src/client/scripts/hpml/expr.ts | 79 +++++++++++++++++++++++ src/client/scripts/hpml/index.ts | 88 +------------------------- src/client/scripts/hpml/lib.ts | 80 ++++++++++++++++++++--- src/client/scripts/hpml/type-checker.ts | 18 +++--- 6 files changed, 317 insertions(+), 146 deletions(-) create mode 100644 src/client/scripts/hpml/block.ts create mode 100644 src/client/scripts/hpml/expr.ts (limited to 'src/client/scripts') diff --git a/src/client/scripts/hpml/block.ts b/src/client/scripts/hpml/block.ts new file mode 100644 index 0000000000..804c5c1124 --- /dev/null +++ b/src/client/scripts/hpml/block.ts @@ -0,0 +1,109 @@ +// blocks + +export type BlockBase = { + id: string; + type: string; +}; + +export type TextBlock = BlockBase & { + type: 'text'; + text: string; +}; + +export type SectionBlock = BlockBase & { + type: 'section'; + title: string; + children: (Block | VarBlock)[]; +}; + +export type ImageBlock = BlockBase & { + type: 'image'; + fileId: string | null; +}; + +export type ButtonBlock = BlockBase & { + type: 'button'; + text: any; + primary: boolean; + action: string; + content: string; + event: string; + message: string; + var: string; + fn: string; +}; + +export type IfBlock = BlockBase & { + type: 'if'; + var: string; + children: Block[]; +}; + +export type TextareaBlock = BlockBase & { + type: 'textarea'; + text: string; +}; + +export type PostBlock = BlockBase & { + type: 'post'; + text: string; + attachCanvasImage: boolean; + canvasId: string; +}; + +export type CanvasBlock = BlockBase & { + type: 'canvas'; + name: string; // canvas id + width: number; + height: number; +}; + +export type NoteBlock = BlockBase & { + type: 'note'; + detailed: boolean; + note: string | null; +}; + +export type Block = + TextBlock | SectionBlock | ImageBlock | ButtonBlock | IfBlock | TextareaBlock | PostBlock | CanvasBlock | NoteBlock | VarBlock; + +// variable blocks + +export type VarBlockBase = BlockBase & { + name: string; +}; + +export type NumberInputVarBlock = VarBlockBase & { + type: 'numberInput'; + text: string; +}; + +export type TextInputVarBlock = VarBlockBase & { + type: 'textInput'; + text: string; +}; + +export type SwitchVarBlock = VarBlockBase & { + type: 'switch'; + text: string; +}; + +export type RadioButtonVarBlock = VarBlockBase & { + type: 'radioButton'; + title: string; + values: string[]; +}; + +export type CounterVarBlock = VarBlockBase & { + type: 'counter'; + text: string; + inc: number; +}; + +export type VarBlock = + NumberInputVarBlock | TextInputVarBlock | SwitchVarBlock | RadioButtonVarBlock | CounterVarBlock; + +const varBlock = ['numberInput', 'textInput', 'switch', 'radioButton', 'counter']; +export function isVarBlock(block: Block): block is VarBlock { + return varBlock.includes(block.type); +} diff --git a/src/client/scripts/hpml/evaluator.ts b/src/client/scripts/hpml/evaluator.ts index 4fa95e89c9..20261d333d 100644 --- a/src/client/scripts/hpml/evaluator.ts +++ b/src/client/scripts/hpml/evaluator.ts @@ -1,12 +1,13 @@ import autobind from 'autobind-decorator'; -import { Variable, PageVar, envVarsDef, Block, isFnBlock, Fn, HpmlScope, HpmlError } from '.'; +import { PageVar, envVarsDef, Fn, HpmlScope, HpmlError } from '.'; import { version } from '@/config'; import { AiScript, utils, values } from '@syuilo/aiscript'; import { createAiScriptEnv } from '../aiscript/api'; import { collectPageVars } from '../collect-page-vars'; import { initHpmlLib, initAiLib } from './lib'; import * as os from '@/os'; -import { markRaw, ref, Ref } from 'vue'; +import { markRaw, ref, Ref, unref } from 'vue'; +import { Expr, isLiteralValue, Variable } from './expr'; /** * Hpml evaluator @@ -94,7 +95,7 @@ export class Hpml { public interpolate(str: string) { if (str == null) return null; return str.replace(/{(.+?)}/g, match => { - const v = this.vars[match.slice(1, -1).trim()]; + const v = unref(this.vars)[match.slice(1, -1).trim()]; return v == null ? 'NULL' : v.toString(); }); } @@ -158,72 +159,76 @@ export class Hpml { } @autobind - private evaluate(block: Block, scope: HpmlScope): any { - if (block.type === null) { - return null; - } + private evaluate(expr: Expr, scope: HpmlScope): any { - if (block.type === 'number') { - return parseInt(block.value, 10); - } + if (isLiteralValue(expr)) { + if (expr.type === null) { + return null; + } - if (block.type === 'text' || block.type === 'multiLineText') { - return this._interpolateScope(block.value || '', scope); - } + if (expr.type === 'number') { + return parseInt((expr.value as any), 10); + } - if (block.type === 'textList') { - return this._interpolateScope(block.value || '', scope).trim().split('\n'); - } + if (expr.type === 'text' || expr.type === 'multiLineText') { + return this._interpolateScope(expr.value || '', scope); + } - if (block.type === 'ref') { - return scope.getState(block.value); - } + if (expr.type === 'textList') { + return this._interpolateScope(expr.value || '', scope).trim().split('\n'); + } + + if (expr.type === 'ref') { + return scope.getState(expr.value); + } - if (block.type === 'aiScriptVar') { - if (this.aiscript) { - try { - return utils.valToJs(this.aiscript.scope.get(block.value)); - } catch (e) { + if (expr.type === 'aiScriptVar') { + if (this.aiscript) { + try { + return utils.valToJs(this.aiscript.scope.get(expr.value)); + } catch (e) { + return null; + } + } else { return null; } - } else { - return null; } - } - // Define user function - if (isFnBlock(block)) { - return { - slots: block.value.slots.map(x => x.name), - exec: (slotArg: Record) => { - return this.evaluate(block.value.expression, scope.createChildScope(slotArg, block.id)); - } - } as Fn; + // Define user function + if (expr.type == 'fn') { + return { + slots: expr.value.slots.map(x => x.name), + exec: (slotArg: Record) => { + return this.evaluate(expr.value.expression, scope.createChildScope(slotArg, expr.id)); + } + } as Fn; + } + return; } // Call user function - if (block.type.startsWith('fn:')) { - const fnName = block.type.split(':')[1]; + if (expr.type.startsWith('fn:')) { + const fnName = expr.type.split(':')[1]; const fn = scope.getState(fnName); const args = {} as Record; for (let i = 0; i < fn.slots.length; i++) { const name = fn.slots[i]; - args[name] = this.evaluate(block.args[i], scope); + args[name] = this.evaluate(expr.args[i], scope); } return fn.exec(args); } - if (block.args === undefined) return null; + if (expr.args === undefined) return null; - const funcs = initHpmlLib(block, scope, this.opts.randomSeed, this.opts.visitor); + const funcs = initHpmlLib(expr, scope, this.opts.randomSeed, this.opts.visitor); // Call function - const fnName = block.type; + const fnName = expr.type; const fn = (funcs as any)[fnName]; if (fn == null) { throw new HpmlError(`No such function '${fnName}'`); } else { - return fn(...block.args.map(x => this.evaluate(x, scope))); + return fn(...expr.args.map(x => this.evaluate(x, scope))); } } } diff --git a/src/client/scripts/hpml/expr.ts b/src/client/scripts/hpml/expr.ts new file mode 100644 index 0000000000..00e3ed118b --- /dev/null +++ b/src/client/scripts/hpml/expr.ts @@ -0,0 +1,79 @@ +import { literalDefs, Type } from '.'; + +export type ExprBase = { + id: string; +}; + +// value + +export type EmptyValue = ExprBase & { + type: null; + value: null; +}; + +export type TextValue = ExprBase & { + type: 'text'; + value: string; +}; + +export type MultiLineTextValue = ExprBase & { + type: 'multiLineText'; + value: string; +}; + +export type TextListValue = ExprBase & { + type: 'textList'; + value: string; +}; + +export type NumberValue = ExprBase & { + type: 'number'; + value: number; +}; + +export type RefValue = ExprBase & { + type: 'ref'; + value: string; // value is variable name +}; + +export type AiScriptRefValue = ExprBase & { + type: 'aiScriptVar'; + value: string; // value is variable name +}; + +export type UserFnValue = ExprBase & { + type: 'fn'; + value: UserFnInnerValue; +}; +type UserFnInnerValue = { + slots: { + name: string; + type: Type; + }[]; + expression: Expr; +}; + +export type Value = + EmptyValue | TextValue | MultiLineTextValue | TextListValue | NumberValue | RefValue | AiScriptRefValue | UserFnValue; + +export function isLiteralValue(expr: Expr): expr is Value { + if (expr.type == null) return true; + if (literalDefs[expr.type]) return true; + return false; +} + +// call function + +export type CallFn = ExprBase & { // "fn:hoge" or string + type: string; + args: Expr[]; + value: null; +}; + +// variable +export type Variable = (Value | CallFn) & { + name: string; +}; + +// expression +export type Expr = Variable | Value | CallFn; diff --git a/src/client/scripts/hpml/index.ts b/src/client/scripts/hpml/index.ts index fa34b25d8d..924cd32eb5 100644 --- a/src/client/scripts/hpml/index.ts +++ b/src/client/scripts/hpml/index.ts @@ -3,52 +3,16 @@ */ import autobind from 'autobind-decorator'; - import { faMagic, faSquareRootAlt, faAlignLeft, - faShareAlt, - faPlus, - faMinus, - faTimes, - faDivide, faList, faQuoteRight, - faEquals, - faGreaterThan, - faLessThan, - faGreaterThanEqual, - faLessThanEqual, - faNotEqual, - faDice, faSortNumericUp, - faExchangeAlt, - faRecycle, - faIndent, - faCalculator, } from '@fortawesome/free-solid-svg-icons'; -import { faFlag } from '@fortawesome/free-regular-svg-icons'; import { Hpml } from './evaluator'; - -export type Block = { - id: string; - type: string; - args: Block[]; - value: V; -}; - -export type FnBlock = Block<{ - slots: { - name: string; - type: Type; - }[]; - expression: Block; -}>; - -export type Variable = Block & { - name: string; -}; +import { funcDefs } from './lib'; export type Fn = { slots: string[]; @@ -57,46 +21,6 @@ export type Fn = { export type Type = 'string' | 'number' | 'boolean' | 'stringArray' | null; -export const funcDefs: Record = { - if: { in: ['boolean', 0, 0], out: 0, category: 'flow', icon: faShareAlt, }, - for: { in: ['number', 'function'], out: null, category: 'flow', icon: faRecycle, }, - not: { in: ['boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, - or: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, - and: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, - add: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faPlus, }, - subtract: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faMinus, }, - multiply: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faTimes, }, - divide: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, }, - mod: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, }, - round: { in: ['number'], out: 'number', category: 'operation', icon: faCalculator, }, - eq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faEquals, }, - notEq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faNotEqual, }, - gt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThan, }, - lt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThan, }, - gtEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThanEqual, }, - ltEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThanEqual, }, - strLen: { in: ['string'], out: 'number', category: 'text', icon: faQuoteRight, }, - strPick: { in: ['string', 'number'], out: 'string', category: 'text', icon: faQuoteRight, }, - strReplace: { in: ['string', 'string', 'string'], out: 'string', category: 'text', icon: faQuoteRight, }, - strReverse: { in: ['string'], out: 'string', category: 'text', icon: faQuoteRight, }, - join: { in: ['stringArray', 'string'], out: 'string', category: 'text', icon: faQuoteRight, }, - stringToNumber: { in: ['string'], out: 'number', category: 'convert', icon: faExchangeAlt, }, - numberToString: { in: ['number'], out: 'string', category: 'convert', icon: faExchangeAlt, }, - splitStrByLine: { in: ['string'], out: 'stringArray', category: 'convert', icon: faExchangeAlt, }, - pick: { in: [null, 'number'], out: null, category: 'list', icon: faIndent, }, - listLen: { in: [null], out: 'number', category: 'list', icon: faIndent, }, - rannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, }, - dailyRannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, }, - seedRannum: { in: [null, 'number', 'number'], out: 'number', category: 'random', icon: faDice, }, - random: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, }, - dailyRandom: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, }, - seedRandom: { in: [null, 'number'], out: 'boolean', category: 'random', icon: faDice, }, - randomPick: { in: [0], out: 0, category: 'random', icon: faDice, }, - dailyRandomPick: { in: [0], out: 0, category: 'random', icon: faDice, }, - seedRandomPick: { in: [null, 0], out: 0, category: 'random', icon: faDice, }, - DRPWPM: { in: ['stringArray'], out: 'string', category: 'random', icon: faDice, }, // dailyRandomPickWithProbabilityMapping -}; - export const literalDefs: Record = { text: { out: 'string', category: 'value', icon: faQuoteRight, }, multiLineText: { out: 'string', category: 'value', icon: faAlignLeft, }, @@ -116,10 +40,6 @@ export const blockDefs = [ })) ]; -export function isFnBlock(block: Block): block is FnBlock { - return block.type === 'fn'; -} - export type PageVar = { name: string; value: any; type: Type; }; export const envVarsDef: Record = { @@ -140,12 +60,6 @@ export const envVarsDef: Record = { NULL: null, }; -export function isLiteralBlock(v: Block) { - if (v.type === null) return true; - if (literalDefs[v.type]) return true; - return false; -} - export class HpmlScope { private layerdStates: Record[]; public name: string; diff --git a/src/client/scripts/hpml/lib.ts b/src/client/scripts/hpml/lib.ts index 11e4f2fc39..7454562184 100644 --- a/src/client/scripts/hpml/lib.ts +++ b/src/client/scripts/hpml/lib.ts @@ -2,9 +2,31 @@ import * as tinycolor from 'tinycolor2'; import Chart from 'chart.js'; import { Hpml } from './evaluator'; import { values, utils } from '@syuilo/aiscript'; -import { Block, Fn, HpmlScope } from '.'; +import { Fn, HpmlScope } from '.'; +import { Expr } from './expr'; import * as seedrandom from 'seedrandom'; +import { + faShareAlt, + faPlus, + faMinus, + faTimes, + faDivide, + faQuoteRight, + faEquals, + faGreaterThan, + faLessThan, + faGreaterThanEqual, + faLessThanEqual, + faNotEqual, + faDice, + faExchangeAlt, + faRecycle, + faIndent, + faCalculator, +} from '@fortawesome/free-solid-svg-icons'; +import { faFlag } from '@fortawesome/free-regular-svg-icons'; + // https://stackoverflow.com/questions/38493564/chart-area-background-color-chartjs Chart.pluginService.register({ beforeDraw: (chart, easing) => { @@ -125,7 +147,47 @@ export function initAiLib(hpml: Hpml) { }; } -export function initHpmlLib(block: Block, scope: HpmlScope, randomSeed: string, visitor?: any) { +export const funcDefs: Record = { + if: { in: ['boolean', 0, 0], out: 0, category: 'flow', icon: faShareAlt, }, + for: { in: ['number', 'function'], out: null, category: 'flow', icon: faRecycle, }, + not: { in: ['boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, + or: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, + and: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, + add: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faPlus, }, + subtract: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faMinus, }, + multiply: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faTimes, }, + divide: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, }, + mod: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, }, + round: { in: ['number'], out: 'number', category: 'operation', icon: faCalculator, }, + eq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faEquals, }, + notEq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faNotEqual, }, + gt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThan, }, + lt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThan, }, + gtEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThanEqual, }, + ltEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThanEqual, }, + strLen: { in: ['string'], out: 'number', category: 'text', icon: faQuoteRight, }, + strPick: { in: ['string', 'number'], out: 'string', category: 'text', icon: faQuoteRight, }, + strReplace: { in: ['string', 'string', 'string'], out: 'string', category: 'text', icon: faQuoteRight, }, + strReverse: { in: ['string'], out: 'string', category: 'text', icon: faQuoteRight, }, + join: { in: ['stringArray', 'string'], out: 'string', category: 'text', icon: faQuoteRight, }, + stringToNumber: { in: ['string'], out: 'number', category: 'convert', icon: faExchangeAlt, }, + numberToString: { in: ['number'], out: 'string', category: 'convert', icon: faExchangeAlt, }, + splitStrByLine: { in: ['string'], out: 'stringArray', category: 'convert', icon: faExchangeAlt, }, + pick: { in: [null, 'number'], out: null, category: 'list', icon: faIndent, }, + listLen: { in: [null], out: 'number', category: 'list', icon: faIndent, }, + rannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, }, + dailyRannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, }, + seedRannum: { in: [null, 'number', 'number'], out: 'number', category: 'random', icon: faDice, }, + random: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, }, + dailyRandom: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, }, + seedRandom: { in: [null, 'number'], out: 'boolean', category: 'random', icon: faDice, }, + randomPick: { in: [0], out: 0, category: 'random', icon: faDice, }, + dailyRandomPick: { in: [0], out: 0, category: 'random', icon: faDice, }, + seedRandomPick: { in: [null, 0], out: 0, category: 'random', icon: faDice, }, + DRPWPM: { in: ['stringArray'], out: 'string', category: 'random', icon: faDice, }, // dailyRandomPickWithProbabilityMapping +}; + +export function initHpmlLib(expr: Expr, scope: HpmlScope, randomSeed: string, visitor?: any) { const date = new Date(); const day = `${visitor ? visitor.id : ''} ${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`; @@ -166,12 +228,12 @@ export function initHpmlLib(block: Block, scope: HpmlScope, randomSeed: string, splitStrByLine: (a: string) => a.split('\n'), pick: (list: any[], i: number) => list[i - 1], listLen: (list: any[]) => list.length, - random: (probability: number) => Math.floor(seedrandom(`${randomSeed}:${block.id}`)() * 100) < probability, - rannum: (min: number, max: number) => min + Math.floor(seedrandom(`${randomSeed}:${block.id}`)() * (max - min + 1)), - randomPick: (list: any[]) => list[Math.floor(seedrandom(`${randomSeed}:${block.id}`)() * list.length)], - dailyRandom: (probability: number) => Math.floor(seedrandom(`${day}:${block.id}`)() * 100) < probability, - dailyRannum: (min: number, max: number) => min + Math.floor(seedrandom(`${day}:${block.id}`)() * (max - min + 1)), - dailyRandomPick: (list: any[]) => list[Math.floor(seedrandom(`${day}:${block.id}`)() * list.length)], + random: (probability: number) => Math.floor(seedrandom(`${randomSeed}:${expr.id}`)() * 100) < probability, + rannum: (min: number, max: number) => min + Math.floor(seedrandom(`${randomSeed}:${expr.id}`)() * (max - min + 1)), + randomPick: (list: any[]) => list[Math.floor(seedrandom(`${randomSeed}:${expr.id}`)() * list.length)], + dailyRandom: (probability: number) => Math.floor(seedrandom(`${day}:${expr.id}`)() * 100) < probability, + dailyRannum: (min: number, max: number) => min + Math.floor(seedrandom(`${day}:${expr.id}`)() * (max - min + 1)), + dailyRandomPick: (list: any[]) => list[Math.floor(seedrandom(`${day}:${expr.id}`)() * list.length)], seedRandom: (seed: any, probability: number) => Math.floor(seedrandom(seed)() * 100) < probability, seedRannum: (seed: any, min: number, max: number) => min + Math.floor(seedrandom(seed)() * (max - min + 1)), seedRandomPick: (seed: any, list: any[]) => list[Math.floor(seedrandom(seed)() * list.length)], @@ -185,7 +247,7 @@ export function initHpmlLib(block: Block, scope: HpmlScope, randomSeed: string, totalFactor += factor; xs.push({ factor, text }); } - const r = seedrandom(`${day}:${block.id}`)() * totalFactor; + const r = seedrandom(`${day}:${expr.id}`)() * totalFactor; let stackedFactor = 0; for (const x of xs) { if (r >= stackedFactor && r <= stackedFactor + x.factor) { diff --git a/src/client/scripts/hpml/type-checker.ts b/src/client/scripts/hpml/type-checker.ts index 14950e0195..9633b3cd01 100644 --- a/src/client/scripts/hpml/type-checker.ts +++ b/src/client/scripts/hpml/type-checker.ts @@ -1,5 +1,7 @@ import autobind from 'autobind-decorator'; -import { Type, Block, funcDefs, envVarsDef, Variable, PageVar, isLiteralBlock } from '.'; +import { Type, envVarsDef, PageVar } from '.'; +import { Expr, isLiteralValue, Variable } from './expr'; +import { funcDefs } from './lib'; type TypeError = { arg: number; @@ -20,10 +22,10 @@ export class HpmlTypeChecker { } @autobind - public typeCheck(v: Block): TypeError | null { - if (isLiteralBlock(v)) return null; + public typeCheck(v: Expr): TypeError | null { + if (isLiteralValue(v)) return null; - const def = funcDefs[v.type]; + const def = funcDefs[v.type || '']; if (def == null) { throw new Error('Unknown type: ' + v.type); } @@ -58,8 +60,8 @@ export class HpmlTypeChecker { } @autobind - public getExpectedType(v: Block, slot: number): Type { - const def = funcDefs[v.type]; + public getExpectedType(v: Expr, slot: number): Type { + const def = funcDefs[v.type || '']; if (def == null) { throw new Error('Unknown type: ' + v.type); } @@ -86,7 +88,7 @@ export class HpmlTypeChecker { } @autobind - public infer(v: Block): Type { + public infer(v: Expr): Type { if (v.type === null) return null; if (v.type === 'text') return 'string'; if (v.type === 'multiLineText') return 'string'; @@ -103,7 +105,7 @@ export class HpmlTypeChecker { return pageVar.type; } - const envVar = envVarsDef[v.value]; + const envVar = envVarsDef[v.value || '']; if (envVar !== undefined) { return envVar; } -- cgit v1.2.3-freya From 40bfa3ef0407f83484031bfe74dcecb149c202a0 Mon Sep 17 00:00:00 2001 From: tamaina Date: Sat, 6 Feb 2021 18:55:53 +0900 Subject: Resurrect Service Worker (#7108) * Resolve #7106 * fix lint * fix lint * save lang in idb * fix lint * fix * cache locale file * fix lint * :v: * wip * fix [wip] * fix [wip] Co-authored-by: syuilo --- package.json | 2 +- src/client/i18n.ts | 45 +----------------- src/client/init.ts | 3 +- src/client/scripts/i18n.ts | 44 +++++++++++++++++ src/client/scripts/initialize-sw.ts | 68 ++++++++++++++++++++++++++ src/client/sw/compose-notification.ts | 13 ++++- src/client/sw/i18n.ts | 5 -- src/client/sw/sw.ts | 89 ++++++++++++++++++++++++++++++++--- src/misc/get-notification-summary.ts | 29 ------------ src/server/web/boot.js | 3 +- src/server/web/index.ts | 4 +- yarn.lock | 10 ++-- 12 files changed, 217 insertions(+), 98 deletions(-) create mode 100644 src/client/scripts/i18n.ts create mode 100644 src/client/scripts/initialize-sw.ts delete mode 100644 src/client/sw/i18n.ts delete mode 100644 src/misc/get-notification-summary.ts (limited to 'src/client/scripts') diff --git a/package.json b/package.json index 64cefa16b5..569ed26ea1 100644 --- a/package.json +++ b/package.json @@ -130,7 +130,6 @@ "css-loader": "5.0.1", "cssnano": "4.1.10", "dateformat": "4.5.1", - "deep-entries": "3.1.0", "diskusage": "1.1.3", "double-ended-queue": "2.1.0-0", "escape-regexp": "0.0.1", @@ -155,6 +154,7 @@ "http-proxy-agent": "4.0.1", "http-signature": "1.3.5", "https-proxy-agent": "5.0.0", + "idb-keyval": "5.0.1", "insert-text-at-cursor": "0.3.0", "is-root": "2.1.0", "is-svg": "4.2.1", diff --git a/src/client/i18n.ts b/src/client/i18n.ts index aeecb58a3e..fbc10a0bad 100644 --- a/src/client/i18n.ts +++ b/src/client/i18n.ts @@ -1,49 +1,6 @@ import { markRaw } from 'vue'; import { locale } from '@/config'; - -export class I18n> { - public locale: T; - - constructor(locale: T) { - this.locale = locale; - - if (_DEV_) { - console.log('i18n', this.locale); - } - - //#region BIND - this.t = this.t.bind(this); - //#endregion - } - - // string にしているのは、ドット区切りでのパス指定を許可するため - // なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも - public t(key: string, args?: Record): string { - try { - let str = key.split('.').reduce((o, i) => o[i], this.locale) as string; - - if (_DEV_) { - if (!str.includes('{')) { - console.warn(`i18n: '${key}' has no any arg. so ref prop directly instead of call this method.`); - } - } - - if (args) { - for (const [k, v] of Object.entries(args)) { - str = str.replace(`{${k}}`, v); - } - } - return str; - } catch (e) { - if (_DEV_) { - console.warn(`missing localization '${key}'`); - return `⚠'${key}'⚠`; - } - - return key; - } - } -} +import { I18n } from '@/scripts/i18n'; export const i18n = markRaw(new I18n(locale)); diff --git a/src/client/init.ts b/src/client/init.ts index f329d22251..17feca4c8b 100644 --- a/src/client/init.ts +++ b/src/client/init.ts @@ -57,6 +57,7 @@ import { fetchInstance, instance } from '@/instance'; import { makeHotkey } from './scripts/hotkey'; import { search } from './scripts/search'; import { getThemes } from './theme-store'; +import { initializeSw } from './scripts/initialize-sw'; console.info(`Misskey v${version}`); @@ -171,7 +172,7 @@ fetchInstance().then(() => { localStorage.setItem('v', instance.version); // Init service worker - //if (this.store.state.instance.meta.swPublickey) this.registerSw(this.store.state.instance.meta.swPublickey); + initializeSw(); }); stream.init($i); diff --git a/src/client/scripts/i18n.ts b/src/client/scripts/i18n.ts new file mode 100644 index 0000000000..d535e236bb --- /dev/null +++ b/src/client/scripts/i18n.ts @@ -0,0 +1,44 @@ +// Notice: Service Workerでも使用します +export class I18n> { + public locale: T; + + constructor(locale: T) { + this.locale = locale; + + if (_DEV_) { + console.log('i18n', this.locale); + } + + //#region BIND + this.t = this.t.bind(this); + //#endregion + } + + // string にしているのは、ドット区切りでのパス指定を許可するため + // なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも + public t(key: string, args?: Record): string { + try { + let str = key.split('.').reduce((o, i) => o[i], this.locale) as string; + + if (_DEV_) { + if (!str.includes('{')) { + console.warn(`i18n: '${key}' has no any arg. so ref prop directly instead of call this method.`); + } + } + + if (args) { + for (const [k, v] of Object.entries(args)) { + str = str.replace(`{${k}}`, v); + } + } + return str; + } catch (e) { + if (_DEV_) { + console.warn(`missing localization '${key}'`); + return `⚠'${key}'⚠`; + } + + return key; + } + } +} diff --git a/src/client/scripts/initialize-sw.ts b/src/client/scripts/initialize-sw.ts new file mode 100644 index 0000000000..d6dbd5dbd4 --- /dev/null +++ b/src/client/scripts/initialize-sw.ts @@ -0,0 +1,68 @@ +import { instance } from '@/instance'; +import { $i } from '@/account'; +import { api } from '@/os'; +import { lang } from '@/config'; + +export async function initializeSw() { + if (instance.swPublickey && + ('serviceWorker' in navigator) && + ('PushManager' in window) && + $i && $i.token) { + navigator.serviceWorker.register(`/sw.js`); + + navigator.serviceWorker.ready.then(registration => { + registration.active?.postMessage({ + msg: 'initialize', + lang, + }); + // SEE: https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe#Parameters + registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: urlBase64ToUint8Array(instance.swPublickey) + }).then(subscription => { + function encode(buffer: ArrayBuffer | null) { + return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer))); + } + + // Register + api('sw/register', { + endpoint: subscription.endpoint, + auth: encode(subscription.getKey('auth')), + publickey: encode(subscription.getKey('p256dh')) + }); + }) + // When subscribe failed + .catch(async (err: Error) => { + // 通知が許可されていなかったとき + if (err.name === 'NotAllowedError') { + return; + } + + // 違うapplicationServerKey (または gcm_sender_id)のサブスクリプションが + // 既に存在していることが原因でエラーになった可能性があるので、 + // そのサブスクリプションを解除しておく + const subscription = await registration.pushManager.getSubscription(); + if (subscription) subscription.unsubscribe(); + }); + }); + } +} + +/** + * Convert the URL safe base64 string to a Uint8Array + * @param base64String base64 string + */ +function urlBase64ToUint8Array(base64String: string): Uint8Array { + const padding = '='.repeat((4 - base64String.length % 4) % 4); + const base64 = (base64String + padding) + .replace(/-/g, '+') + .replace(/_/g, '/'); + + const rawData = window.atob(base64); + const outputArray = new Uint8Array(rawData.length); + + for (let i = 0; i < rawData.length; ++i) { + outputArray[i] = rawData.charCodeAt(i); + } + return outputArray; +} diff --git a/src/client/sw/compose-notification.ts b/src/client/sw/compose-notification.ts index 17421db5c8..e9586dd574 100644 --- a/src/client/sw/compose-notification.ts +++ b/src/client/sw/compose-notification.ts @@ -1,8 +1,17 @@ +/** + * Notification composer of Service Worker + */ +declare var self: ServiceWorkerGlobalScope; + import { getNoteSummary } from '../../misc/get-note-summary'; import getUserName from '../../misc/get-user-name'; -import { i18n } from '@/sw/i18n'; -export default async function(type, data): Promise<[string, NotificationOptions]> { +export default async function(type, data, i18n): Promise<[string, NotificationOptions] | null | undefined> { + if (!i18n) { + console.log('no i18n'); + return; + } + switch (type) { case 'driveFileCreated': // TODO (Server Side) return [i18n.t('_notification.fileUploaded'), { diff --git a/src/client/sw/i18n.ts b/src/client/sw/i18n.ts deleted file mode 100644 index 9b3e3b2f4d..0000000000 --- a/src/client/sw/i18n.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { I18n } from '@/i18n'; - -export const i18n = new I18n({ - // TODO -}); diff --git a/src/client/sw/sw.ts b/src/client/sw/sw.ts index 91d668c27b..c92cae1292 100644 --- a/src/client/sw/sw.ts +++ b/src/client/sw/sw.ts @@ -3,17 +3,30 @@ */ declare var self: ServiceWorkerGlobalScope; +import { get, set } from 'idb-keyval'; import composeNotification from '@/sw/compose-notification'; +import { I18n } from '@/scripts/i18n'; +//#region Variables const version = _VERSION_; const cacheName = `mk-cache-${version}`; - const apiUrl = `${location.origin}/api/`; -// インストールされたとき -self.addEventListener('install', ev => { - console.info('installed'); +let lang: string; +let i18n: I18n; +let pushesPool: any[] = []; +//#endregion + +//#region Startup +get('lang').then(async prelang => { + if (!prelang) return; + lang = prelang; + return fetchLocale(); +}); +//#endregion +//#region Lifecycle: Install +self.addEventListener('install', ev => { ev.waitUntil( caches.open(cacheName) .then(cache => { @@ -24,7 +37,9 @@ self.addEventListener('install', ev => { .then(() => self.skipWaiting()) ); }); +//#endregion +//#region Lifecycle: Activate self.addEventListener('activate', ev => { ev.waitUntil( caches.keys() @@ -36,7 +51,9 @@ self.addEventListener('activate', ev => { .then(() => self.clients.claim()) ); }); +//#endregion +//#region When: Fetching self.addEventListener('fetch', ev => { if (ev.request.method !== 'GET' || ev.request.url.startsWith(apiUrl)) return; ev.respondWith( @@ -49,8 +66,9 @@ self.addEventListener('fetch', ev => { }) ); }); +//#endregion -// プッシュ通知を受け取ったとき +//#region When: Caught Notification self.addEventListener('push', ev => { // クライアント取得 ev.waitUntil(self.clients.matchAll({ @@ -59,8 +77,65 @@ self.addEventListener('push', ev => { // クライアントがあったらストリームに接続しているということなので通知しない if (clients.length != 0) return; - const { type, body } = ev.data.json(); + const { type, body } = ev.data?.json(); + + // localeを読み込めておらずi18nがundefinedだった場合はpushesPoolにためておく + if (!i18n) return pushesPool.push({ type, body }); - return self.registration.showNotification(...(await composeNotification(type, body))); + const n = await composeNotification(type, body, i18n); + if (n) return self.registration.showNotification(...n); })); }); +//#endregion + +//#region When: Caught a message from the client +self.addEventListener('message', ev => { + switch(ev.data) { + case 'clear': + return; // TODO + default: + break; + } + + if (typeof ev.data === 'object') { + // E.g. '[object Array]' → 'array' + const otype = Object.prototype.toString.call(ev.data).slice(8, -1).toLowerCase(); + + if (otype === 'object') { + if (ev.data.msg === 'initialize') { + lang = ev.data.lang; + set('lang', lang); + fetchLocale(); + } + } + } +}); +//#endregion + +//#region Function: (Re)Load i18n instance +async function fetchLocale() { + //#region localeファイルの読み込み + // Service Workerは何度も起動しそのたびにlocaleを読み込むので、CacheStorageを使う + const localeUrl = `/assets/locales/${lang}.${version}.json`; + let localeRes = await caches.match(localeUrl); + + if (!localeRes) { + localeRes = await fetch(localeUrl); + const clone = localeRes?.clone(); + if (!clone?.clone().ok) return; + + caches.open(cacheName).then(cache => cache.put(localeUrl, clone)); + } + + i18n = new I18n(await localeRes.json()); + //#endregion + + //#region i18nをきちんと読み込んだ後にやりたい処理 + for (const { type, body } of pushesPool) { + const n = await composeNotification(type, body, i18n); + if (n) self.registration.showNotification(...n); + } + pushesPool = []; + //#endregion +} +//#endregion diff --git a/src/misc/get-notification-summary.ts b/src/misc/get-notification-summary.ts deleted file mode 100644 index aade3f75be..0000000000 --- a/src/misc/get-notification-summary.ts +++ /dev/null @@ -1,29 +0,0 @@ -import getUserName from './get-user-name'; -import { getNoteSummary } from './get-note-summary'; -import getReactionEmoji from './get-reaction-emoji'; -import locales = require('../../locales'); - -/** - * 通知を表す文字列を取得します。 - * @param notification 通知 - */ -export default function(notification: any): string { - switch (notification.type) { - case 'follow': - return `${getUserName(notification.user)}にフォローされました`; - case 'mention': - return `言及されました:\n${getUserName(notification.user)}「${getNoteSummary(notification.note, locales['ja-JP'])}」`; - case 'reply': - return `返信されました:\n${getUserName(notification.user)}「${getNoteSummary(notification.note, locales['ja-JP'])}」`; - case 'renote': - return `Renoteされました:\n${getUserName(notification.user)}「${getNoteSummary(notification.note, locales['ja-JP'])}」`; - case 'quote': - return `引用されました:\n${getUserName(notification.user)}「${getNoteSummary(notification.note, locales['ja-JP'])}」`; - case 'reaction': - return `リアクションされました:\n${getUserName(notification.user)} <${getReactionEmoji(notification.reaction)}>「${getNoteSummary(notification.note, locales['ja-JP'])}」`; - case 'pollVote': - return `投票されました:\n${getUserName(notification.user)}「${getNoteSummary(notification.note, locales['ja-JP'])}」`; - default: - return `<不明な通知タイプ: ${notification.type}>`; - } -} diff --git a/src/server/web/boot.js b/src/server/web/boot.js index eb7c21fb63..2bd306ea94 100644 --- a/src/server/web/boot.js +++ b/src/server/web/boot.js @@ -33,9 +33,8 @@ } const res = await fetch(`/assets/locales/${lang}.${v}.json`); - const json = await res.json(); localStorage.setItem('lang', lang); - localStorage.setItem('locale', JSON.stringify(json)); + localStorage.setItem('locale', await res.text()); } //#endregion diff --git a/src/server/web/index.ts b/src/server/web/index.ts index caa3f65c27..f3442c6199 100644 --- a/src/server/web/index.ts +++ b/src/server/web/index.ts @@ -73,8 +73,8 @@ router.get('/apple-touch-icon.png', async ctx => { }); // ServiceWorker -router.get(/^\/sw\.(.+?)\.js$/, async ctx => { - await send(ctx as any, `/assets/sw.${ctx.params[0]}.js`, { +router.get('/sw.js', async ctx => { + await send(ctx as any, `/assets/sw.${config.version}.js`, { root: client }); }); diff --git a/yarn.lock b/yarn.lock index b19feac238..4eecfd41f8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3260,11 +3260,6 @@ decompress-response@^6.0.0: dependencies: mimic-response "^3.1.0" -deep-entries@3.1.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/deep-entries/-/deep-entries-3.1.0.tgz#e456aa791d01b045641c75e41e170c0c95a9d472" - integrity sha512-pCpcCqx/hclnT2e4mMlM9geG8XIaxWN+yNKJHHwu1FZyYKErKU/fPztYYSk2HwnqRPf55cDEXraV6MLv8I5FrA== - deep-eql@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df" @@ -5011,6 +5006,11 @@ icss-utils@^5.0.0: resolved "https://registry.yarnpkg.com/icss-utils/-/icss-utils-5.0.0.tgz#03ed56c3accd32f9caaf1752ebf64ef12347bb84" integrity sha512-aF2Cf/CkEZrI/vsu5WI/I+akFgdbwQHVE9YRZxATrhH4PVIe6a3BIjwjEcW+z+jP/hNh+YvM3lAAn1wJQ6opSg== +idb-keyval@5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/idb-keyval/-/idb-keyval-5.0.1.tgz#d3913debfb58edee299da5cf2dded6c2670c05ef" + integrity sha512-bfi+Znn6oSPPgGcVUj2tYMIOQ5TD6V1qj50SdKQecGZx9lqUATcQ7ArHOt9sPcEhACoYe//yr2igmS6SMc59SA== + ieee754@1.1.13, ieee754@^1.1.13, ieee754@^1.1.4: version "1.1.13" resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.13.tgz#ec168558e95aa181fd87d37f55c32bbcb6708b84" -- cgit v1.2.3-freya