Skip to content

Commit

Permalink
fix: draw emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
nusr committed Dec 13, 2023
1 parent 536a8a9 commit e251186
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/canvas/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ERROR_FORMULA_COLOR,
dpr,
isEmpty,
splitToWords,
} from '@/util';
import { ModelCellType, CanvasOverlayPosition, ErrorTypes, Point, EUnderLine, IWindowSize, ResultType } from '@/types';

Expand Down Expand Up @@ -116,8 +117,7 @@ export function renderCell(
fillStyle = ERROR_FORMULA_COLOR;
}
const result: IRenderCellResult = {};
// unicode
const texts = [...text];
const texts = splitToWords(text)
if (texts.length === 0) {
return result;
}
Expand Down
7 changes: 7 additions & 0 deletions src/formula/__tests__/interpreter/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ describe('parseFormula function', () => {
expressionStr: '',
});
});
it('function CONCAT', () => {
expect(parseFormula('CONCAT("😊", "πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§", "πŸ‘¦πŸΎ")')).toEqual({
error: null,
result: 'πŸ˜ŠπŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§πŸ‘¦πŸΎ',
expressionStr: 'CONCAT(😊,πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§,πŸ‘¦πŸΎ)',
});
});
});
15 changes: 13 additions & 2 deletions src/formula/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { Scanner } from './scanner';
import { Parser } from './parser';
import formulas, { CustomError } from './formula';
import { Interpreter } from './interpreter';
import { CellDataMap, InterpreterResult, DefinedNamesMap, FormulaData, IRange } from '@/types';
import {
CellDataMap,
InterpreterResult,
DefinedNamesMap,
FormulaData,
IRange,
} from '@/types';

export function parseFormula(
source: string,
Expand All @@ -14,7 +20,12 @@ export function parseFormula(
try {
const list = new Scanner(source).scan();
const expressions = new Parser(list).parse();
const result = new Interpreter(expressions, cellData, definedNamesMap, functionMap).interpret();
const result = new Interpreter(
expressions,
cellData,
definedNamesMap,
functionMap,
).interpret();

const strList: string[] = [];
for (const item of expressions) {
Expand Down
8 changes: 6 additions & 2 deletions src/formula/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ export class CallExpression implements Expression {
return visitor.visitCallExpression(this);
}
toString(): string {
return `${this.name.toString().toUpperCase()}(${this.params.map((item) => item.toString()).join(',')})`;
return `${this.name.toString().toUpperCase()}(${this.params
.map((item) => item.toString())
.join(',')})`;
}
}
export class CellRangeExpression implements Expression {
Expand All @@ -140,7 +142,9 @@ export class CellRangeExpression implements Expression {
return visitor.visitCellRangeExpression(this);
}
toString(): string {
return this.left.toString() + this.operator.toString() + this.right.toString();
return (
this.left.toString() + this.operator.toString() + this.right.toString()
);
}
}

Expand Down
28 changes: 0 additions & 28 deletions src/formula/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,35 +154,7 @@ export class Parser {
if (errorSet.has(realValue as ErrorTypes)) {
throw new CustomError(realValue as ErrorTypes);
}
// if (this.match(TokenType.EXCLAMATION)) {
// const expr = this.expression();
// if (expr instanceof TokenExpression) {
// return this.addCellExpression(expr.value, expr.type, name);
// }
// throw new CustomError('#REF!');
// }
return new TokenExpression(name);
// const newToken = new Token(type, realValue);
// if (this.isFunctionName(realValue)) {
// return new TokenExpression(new Token(type, getFunctionName(realValue)))
// }
// if (/^[a-z]+$/i.test(value)) {
// return new TokenExpression(name);
// }
// if (
// /^\$[A-Z]+\$\d+$/.test(realValue) ||
// /^\$[A-Z]+$/.test(realValue) ||
// /^\$\d+$/.test(realValue)
// ) {
// return this.addCellExpression(newToken, 'absolute', null);
// }
// if (/^\$[A-Z]+\d+$/.test(realValue) || /^[A-Z]+\$\d+$/.test(realValue)) {
// return this.addCellExpression(newToken, 'mixed', null);
// }
// if (/^[A-Z]+\d+$/.test(realValue) || /^[A-Z]+$/.test(realValue)) {
// return this.addCellExpression(newToken, 'relative', null);
// }
// return new TokenExpression(name);
}

throw new CustomError('#ERROR!');
Expand Down
4 changes: 2 additions & 2 deletions src/formula/scanner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TokenType } from '@/types';
import { Token } from './token';
import { CustomError } from './formula';
import { splitToWords } from '@/util'

const emptyData = '';
const identifierMap = new Map<string, TokenType>([
Expand All @@ -14,8 +15,7 @@ export class Scanner {
private start = 0;
private readonly tokens: Token[] = [];
constructor(source: string) {
// unicode
this.list = [...source];
this.list = splitToWords(source)
}
scan(): Token[] {
while (!this.isAtEnd()) {
Expand Down
41 changes: 40 additions & 1 deletion src/model/mockModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const MOCK_MODEL: WorkBookJSON = {
1: {
0: {
0: {
value: '1',
value: '1abcd',
style: {
fontColor: '#ff0000',
},
Expand All @@ -75,6 +75,33 @@ export const MOCK_MODEL: WorkBookJSON = {
underline: EUnderLine.SINGLE,
},
},
6: {
formula: '=CONCAT("😊", "πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§", "πŸ‘¦πŸΎ")',
style: {
fontSize: 30,
},
},
7: {
value: 'ZΝ‘Ν«ΝƒΝͺΜ‚Ν«Μ½ΝΜ΄Μ™Μ€ΜžΝ‰ΝšΜ―ΜžΜ ΝAΝ«Ν—Μ΄Ν’Μ΅ΜœΜ°Ν”L̠ͨͧͩ͘GΜ‘Ν—ΜŽΜ…Ν›ΝΜ΄Μ»ΝˆΝΝ”ΜΉOΝ‚ΜŒΜŒΝ˜Μ¨Μ΅ΜΉΜ»ΜΜ³!ΜΏΜ‹Ν₯Ν₯Μ‚Ν£ΜΜΜΝžΝœΝ–Μ¬Μ°Μ™Μ—',
style: {
fontSize: 30,
},
},
8: {
value: '뎌쉐',
style: {
fontSize: 30,
},
},
9: {
value: 'ĹoΝ‚řeΜ§mΜ…',
style: {
fontSize: 30,
},
},
10: {
value: 'πŸŒ·πŸŽπŸ’©πŸ˜œπŸ‘πŸ³οΈβ€πŸŒˆ',
},
},
3: {
0: {
Expand Down Expand Up @@ -136,6 +163,18 @@ export const MOCK_MODEL: WorkBookJSON = {
widthOrHeight: 100,
isHide: true,
},
6: {
widthOrHeight: 200,
isHide: false
},
7: {
widthOrHeight: 200,
isHide: false
},
10: {
widthOrHeight: 100,
isHide: false
},
},
},
definedNames: {
Expand Down
13 changes: 12 additions & 1 deletion src/util/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getListMaxNum, isNumber, parseNumber, getDefaultSheetInfo } from '../util';
import {
getListMaxNum,
isNumber,
parseNumber,
getDefaultSheetInfo,
splitToWords,
} from '../util';

describe('util.test.ts', () => {
describe('getListMaxNum', () => {
Expand Down Expand Up @@ -74,4 +80,9 @@ describe('util.test.ts', () => {
expect(parseNumber('1.2')).toEqual(1.2);
});
});
describe('splitToWords', () => {
it('should splitToWords', () => {
expect(splitToWords('πŸ˜ŠπŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§πŸ‘¦πŸΎ')).toEqual(['😊', 'πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§', 'πŸ‘¦πŸΎ']);
});
});
});
10 changes: 9 additions & 1 deletion src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function getListMaxNum(list: string[] = []): number {
return Math.max(Math.max(...idList), 0);
}

export function getDefaultSheetInfo(list: WorksheetType[] = []): Pick<WorksheetType, 'name' | 'sheetId'> {
export function getDefaultSheetInfo(
list: WorksheetType[] = [],
): Pick<WorksheetType, 'name' | 'sheetId'> {
const sheetId = getListMaxNum(list.map((item) => item.sheetId)) + 1;
return {
name: `${SHEET_NAME_PREFIX}${sheetId}`,
Expand All @@ -60,3 +62,9 @@ export function isTestEnv(): boolean {
export function isDevEnv(): boolean {
return process.env.NODE_ENV === 'development';
}

export function splitToWords(str: string): string[] {
// unicode
const list = new Intl.Segmenter().segment(str);
return [...list].map((x) => x.segment);
}

0 comments on commit e251186

Please sign in to comment.