1+ import { MonacoReactFieldEditor } from "./field_react" ;
2+ import { registerMonacoFieldEditor } from "./monacoFieldEditor" ;
3+
4+ const fieldEditorId = "animation-editor" ;
5+
6+ export class MonacoAnimationEditor extends MonacoReactFieldEditor < pxt . Animation > {
7+ protected isPython : boolean ;
8+ protected editing : pxt . Asset ;
9+
10+ protected textToValue ( text : string ) : pxt . Animation {
11+ this . isPython = text . indexOf ( "`" ) === - 1
12+
13+ const match = pxt . parseAssetTSReference ( text ) ;
14+ if ( match ) {
15+ const { name : matchedName } = match ;
16+ const name = matchedName . trim ( ) ;
17+ const project = pxt . react . getTilemapProject ( ) ;
18+ const asset = project . lookupAssetByName ( pxt . AssetType . Animation , name ) ;
19+ if ( asset ) {
20+ this . editing = asset ;
21+ return asset ;
22+ }
23+ else {
24+ const newAsset = project . createNewAnimation ( ) ;
25+
26+ if ( name && ! project . isNameTaken ( pxt . AssetType . Animation , name ) && pxt . validateAssetName ( name ) ) {
27+ newAsset . meta . displayName = name ;
28+ }
29+ else {
30+ newAsset . meta . displayName = project . generateNewName ( pxt . AssetType . Animation ) ;
31+ }
32+
33+ this . editing = newAsset ;
34+
35+ return newAsset ;
36+ }
37+ }
38+
39+ return undefined ;
40+ }
41+
42+ protected resultToText ( result : pxt . Animation ) : string {
43+ const project = pxt . react . getTilemapProject ( ) ;
44+ project . pushUndo ( ) ;
45+ result = pxt . patchTemporaryAsset ( this . editing , result , project ) as pxt . Animation ;
46+ result = project . updateAsset ( result )
47+ return pxt . getTSReferenceForAsset ( result , this . isPython ) ;
48+ }
49+
50+ protected getFieldEditorId ( ) {
51+ return fieldEditorId ;
52+ }
53+ protected getOptions ( ) : any {
54+ return {
55+ initWidth : 16 ,
56+ initHeight : 16 ,
57+ blocksInfo : this . host . blocksInfo ( )
58+ } ;
59+ }
60+ }
61+
62+
63+ const regexes = [
64+ // typescript
65+ "assets\\s*\\.\\s*animation\\s*`[a-zA-Z_\\s\\n]*`" ,
66+
67+ // python
68+ 'assets\\s*\\.\\s*animation\\s*\\(\\s*"""[a-zA-Z_\\s\\n]*"""\\s*\\)'
69+ ] ;
70+
71+ const searchString = regexes . map ( r => `(?:${ r } )` ) . join ( "|" ) ;
72+
73+ export const animationEditorDefinition : pxt . editor . MonacoFieldEditorDefinition = {
74+ id : fieldEditorId ,
75+ foldMatches : true ,
76+ glyphCssClass : "sprite-editor-glyph sprite-focus-hover" ,
77+ heightInPixels : 510 ,
78+ matcher : {
79+ searchString : searchString ,
80+ isRegex : true ,
81+ matchCase : true ,
82+ matchWholeWord : false
83+ } ,
84+ proto : MonacoAnimationEditor
85+ } ;
86+
87+ registerMonacoFieldEditor ( fieldEditorId , animationEditorDefinition ) ;
0 commit comments