@ -689,99 +689,121 @@ editor_blockly = function () {
editor _blockly . completeItems = [ ] ;
Blockly . BlockSvg . prototype . updateColour _origin = Blockly . BlockSvg . prototype . updateColour ;
Blockly . BlockSvg . prototype . setShadowColour _ _origin = Blockly . BlockSvg . prototype . setShadowColour _ ;
Blockly . BlockSvg . prototype . setBorderColour _ _origin = Blockly . BlockSvg . prototype . setBorderColour _ ;
editor _blockly . onTextFieldCreate = function ( self , htmlInput ) {
var pb = self . sourceBlock _
var args = MotaActionBlocks [ pb . type ] . args
var targetf = args [ args . indexOf ( self . name ) + 1 ]
editor _blockly . setDarkScale = function ( ) {
var computedStyle = window . getComputedStyle ( document . getElementById ( 'blocklyDarkScale' ) ) || { } ;
var globalScale = parseFloat ( computedStyle . opacity ) || 0 ;
// ------ colour
if ( globalScale > 0 ) {
// todo: fix to suit the new blockly version
// ref https://github.com/google/blockly/tree/52b818ed67c5bfa7fd73edf2b067649b8b1f447b
Blockly . BlockSvg . prototype . updateColour = function ( ) {
if ( this . disabled ) {
// Disabled blocks don't have colour.
return ;
if ( targetf && targetf . slice ( 0 , 7 ) === 'Colour_' ) {
var inputDom = htmlInput ;
// var getValue=function(){ // 获得自己的字符串
// return pb.getFieldValue(self.name);
// }
var setValue = function ( newValue ) { // 设置右边颜色块的css颜色
pb . setFieldValue ( newValue , targetf )
}
// 给inputDom绑事件
inputDom . oninput = function ( ) {
var value = inputDom . value
if ( /^[0-9 ]+,[0-9 ]+,[0-9 ]+(,[0-9. ]+)?$/ . test ( value ) ) {
setValue ( 'rgba(' + value + ')' )
}
var hexColour = this . getColour ( ) ;
var colourSecondary = this . getColourSecondary ( ) ;
var colourTertiary = this . getColourTertiary ( ) ;
var rgb = goog . color . darken ( goog . color . hexToRgb ( hexColour ) , globalScale ) ;
hexColour = goog . color . rgbArrayToHex ( rgb ) ;
if ( this . isShadow ( ) ) {
hexColour = this . setShadowColour _ ( rgb , colourSecondary ) ;
} else {
this . setBorderColour _ ( rgb , colourTertiary ) ;
}
}
else {
htmlInput . onkeydown = function ( e ) {
if ( e . keyCode == 13 && awesomplete . opened && awesomplete . selected ) {
e . stopPropagation ( ) ;
e . stopImmediatePropagation ( ) ;
e . preventDefault ( ) ;
awesomplete . select ( ) ;
return false ;
}
this . svgPath _ . setAttribute ( 'fill' , hexColour ) ;
var icons = this . getIcons ( ) ;
for ( var i = 0 ; i < icons . length ; i ++ ) {
icons [ i ] . updateColour ( ) ;
}
// --- awesomplete
var awesomplete = new Awesomplete ( htmlInput , {
minChars : 1 ,
maxItems : 12 ,
autoFirst : true ,
replace : function ( text ) {
text = text . toString ( ) ;
var index = text . indexOf ( "( " ) ;
if ( index >= 0 ) text = text . substring ( 0 , index ) ;
var value = this . input . value , index = this . input . selectionEnd ;
if ( index == null ) index = value . length ;
if ( index < awesomplete . prefix . length ) index = awesomplete . prefix . length ;
var str = value . substring ( 0 , index - awesomplete . prefix . length ) + text + value . substring ( index ) ;
this . input . value = str ;
pb . setFieldValue ( str , self . name ) ;
self . forceRerender ( ) ;
self . resizeEditor _ ( ) ;
index += text . length - awesomplete . prefix . length ;
this . input . setSelectionRange ( index , index ) ;
editor _blockly . completeItems = editor _blockly . completeItems . filter ( function ( x ) {
return x != text ;
} ) ;
editor _blockly . completeItems . unshift ( text ) ;
} ,
filter : function ( ) { return true ; } ,
item : function ( text , input ) {
var li = document . createElement ( "li" ) ;
li . setAttribute ( "role" , "option" ) ;
li . setAttribute ( "aria-selected" , "false" ) ;
input = awesomplete . prefix . trim ( ) ;
if ( input != "" ) text = text . replace ( new RegExp ( "^" + input , "i" ) , "<mark>$&</mark>" ) ;
li . innerHTML = text ;
return li ;
} ,
sort : function ( a , b ) {
a = a . toString ( ) ; b = b . toString ( ) ;
var ia = editor _blockly . completeItems . indexOf ( a ) , ib = editor _blockly . completeItems . indexOf ( b ) ;
if ( ia < 0 ) ia = editor _blockly . completeItems . length ;
if ( ib < 0 ) ib = editor _blockly . completeItems . length ;
if ( ia != ib ) return ia - ib ;
if ( a . length != b . length ) return a . length - b . length ;
return a < b ? - 1 : 1 ;
}
// Bump every dropdown to change its colour.
// TODO (#1456)
for ( var x = 0 , input ; input = this . inputList [ x ] ; x ++ ) {
for ( var y = 0 , field ; field = input . fieldRow [ y ] ; y ++ ) {
field . forceRerender ( ) ;
} ) ;
htmlInput . oninput = function ( ) {
var value = htmlInput . value , index = htmlInput . selectionEnd ;
if ( index == null ) index = value . length ;
value = value . substring ( 0 , index ) ;
// cal prefix
awesomplete . prefix = value ;
for ( var i = index - 1 ; i >= 0 ; i -- ) {
var c = value . charAt ( i ) ;
if ( ! /^[a-zA-Z0-9_\u4E00-\u9FCC]$/ . test ( c ) ) {
awesomplete . prefix = value . substring ( i + 1 ) ;
break ;
}
}
var list = editor _blockly . getAutoCompletions ( value , pb . type , self . name ) ;
awesomplete . list = list ;
awesomplete . ul . style . marginLeft = getCaretCoordinates ( htmlInput , htmlInput . selectionStart ) . left -
htmlInput . scrollLeft - 20 + "px" ;
awesomplete . evaluate ( ) ;
}
// todo: fix to suit the new blockly version
// ref https://github.com/google/blockly/tree/52b818ed67c5bfa7fd73edf2b067649b8b1f447b
Blockly . BlockSvg . prototype . setShadowColour _ = function ( a , b ) {
if ( b ) {
this . svgPathLight _ . style . display = "none" ;
this . svgPathDark _ . style . display = "none" ;
this . svgPath _ . setAttribute ( "fill" , b ) ;
var c = b
} else
a = goog . color . darken ( a , . 4 ) ,
c = goog . color . rgbArrayToHex ( a ) ,
this . svgPathLight _ . style . display = "none" ,
this . svgPathDark _ . setAttribute ( "fill" , c ) ;
return c
}
// todo: fix to suit the new blockly version
// ref https://github.com/google/blockly/tree/52b818ed67c5bfa7fd73edf2b067649b8b1f447b
Blockly . BlockSvg . prototype . setBorderColour _ = function ( a , b ) {
if ( b )
this . svgPathLight _ . setAttribute ( "stroke" , "none" ) ,
this . svgPathDark _ . setAttribute ( "fill" , "none" ) ,
this . svgPath _ . setAttribute ( "stroke" , b ) ;
else {
this . svgPathLight _ . style . display = "" ;
var c = goog . color . rgbArrayToHex ( goog . color . darken ( a , . 2 ) )
, d = goog . color . rgbArrayToHex ( goog . color . lighten ( a , . 3 ) ) ;
this . svgPathLight _ . setAttribute ( "stroke" , c ) ;
this . svgPathDark _ . setAttribute ( "fill" , d ) ;
this . svgPath _ . setAttribute ( "stroke" , "none" )
}
}
} else {
Blockly . BlockSvg . prototype . updateColour = Blockly . BlockSvg . prototype . updateColour _origin ;
Blockly . BlockSvg . prototype . setShadowColour _ = Blockly . BlockSvg . prototype . setShadowColour _ _origin ;
Blockly . BlockSvg . prototype . setBorderColour _ = Blockly . BlockSvg . prototype . setBorderColour _ _origin ;
awesomplete . container . style . width = "100%" ;
window . awesomplete = awesomplete ;
}
}
editor _blockly . setDarkScale ( ) ;
return editor _blockly ;
}
// --- modify Blockly
// todo: fix to suit the new blockly version
// ref https://github.com/google/blockly/tree/52b818ed67c5bfa7fd73edf2b067649b8b1f447b
Blockly . FieldColour . prototype . createWidget _ = function ( ) {
Blockly . FieldColour . prototype . showEditor _ = function ( ) {
Blockly . WidgetDiv . hide ( ) ;
// console.log('here')
@ -802,189 +824,90 @@ Blockly.FieldColour.prototype.createWidget_ = function() {
var setValue = function ( newValue ) { // css颜色
self . setValue ( newValue )
var c = new Colors ( ) ;
c . setColor ( newValue )
var rgbatext = [ c . colors . webSmart . r , c . colors . webSmart . g , c . colors . webSmart . b , c . colors . alpha ] . join ( "," ) ;
pb . setFieldValue ( rgbatext , targetf ) // 放在颜色块左边的域中
pb . setFieldValue ( newValue . replace ( "rgba(" , "" ) . replace ( ")" , "" ) , targetf ) // 放在颜色块左边的域中
}
setTimeout ( function ( ) {
document . getElementById ( "colorPicker" ) . value = getValue ( ) ;
// 设置位置
openColorPicker ( Blockly . WidgetDiv . DIV . style . left . replace ( /[^\d.]/g , '' ) , Blockly . WidgetDiv . DIV . style . top . replace ( /[^\d.]/g , '' ) , setValue ) ;
var scaledBBox = self . getScaledBBox ( ) ;
openColorPicker ( scaledBBox . left , scaledBBox . bottom , setValue ) ;
} ) ;
return document . createElement ( 'table' ) ;
} ;
// todo: fix to suit the new blockly version
// ref https://github.com/google/blockly/tree/52b818ed67c5bfa7fd73edf2b067649b8b1f447b
Blockly . FieldColour . prototype . setValue = function ( colour ) {
this . doValueUpdate _ ( colour ) ;
}
Blockly . FieldTextInput . prototype . showInlineEditor _ = function ( quietInput ) {
Blockly . WidgetDiv . show ( this , this . sourceBlock _ . RTL , this . widgetDispose _ ( ) ) ;
var div = Blockly . WidgetDiv . DIV ;
// Create the input.
var htmlInput =
goog . dom . createDom ( goog . dom . TagName . INPUT , 'blocklyHtmlInput' ) ;
htmlInput . setAttribute ( 'spellcheck' , this . spellcheck _ ) ;
var fontSize =
( Blockly . FieldTextInput . FONTSIZE * this . workspace _ . scale ) + 'pt' ;
div . style . fontSize = fontSize ;
htmlInput . style . fontSize = fontSize ;
Blockly . FieldTextInput . htmlInput _ = htmlInput ;
div . appendChild ( htmlInput ) ;
htmlInput . value = htmlInput . defaultValue = this . text _ ;
htmlInput . oldValue _ = null ;
// console.log('here')
var self = this ;
var pb = self . sourceBlock _
var args = MotaActionBlocks [ pb . type ] . args
var targetf = args [ args . indexOf ( self . name ) + 1 ]
// ------ colour
if ( targetf && targetf . slice ( 0 , 7 ) === 'Colour_' ) {
var inputDom = htmlInput ;
// var getValue=function(){ // 获得自己的字符串
// return pb.getFieldValue(self.name);
// }
var setValue = function ( newValue ) { // 设置右边颜色块的css颜色
pb . setFieldValue ( newValue , targetf )
}
// 给inputDom绑事件
inputDom . oninput = function ( ) {
var value = inputDom . value
if ( /^[0-9 ]+,[0-9 ]+,[0-9 ]+(,[0-9. ]+)?$/ . test ( value ) ) {
setValue ( 'rgba(' + value + ')' )
}
}
}
else {
htmlInput . onkeydown = function ( e ) {
if ( e . keyCode == 13 && awesomplete . opened && awesomplete . selected ) {
e . stopPropagation ( ) ;
e . stopImmediatePropagation ( ) ;
e . preventDefault ( ) ;
awesomplete . select ( ) ;
return false ;
}
}
// --- awesomplete
var awesomplete = new Awesomplete ( htmlInput , {
minChars : 1 ,
maxItems : 12 ,
autoFirst : true ,
replace : function ( text ) {
text = text . toString ( ) ;
var index = text . indexOf ( "( " ) ;
if ( index >= 0 ) text = text . substring ( 0 , index ) ;
var value = this . input . value , index = this . input . selectionEnd ;
if ( index == null ) index = value . length ;
if ( index < awesomplete . prefix . length ) index = awesomplete . prefix . length ;
var str = value . substring ( 0 , index - awesomplete . prefix . length ) + text + value . substring ( index ) ;
this . input . value = str ;
pb . setFieldValue ( str , self . name ) ;
index += text . length - awesomplete . prefix . length ;
this . input . setSelectionRange ( index , index ) ;
editor _blockly . completeItems = editor _blockly . completeItems . filter ( function ( x ) {
return x != text ;
} ) ;
editor _blockly . completeItems . unshift ( text ) ;
} ,
filter : function ( ) { return true ; } ,
item : function ( text , input ) {
var li = document . createElement ( "li" ) ;
li . setAttribute ( "role" , "option" ) ;
li . setAttribute ( "aria-selected" , "false" ) ;
input = awesomplete . prefix . trim ( ) ;
if ( input != "" ) text = text . replace ( new RegExp ( "^" + input , "i" ) , "<mark>$&</mark>" ) ;
li . innerHTML = text ;
return li ;
} ,
sort : function ( a , b ) {
a = a . toString ( ) ; b = b . toString ( ) ;
var ia = editor _blockly . completeItems . indexOf ( a ) , ib = editor _blockly . completeItems . indexOf ( b ) ;
if ( ia < 0 ) ia = editor _blockly . completeItems . length ;
if ( ib < 0 ) ib = editor _blockly . completeItems . length ;
if ( ia != ib ) return ia - ib ;
if ( a . length != b . length ) return a . length - b . length ;
return a < b ? - 1 : 1 ;
}
} ) ;
htmlInput . oninput = function ( ) {
var value = htmlInput . value , index = htmlInput . selectionEnd ;
if ( index == null ) index = value . length ;
value = value . substring ( 0 , index ) ;
// cal prefix
awesomplete . prefix = value ;
for ( var i = index - 1 ; i >= 0 ; i -- ) {
var c = value . charAt ( i ) ;
if ( ! /^[a-zA-Z0-9_\u4E00-\u9FCC]$/ . test ( c ) ) {
awesomplete . prefix = value . substring ( i + 1 ) ;
break ;
}
}
var list = editor _blockly . getAutoCompletions ( value , pb . type , self . name ) ;
awesomplete . list = list ;
awesomplete . ul . style . marginLeft = getCaretCoordinates ( htmlInput , htmlInput . selectionStart ) . left -
htmlInput . scrollLeft - 20 + "px" ;
awesomplete . evaluate ( ) ;
}
awesomplete . container . style . width = "100%" ;
window . awesomplete = awesomplete ;
}
Blockly . WidgetDiv . show (
this , this . sourceBlock _ . RTL , this . widgetDispose _ . bind ( this ) ) ;
this . htmlInput _ = this . widgetCreate _ ( ) ;
this . isBeingEdited _ = true ;
editor _blockly . onTextFieldCreate ( this , this . htmlInput _ ) ;
if ( ! quietInput ) {
htmlInput . focus ( ) ;
htmlInput . select ( ) ;
this . htmlInput _ . focus ( { preventScroll : true } ) ;
this . htmlInput _ . select ( ) ;
}
this . validate _ ( ) ;
this . resizeEditor _ ( ) ;
} ;
this . bindEvents _ ( htmlInput ) ;
Blockly . FieldTextInput . prototype . onHtmlInputKeyDown _ = function ( e ) {
if ( e . keyCode == Blockly . utils . KeyCodes . ENTER && ! ( window . awesomplete && window . awesomplete . opened ) ) {
Blockly . WidgetDiv . hide ( ) ;
Blockly . DropDownDiv . hideWithoutAnimation ( ) ;
} else if ( e . keyCode == Blockly . utils . KeyCodes . ESC ) {
this . htmlInput _ . value = this . htmlInput _ . defaultValue ;
Blockly . WidgetDiv . hide ( ) ;
Blockly . DropDownDiv . hideWithoutAnimation ( ) ;
} else if ( e . keyCode == Blockly . utils . KeyCodes . TAB ) {
Blockly . WidgetDiv . hide ( ) ;
Blockly . DropDownDiv . hideWithoutAnimation ( ) ;
this . sourceBlock _ . tab ( this , ! e . shiftKey ) ;
e . preventDefault ( ) ;
}
} ;
Blockly . copy _ = function ( toCopy ) {
if ( toCopy . isComment ) {
var xml = toCopy . toXmlWithXY ( ) ;
var xml = toCopy . toXmlWithXY ( ) ;
} else {
var xml = Blockly . Xml . blockToDom ( toCopy , true ) ;
// Copy only the selected block and internal blocks.
Blockly . Xml . deleteNext ( xml ) ;
// Encode start position in XML.
var xy = toCopy . getRelativeToSurfaceXY ( ) ;
xml . setAttribute ( 'x' , toCopy . RTL ? - xy . x : xy . x ) ;
xml . setAttribute ( 'y' , xy . y ) ;
var xml = Blockly . Xml . blockToDom ( toCopy , true ) ;
// Copy only the selected block and internal blocks.
Blockly . Xml . deleteNext ( xml ) ;
// Encode start position in XML.
var xy = toCopy . getRelativeToSurfaceXY ( ) ;
xml . setAttribute ( 'x' , toCopy . RTL ? - xy . x : xy . x ) ;
xml . setAttribute ( 'oy' , xy . y ) ;
xml . setAttribute ( 'sy' , toCopy . workspace . scrollY ) ;
}
Blockly . clipboardXml _ = xml ;
Blockly . clipboardSource _ = toCopy . workspace ;
Blockly . clipboardTypeCounts _ = toCopy . isComment ? null :
Blockly . utils . getBlockTypeCounts ( toCopy , true ) ;
setTimeout ( function ( ) { Blockly . clipboardSource _ . paste ( Blockly . clipboardXml _ ) } ) ;
} ;
Blockly . duplicate = function ( toDuplicate ) {
// Save the clipboard.
var clipboardXml = Blockly . clipboardXml _ ;
var clipboardSource = Blockly . clipboardSource _ ;
// Create a duplicate via a copy/paste operation.
Blockly . copy _ ( toDuplicate ) ;
// Restore the clipboard.
Blockly . clipboardXml _ = clipboardXml ;
Blockly . clipboardSource _ = clipboardSource ;
} ;
Blockly . utils . KeyCodes . V = 99999
} ;
/ * *
* Paste the provided block onto the workspace .
* @ param { ! Element } xmlBlock XML block element .
* /
Blockly . WorkspaceSvg . prototype . paste = function ( xmlBlock ) {
if ( ! this . rendered || xmlBlock . getElementsByTagName ( 'block' ) . length >=
this . remainingCapacity ( ) ) {
return ;
}
if ( this . currentGesture _ ) {
this . currentGesture _ . cancel ( ) ; // Dragging while pasting? No.
}
if ( xmlBlock . tagName . toLowerCase ( ) == 'comment' ) {
this . pasteWorkspaceComment _ ( xmlBlock ) ;
} else {
if ( xmlBlock . hasAttribute ( 'oy' ) && xmlBlock . hasAttribute ( 'sy' ) ) {
xmlBlock . setAttribute ( 'y' , parseFloat ( xmlBlock . getAttribute ( 'oy' ) ) + parseFloat ( xmlBlock . getAttribute ( 'sy' ) ) - this . scrollY ) ;
}
this . pasteBlock _ ( xmlBlock ) ;
}
} ;