-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Features: - Ability to configure folder indentation (#730) - Inline color picker for JSON files (#614) ## Fixes: - Fixed transformFiles compilation step overwriting files in rare cases (bridge-core/dash-compiler@8178e1d) - Fixed "molang" plugin no longer transforming Molang statements (#732) - Fixed custom item components using the player API (#489) - Dash now creates a default hooks map upon loading cache file (bridge-core/dash-compiler@4741b92) - Fixed generator scripts generating files to wrong output location (#735) - "useTemplate" within generator scripts now works reliably (bridge-core/dash-compiler@7a26aff)
- Loading branch information
Showing
19 changed files
with
772 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type { languages } from 'monaco-editor' | ||
import { toTwoDigitHex } from './parse/hex' | ||
|
||
export class Color { | ||
constructor(public colorInfo: languages.IColor) {} | ||
|
||
/** | ||
* Formats the color as #RRGGBB | ||
*/ | ||
toHex() { | ||
return `#${toTwoDigitHex(this.colorInfo.red * 255)}${toTwoDigitHex( | ||
this.colorInfo.green * 255 | ||
)}${toTwoDigitHex(this.colorInfo.blue * 255)}` | ||
} | ||
|
||
/** | ||
* Formats the color as #RRGGBBAA | ||
*/ | ||
toHexA() { | ||
return `#${toTwoDigitHex(this.colorInfo.red * 255)}${toTwoDigitHex( | ||
this.colorInfo.green * 255 | ||
)}${toTwoDigitHex(this.colorInfo.blue * 255)}${toTwoDigitHex( | ||
Math.round(this.colorInfo.alpha * 255) | ||
)}` | ||
} | ||
|
||
/** | ||
* Formats the color as #AARRGGBB | ||
*/ | ||
toAHex() { | ||
return `#${toTwoDigitHex( | ||
Math.round(this.colorInfo.alpha * 255) | ||
)}${toTwoDigitHex(this.colorInfo.red * 255)}${toTwoDigitHex( | ||
this.colorInfo.green * 255 | ||
)}${toTwoDigitHex(this.colorInfo.blue * 255)}` | ||
} | ||
|
||
/** | ||
* Formats the color as [r, g, b], 0-1 | ||
*/ | ||
toDecRgbArray() { | ||
return [ | ||
+this.colorInfo.red.toFixed(5), | ||
+this.colorInfo.green.toFixed(5), | ||
+this.colorInfo.blue.toFixed(5), | ||
] | ||
} | ||
/** | ||
* Formats the color as [r, g, b], 0-255 | ||
*/ | ||
toRgbArray() { | ||
return [ | ||
Math.round(this.colorInfo.red * 255), | ||
Math.round(this.colorInfo.green * 255), | ||
Math.round(this.colorInfo.blue * 255), | ||
] | ||
} | ||
|
||
/** | ||
* Formats the color as [r, g, b, a] 0-1 | ||
*/ | ||
toDecRgbaArray() { | ||
return [ | ||
+this.colorInfo.red.toFixed(5), | ||
+this.colorInfo.green.toFixed(5), | ||
+this.colorInfo.blue.toFixed(5), | ||
this.colorInfo.alpha, | ||
] | ||
} | ||
/** | ||
* Formats the color as [r, g, b, a], 0-255 | ||
*/ | ||
toRgbaArray() { | ||
return [ | ||
Math.round(this.colorInfo.red * 255), | ||
Math.round(this.colorInfo.green * 255), | ||
Math.round(this.colorInfo.blue * 255), | ||
Math.round(this.colorInfo.alpha * 255), | ||
] | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
src/components/Languages/Json/ColorPicker/ColorPicker.ts
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 |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { useMonaco } from '/@/utils/libs/useMonaco' | ||
import type { editor, CancellationToken, languages } from 'monaco-editor' | ||
import { Color } from './Color' | ||
import { findColors } from './findColors' | ||
import { parseColor } from './parse/main' | ||
|
||
export async function registerColorPicker() { | ||
const { languages, Position, Range } = await useMonaco() | ||
|
||
languages.registerColorProvider('json', { | ||
provideDocumentColors: async ( | ||
model: editor.ITextModel, | ||
token: CancellationToken | ||
) => { | ||
return await findColors(model) | ||
}, | ||
provideColorPresentations: async ( | ||
model: editor.ITextModel, | ||
colorInfo: languages.IColorInformation, | ||
token: CancellationToken | ||
) => { | ||
const newColor = new Color(colorInfo.color) | ||
const value = model.getValueInRange(colorInfo.range) | ||
const position = new Position( | ||
colorInfo.range.startLineNumber, | ||
colorInfo.range.startColumn + 2 | ||
) | ||
|
||
// We need to decide which format this color is supposed to be in by doing 2 things: | ||
// 1. Parse the value to find the format | ||
// 2. Check the json path against valid locations to confirm the format, if necessary | ||
|
||
/** | ||
* Takes an array as a string and inserts values into the array while preserving whitespace | ||
*/ | ||
const insertToStringArray = (arr: string, newValues: any[]) => { | ||
const valueTest = /(\d+(?:\.\d*)?)/gim | ||
const split = arr.split(',') | ||
let newArr = '' | ||
// If the number of values to replace != the number of values available replace just return the orignal value | ||
if (split.length !== newValues.length) return arr | ||
for (const [i, value] of newValues.entries()) { | ||
// Get the first value to replace | ||
const toReplace = split[i].match(valueTest) | ||
// If there is something to replace, replace it | ||
if (toReplace && toReplace[0]) | ||
newArr += `${split[i].replace(toReplace[0], value)}${ | ||
i + 1 === newValues.length ? '' : ',' // Comma after value if not last element | ||
}` | ||
} | ||
return newArr | ||
} | ||
|
||
const { format } = await parseColor(value, { | ||
model, | ||
position, | ||
}) | ||
|
||
switch (format) { | ||
case 'hex': | ||
return [ | ||
{ | ||
label: `"${newColor.toHex().toUpperCase()}"`, | ||
}, | ||
] | ||
case 'hexa': | ||
return [ | ||
{ | ||
label: `"${newColor.toHexA().toUpperCase()}"`, | ||
}, | ||
] | ||
case 'ahex': | ||
return [ | ||
{ | ||
label: `"${newColor.toAHex().toUpperCase()}"`, | ||
}, | ||
] | ||
|
||
case 'rgbDec': | ||
return [ | ||
{ | ||
label: `[${newColor.toDecRgbArray().join(', ')}]`, | ||
textEdit: { | ||
range: colorInfo.range, | ||
text: insertToStringArray( | ||
value, | ||
newColor.toDecRgbArray() | ||
), | ||
}, | ||
}, | ||
] | ||
case 'rgb': | ||
return [ | ||
{ | ||
label: `[${newColor.toRgbArray().join(', ')}]`, | ||
textEdit: { | ||
range: colorInfo.range, | ||
text: insertToStringArray( | ||
value, | ||
newColor.toRgbArray() | ||
), | ||
}, | ||
}, | ||
] | ||
case 'rgbaDec': | ||
return [ | ||
{ | ||
label: `[${newColor.toDecRgbaArray().join(', ')}]`, | ||
textEdit: { | ||
range: colorInfo.range, | ||
text: insertToStringArray( | ||
value, | ||
newColor.toDecRgbaArray() | ||
), | ||
}, | ||
}, | ||
] | ||
case 'rgba': | ||
return [ | ||
{ | ||
label: `[${newColor.toRgbaArray().join(', ')}]`, | ||
textEdit: { | ||
range: colorInfo.range, | ||
text: insertToStringArray( | ||
value, | ||
newColor.toRgbaArray() | ||
), | ||
}, | ||
}, | ||
] | ||
|
||
default: | ||
// If all fails, don't do anything to be safe | ||
return [ | ||
{ | ||
label: '', | ||
textEdit: { | ||
range: new Range(0, 0, 0, 0), | ||
text: '', | ||
}, | ||
}, | ||
] | ||
} | ||
}, | ||
}) | ||
} |
Oops, something went wrong.