-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,80 @@ | ||
import * as jsonc from 'jsonc-parser'; | ||
import * as y3 from 'y3-helper'; | ||
import { Json } from '../tools/json'; | ||
import * as jsonc from 'jsonc-parser'; | ||
|
||
const jsonFormatOptions: y3.json.formatOptions = { | ||
stringify: (value) => { | ||
if (typeof value === 'bigint') { | ||
function stringify(value: any, tabLevel = 0): string { | ||
switch (typeof value) { | ||
case '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) => { | ||
}; | ||
case 'number': { | ||
if (isFinite(value)) { | ||
return value.toFixed(1); | ||
} else { | ||
return JSON.stringify(value); | ||
} | ||
}; | ||
case 'string': { | ||
return JSON.stringify(value).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, | ||
}); | ||
}; | ||
case 'object': { | ||
if (value === null) { | ||
return 'null'; | ||
} | ||
if (Array.isArray(value)) { | ||
if (value.length === 0) { | ||
return '[]'; | ||
} | ||
let result = '[\n'; | ||
const tab = ' '.repeat(tabLevel + 1); | ||
for (let i = 0; i < value.length; i++) { | ||
let item = value[i]; | ||
result += tab + stringify(item, tabLevel + 1); | ||
if (i < value.length - 1) { | ||
result += ', '; | ||
} | ||
result += '\n'; | ||
} | ||
result += ' '.repeat(tabLevel) + ']'; | ||
return result; | ||
} else { | ||
let keys = Object.keys(value); | ||
if (keys.length === 0) { | ||
return '{}'; | ||
} | ||
keys.sort(); | ||
let result = '{\n'; | ||
const tab = ' '.repeat(tabLevel + 1); | ||
for (let i = 0; i < keys.length; i++) { | ||
let key = keys[i]; | ||
let item = value[key]; | ||
result += tab + stringify(key) + ': ' + stringify(item, tabLevel + 1); | ||
if (i < keys.length - 1) { | ||
result += ', '; | ||
} | ||
result += '\n'; | ||
} | ||
result += ' '.repeat(tabLevel) + '}'; | ||
return result; | ||
} | ||
} | ||
edit.content = jsonc.applyEdits(edit.content, edits); | ||
edit.content = edit.content.replace(/,([\r\n])/g, ', $1'); | ||
return edit; | ||
}, | ||
}; | ||
}; | ||
default: { | ||
return JSON.stringify(value); | ||
}; | ||
} | ||
} | ||
|
||
export class EditorJson extends Json { | ||
constructor(text: string) { | ||
super(text, jsonFormatOptions); | ||
super(text, { | ||
stringify: stringify, | ||
patchJson: true, | ||
patchEdit: (edit: jsonc.Edit) => { | ||
edit.content = edit.content.replace(/,\n/g, ', \n'); | ||
return edit; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters