documentオブジェクトとwindowオブジェクトを拡張できるようにする。
EmEditorマクロをJavaScriptで定義し直す。(Wrapperパターン)
定義し直すと何がうれしいか。
- 何がうれしいか
- マクロを、document.xxxXXX(); や window.xxYY(); 形式で呼べる.
- ソースが一カ所に集まってくれる。
- 統一感が出る。
- 同じメソッドを使い回しやすい。
- 名前空間が汚染されにくい。
使い方。load("document.prototype.js");としてロードする
keyPress.js
....load関数(省略) load("document.prototype.js"); document.keyPress("{A}");//キーを押す。
document.prototype.js
///////////////////////////////////////////////////// Object.extend = function(destination, source) { for (property in source) { destination[property] = source[property]; } return destination; } ///////////////////////////////////////////////////// //オブジェクト拡張 ///////////////////////////////////////////////////// var _document = { keyPress : function(key){ var shell = new ActiveXObject("WScript.Shell"); shell.SendKeys( key );//key={F5}, key={A}... }, onSave : function(){}, onLoad : function(){}, onClose : function(){} } var _window ={ n2blr : function(){ var text = document.selection.text.replace( /\r\n/g, "<br />\n"); document.selection.text = text; } } ///////////////////////////////////////////////////// ///Emeditor オブジェクトをJSで定義する ///////////////////////////////////////////////////// var window ={ clipboardData : { clearData : function(x){Window.clipboardData.clearData(x)}, getData : function(x){Window.clipboardData.getData(x)}, setData : function(x,y){Window.clipboardData.setData(x,y)} }, editor : { ActiveDocument : { ConfigName : Window.Editor.ActiveDocument.ConfigName, Encoding : Window.Editor.ActiveDocument.Encoding, FontCategory : Window.Editor.ActiveDocument.FontCategory, FullName : Window.Editor.ActiveDocument.FullName, HighlightFind : Window.Editor.ActiveDocument.HighlightFind, HighlightTag : Window.Editor.ActiveDocument.HighlightTag, Name : Window.Editor.ActiveDocument.Name, Path : Window.Editor.ActiveDocument.Path, ReadOnly : Window.Editor.ActiveDocument.ReadOnly, selection : Window.Editor.ActiveDocument.selection, Activate : function(){Window.Editor.ActiveDocument.Activate()}, close : function(){Window.Editor.ActiveDocument.close()}, CopyFullName : function(){Window.Editor.ActiveDocument.CopyFullName()}, CopyPath : function(){Window.Editor.ActiveDocument.CopyPath()}, Redo : function(){Window.Editor.ActiveDocument.Redo()}, Undo : function(){Window.Editor.ActiveDocument.Undo()}, Save : function(x){Window.Editor.ActiveDocument.Save(x)}, write : function(x){Window.Editor.ActiveDocument.write(x)}, writeln : function(x){Window.Editor.ActiveDocument.writeln(x)}, UnicodeSignature : Window.Editor.ActiveDocument.UnicodeSignature }, Documents : { Count : Window.Editor.Documents.Count, Item : function(x){ var doc = new Object; doc.base = Window.Editor.Documents.Item(x); doc.ConfigName = doc.base.ConfigName; doc.Encoding = doc.base.Encoding; doc.FontCategory = doc.base.FontCategory; doc.FullName = doc.base.FullName; doc.HighlightFind = doc.base.HighlightFind; doc.HighlightTag = doc.base.HighlightTag; doc.Name = doc.base.Name; doc.Path = doc.base.Path; doc.ReadOnly = doc.base.ReadOnly; doc.selection = function(){return doc.base.selection}//遅延評価 doc.Activate = function(){doc.base.Activate()}; doc.close = function(){doc.base.close()}; doc.CopyFullName = function(){doc.base.CopyFullName()}; doc.CopyPath = function(){doc.base.CopyPath()}; doc.Redo = function(){doc.base.Redo()}; doc.Undo = function(){doc.base.Undo()}; doc.Save = function(x){doc.base.Save(x)}; doc.write = function(x){doc.base.write(x)}; doc.writeln = function(x){doc.base.writeln(x)}; doc.UnicodeSignature = doc.base.UnicodeSignature; Object.extend( doc, _document ); return doc; } }, EnableTab : Window.Editor.EnableTab, FullName : Window.Editor.FullName, Version : Window.Editor.Version, version : Window.Editor.Version, ExecuteCommandByID : function(x){Editor.ExecuteCommandByID(x)}, FindInFiles : function(a,b,c,d,e){ Editor.FindInFiles(a,b,c,d,e) }, NewFile : function(){Editor.NewFile()}, OpenFile : function(x,y,z){Editor.OpenFile(x,y,z)}, QueryStatusByID : function(x){Editor.QueryStatusByID(x)}, ReplaceInFiles : function(a,b,c,d,d,e,f){ Editor.ReplaceInFiles(a,b,c,d,d,e,f) }, SaveAll : function(){Editor.SaveAll()}, SaveCloseAll : function(){Editor.SaveCloseAll()} }, document : { ConfigName : Window.Document.ConfigName, Encoding : Window.Document.Encoding, FontCategory : Window.Document.FontCategory, FullName : Window.Document.FullName, HighlightFind : Window.Document.HighlightFind, HighlightTag : Window.Document.HighlightTag, Name : Window.Document.Name, Path : Window.Document.Path, ReadOnly : Window.Document.ReadOnly, selection : Window.Document.selection, Activate : function(){Window.Document.Activate()}, close : function(){Window.Document.close()}, CopyFullName : function(){Window.Document.CopyFullName()}, CopyPath : function(){Window.Document.CopyPath()}, Redo : function(){Window.Document.Redo()}, Undo : function(){Window.Document.Undo()}, Save : function(x){Window.Document.Save(x)}, write : function(x){Window.Document.write(x)}, writeln : function(x){Window.Document.writeln(x)}, UnicodeSignature : Window.Document.UnicodeSignature }, Redraw : Window.Redraw, ScriptFullName : Window.ScriptFullName, ScriptName : Window.ScriptName, scrollX : Window.scrollX, scrollY : Window.scrollY, status : Window.status, alert : function(x){Window.alert(x)}, close : function(){Window.close()}, confirm : function(x){Window.confirm(x)}, CreatePopupMenu : function(){Window.CreatePopupMenu()}, print : function(){Window.print()}, prompt : function(x){Window.prompt(x)}, Quit : function(){Window.Quit()}, scrollBy : function(x, y){Window.scrollBy(x, y)}, scrollBy : function(x, y){Window.scrollBy(x, y)}, Sleep : function(x){Window.Sleep(x)} } ///////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////// Object.extend( window.document, _document ); Object.extend( window.editor.ActiveDocument, _document ); ///////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////// var document = window.document; var editor = window.editor; var Redraw = window.Redraw; var scrollX = window.scrollX; var scrollY = window.scrollY; var status = window.status; var ScriptName = window.ScriptName; var ScriptFullName = window.ScriptFullName; var clipboardData = window.clipboardData; var alert = window.alert; var close = window.close; var confirm = window.confirm; var CreatePopupMenu= window.CreatePopupMenu; var print = window.print; var prompt = window.prompt; var Quit = window.Quit; var scrollBy = window.scrollBy; var scrollBy = window.scrollBy; var Sleep = window.Sleep; /////////////////////////////////////
これ使って、EmEditor Macro用のwindow/documentをJavaScript Objectに変更。
勝手気ままに拡張し放題。
これと、naoExecMacroOnEventを組み合わせてイベントハンドラを実現していきたい。
来週の休みにでもやるかな。