Skip to content

Commit

Permalink
unicode全部转义
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Dec 12, 2024
1 parent 57d8796 commit 0775e83
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
52 changes: 52 additions & 0 deletions src/editorTable/editorJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as jsonc from 'jsonc-parser';
import * as y3 from 'y3-helper';
import { Json } from '../tools/json';

const jsonFormatOptions: y3.json.formatOptions = {
stringify: (value) => {
if (typeof value === 'bigint') {
return value.toString();
}
if (typeof value === 'number' && isFinite(value)) {
return value.toFixed(1);
}
if (typeof value === 'string') {
// 将所有的 unicode 转为 \uXXXX 形式
let result = JSON.stringify(value);
result = result.replace(/[\u0080-\uffff]/g, (ch) => {
return '\\u' + ('0000' + ch.charCodeAt(0).toString(16)).slice(-4);
});
return result;
}
},
patchEdit: (edit) => {
let scanner = jsonc.createScanner(edit.content);
let edits: jsonc.Edit[] = [];
while (scanner.scan() !== jsonc.SyntaxKind.EOF) {
if (scanner.getToken() === jsonc.SyntaxKind.StringLiteral) {
let start = scanner.getTokenOffset();
let length = scanner.getTokenLength();
let value = edit.content.slice(start + 1, start + length - 1);
let newValue = value.replace(/[\u0080-\uffff]/g, (ch) => {
return '\\u' + ('0000' + ch.charCodeAt(0).toString(16)).slice(-4);
});
if (value !== newValue) {
edits.push({
offset: start + 1,
length: value.length,
content: newValue,
});
}
}
}
edit.content = jsonc.applyEdits(edit.content, edits);
edit.content = edit.content.replace(/,([\r\n])/g, ', $1');
return edit;
},
};

export class EditorJson extends Json {
constructor(text: string) {
super(text, jsonFormatOptions);
}
}
16 changes: 3 additions & 13 deletions src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { queue, throttle } from "../utility/decorators";
import { EditorData, valueOnGet, valueOnSet } from "./editorData";
import { define } from "../customDefine";
export { EditorData } from "./editorData";
import { EditorJson } from "./editorJson";

const templateDir = 'template\\json_template';
const metaDir = 'src\\helper_meta\\editor';
Expand Down Expand Up @@ -92,17 +93,6 @@ export async function ready() {
});
}

const jsonFormatOptions: y3.json.formatOptions = {
stringify: (value) => {
if (typeof value === 'bigint') {
return value.toString();
}
if (typeof value === 'number' && isFinite(value)) {
return value.toFixed(1);
}
},
};

export class EditorObject<N extends Table.NameCN = Table.NameCN> {
private _json?: y3.json.Json;
private _name?: string;
Expand All @@ -122,7 +112,7 @@ export class EditorObject<N extends Table.NameCN = Table.NameCN> {
if (!this.text) {
return undefined;
}
this._json = new y3.json.Json(this.text, jsonFormatOptions);
this._json = new EditorJson(this.text);
}
return this._json;
}
Expand Down Expand Up @@ -457,7 +447,7 @@ export class EditorTable<N extends Table.NameCN = Table.NameCN> extends vscode.D
templateJson = template.string;
}

let json = new y3.json.Json(templateJson, jsonFormatOptions);
let json = new EditorJson(templateJson);
json.set('name', y3.language.keyOf(name, true));
json.set('uid', key.toString());
json.set('key', BigInt(key));
Expand Down
5 changes: 4 additions & 1 deletion src/tools/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { string } from 'is';
import * as jsonc from 'jsonc-parser';

const parseOptions = {
Expand All @@ -21,6 +20,7 @@ export type JObject = { [key: string]: Item };

export interface formatOptions {
stringify?: (value: any) => string | undefined;
patchEdit?: (edit: jsonc.Edit) => jsonc.Edit;
}

export class Json {
Expand Down Expand Up @@ -104,6 +104,9 @@ export class Json {
for (const key in this._patch) {
const value = this._patch[key];
let edits = jsonc.modify(this._text, [key], value, editOptions);
if (this.options?.patchEdit) {
edits = edits.map(this.options.patchEdit);
}
this._text = jsonc.applyEdits(this._text, edits);
}
} finally {
Expand Down

0 comments on commit 0775e83

Please sign in to comment.