浏览代码

Suggested PR changes for #5395

ssaket 6 年之前
父节点
当前提交
925bf2c573
共有 3 个文件被更改,包括 76 次插入34 次删除
  1. 53 10
      gui/src/2D/advancedDynamicTexture.ts
  2. 3 6
      gui/src/2D/controls/control.ts
  3. 20 18
      gui/src/2D/controls/inputText.ts

+ 53 - 10
gui/src/2D/advancedDynamicTexture.ts

@@ -1,4 +1,4 @@
-import { DynamicTexture, Nullable, Observer, Camera, Engine, KeyboardInfoPre, PointerInfoPre, PointerInfo, Layer, Viewport, Scene, Texture, KeyboardEventTypes, Vector3, Matrix, Vector2, Tools, PointerEventTypes, AbstractMesh, StandardMaterial, Color3, Observable, ClipboardInfo } from 'babylonjs';
+import { DynamicTexture, Nullable, Observer, Camera, Engine, KeyboardInfoPre, PointerInfoPre, PointerInfo, ClipboardEventTypes, Layer, Viewport, Scene, Texture, KeyboardEventTypes, Vector3, Matrix, Vector2, Tools, PointerEventTypes, AbstractMesh, StandardMaterial, Color3, Observable, ClipboardInfo } from 'babylonjs';
 import { Container } from "./controls/container";
 import { Container } from "./controls/container";
 import { Control } from "./controls/control";
 import { Control } from "./controls/control";
 import { Style } from "./style";
 import { Style } from "./style";
@@ -21,7 +21,6 @@ export interface IFocusableControl {
      * KeyboardEvent -> handles key events
      * KeyboardEvent -> handles key events
      * PointerEvent -> handles dbl click event and text highlight.
      * PointerEvent -> handles dbl click event and text highlight.
      * ClipboardInfo -> handles copy, cut, paste events.
      * ClipboardInfo -> handles copy, cut, paste events.
-     * @param evt defines the current keyboard event
      */
      */
     processKeyboard(evt: Event): void;
     processKeyboard(evt: Event): void;
 
 
@@ -71,12 +70,16 @@ export class AdvancedDynamicTexture extends DynamicTexture {
     private _blockNextFocusCheck = false;
     private _blockNextFocusCheck = false;
     private _renderScale = 1;
     private _renderScale = 1;
     private _rootCanvas: Nullable<HTMLCanvasElement>;
     private _rootCanvas: Nullable<HTMLCanvasElement>;
-    private _clipboardData: DataTransfer = new DataTransfer();
+    /**
+     * Define type to string to ensure compatibility across browsers
+     * Safari doesn't support DataTransfer constructor
+     */
+    private _clipboardData: string = "";
 
 
     /**
     /**
      * Observable event triggered each time an clipboard event is received from the rendering canvas
      * Observable event triggered each time an clipboard event is received from the rendering canvas
      */
      */
-    public onClipboardObserver = new Observable<ClipboardInfo>();
+    public onClipboardObservable = new Observable<ClipboardInfo>();
 
 
     /**
     /**
      * Gets or sets a boolean defining if alpha is stored as premultiplied
      * Gets or sets a boolean defining if alpha is stored as premultiplied
@@ -248,10 +251,10 @@ export class AdvancedDynamicTexture extends DynamicTexture {
     /**
     /**
      * Gets or set information about clipboardData
      * Gets or set information about clipboardData
      */
      */
-    public get clipboardData(): DataTransfer {
+    public get clipboardData(): string {
         return this._clipboardData;
         return this._clipboardData;
     }
     }
-    public set clipboardData(value: DataTransfer) {
+    public set clipboardData(value: string) {
         this._clipboardData = value;
         this._clipboardData = value;
     }
     }
 
 
@@ -399,8 +402,7 @@ export class AdvancedDynamicTexture extends DynamicTexture {
         }
         }
 
 
         this._rootContainer.dispose();
         this._rootContainer.dispose();
-        this.onClipboardObserver.clear();
-        this._clipboardData.clearData();
+        this.onClipboardObservable.clear();
 
 
         super.dispose();
         super.dispose();
     }
     }
@@ -615,7 +617,13 @@ export class AdvancedDynamicTexture extends DynamicTexture {
                 return;
                 return;
             }
             }
             //check for focused control and call the onClipboardPointerEvents
             //check for focused control and call the onClipboardPointerEvents
-            (this._focusedControl) ? ((pi.type === PointerEventTypes.POINTERDOUBLETAP) ? this._focusedControl.processKeyboard(pi.event) : null) : null;
+            if (this._focusedControl) {
+                if (pi.type === PointerEventTypes.POINTERDOUBLETAP) {
+                    this._focusedControl.processKeyboard(pi.event);
+                    return;
+                }
+
+            }
 
 
             if (pi.type !== PointerEventTypes.POINTERMOVE
             if (pi.type !== PointerEventTypes.POINTERMOVE
                 && pi.type !== PointerEventTypes.POINTERUP
                 && pi.type !== PointerEventTypes.POINTERUP
@@ -647,7 +655,7 @@ export class AdvancedDynamicTexture extends DynamicTexture {
             }
             }
         });
         });
 
 
-        this.onClipboardObserver.add((pi, state) => {
+        this.onClipboardObservable.add((pi, state) => {
             if (this.focusedControl) {
             if (this.focusedControl) {
                 // call the event's callback
                 // call the event's callback
                 this.focusedControl.processKeyboard(pi.event);
                 this.focusedControl.processKeyboard(pi.event);
@@ -657,6 +665,41 @@ export class AdvancedDynamicTexture extends DynamicTexture {
         this._attachToOnPointerOut(scene);
         this._attachToOnPointerOut(scene);
     }
     }
 
 
+    /** @hidden */
+    private onClipboardCopy = (evt: ClipboardEvent) => {
+        let ev = new ClipboardInfo(ClipboardEventTypes.COPY, evt);
+        this.onClipboardObservable.notifyObservers(ev);
+        evt.preventDefault();
+    }
+     /** @hidden */
+    private onClipboardCut = (evt: ClipboardEvent) => {
+        let ev = new ClipboardInfo(ClipboardEventTypes.CUT, evt);
+        this.onClipboardObservable.notifyObservers(ev);
+        evt.preventDefault();
+    }
+    /** @hidden */
+    private onClipboardPaste = (evt: ClipboardEvent) => {
+        let ev = new ClipboardInfo(ClipboardEventTypes.PASTE, evt);
+        this.onClipboardObservable.notifyObservers(ev);
+    }
+
+   /**
+     * @hidden
+     */
+    protected registerClipboardEvents(): void {
+        self.addEventListener("copy", this.onClipboardCopy, false);
+        self.addEventListener("cut", this.onClipboardCut, false);
+        self.addEventListener("paste", this.onClipboardPaste, false);
+    }
+    /**
+     * @hidden
+     */
+    protected unRegisterClipboardEvents(): void {
+        self.removeEventListener("copy", this.onClipboardCopy);
+        self.removeEventListener("cut",  this.onClipboardCut);
+        self.removeEventListener("paste", this.onClipboardPaste);
+    }
+
     /**
     /**
      * Connect the texture to a hosting mesh to enable interactions
      * Connect the texture to a hosting mesh to enable interactions
      * @param mesh defines the mesh to attach to
      * @param mesh defines the mesh to attach to

+ 3 - 6
gui/src/2D/controls/control.ts

@@ -155,9 +155,6 @@ export class Control {
    */
    */
     public onAfterDrawObservable = new Observable<Control>();
     public onAfterDrawObservable = new Observable<Control>();
 
 
-    /** Observable raised when the clipboard event is raised */
-    public onClipboardObservable: Nullable<Observer<ClipboardInfo>>;
-
     /** Gets or set information about font offsets (used to render and align text) */
     /** Gets or set information about font offsets (used to render and align text) */
     public get fontOffset(): { ascent: number, height: number, descent: number } {
     public get fontOffset(): { ascent: number, height: number, descent: number } {
         return this._fontOffset;
         return this._fontOffset;
@@ -1375,19 +1372,19 @@ export class Control {
     /** @hidden */
     /** @hidden */
     private onClipboardCopy = (evt: ClipboardEvent) => {
     private onClipboardCopy = (evt: ClipboardEvent) => {
         let ev = new ClipboardInfo(ClipboardEventTypes.COPY, evt);
         let ev = new ClipboardInfo(ClipboardEventTypes.COPY, evt);
-        this._host.onClipboardObserver.notifyObservers(ev);
+        this._host.onClipboardObservable.notifyObservers(ev);
         evt.preventDefault();
         evt.preventDefault();
     }
     }
      /** @hidden */
      /** @hidden */
     private onClipboardCut = (evt: ClipboardEvent) => {
     private onClipboardCut = (evt: ClipboardEvent) => {
         let ev = new ClipboardInfo(ClipboardEventTypes.CUT, evt);
         let ev = new ClipboardInfo(ClipboardEventTypes.CUT, evt);
-        this._host.onClipboardObserver.notifyObservers(ev);
+        this._host.onClipboardObservable.notifyObservers(ev);
         evt.preventDefault();
         evt.preventDefault();
     }
     }
     /** @hidden */
     /** @hidden */
     private onClipboardPaste = (evt: ClipboardEvent) => {
     private onClipboardPaste = (evt: ClipboardEvent) => {
         let ev = new ClipboardInfo(ClipboardEventTypes.PASTE, evt);
         let ev = new ClipboardInfo(ClipboardEventTypes.PASTE, evt);
-        this._host.onClipboardObserver.notifyObservers(ev);
+        this._host.onClipboardObservable.notifyObservers(ev);
     }
     }
 
 
    /**
    /**

+ 20 - 18
gui/src/2D/controls/inputText.ts

@@ -291,7 +291,7 @@ export class InputText extends Control implements IFocusableControl {
 
 
         this.onBlurObservable.notifyObservers(this);
         this.onBlurObservable.notifyObservers(this);
 
 
-        this.unRegisterClipboardEvents();
+        this._host.unRegisterClipboardEvents();
     }
     }
 
 
     /** @hidden */
     /** @hidden */
@@ -317,7 +317,7 @@ export class InputText extends Control implements IFocusableControl {
             return;
             return;
         }
         }
 
 
-        this.registerClipboardEvents();
+        this._host.registerClipboardEvents();
 
 
     }
     }
 
 
@@ -484,46 +484,49 @@ export class InputText extends Control implements IFocusableControl {
     /** @hidden */
     /** @hidden */
     public processKeyboard(evt: KeyboardEvent | PointerEvent | ClipboardEvent): void {
     public processKeyboard(evt: KeyboardEvent | PointerEvent | ClipboardEvent): void {
         // check the event type and call its action
         // check the event type and call its action
-        (evt instanceof KeyboardEvent) ? this.processKey(evt.keyCode, evt.key, evt) :
-         ((evt instanceof PointerEvent) ? this._processDblClick(evt) : this._processClipboardEvent(evt));
+        if (evt instanceof KeyboardEvent) {
+            this.processKey(evt.keyCode, evt.key, evt);
+        }
+        else if (evt instanceof PointerEvent) {
+            this._processDblClick(evt);
+            evt.preventDefault();
+        }
+        else if (evt instanceof ClipboardEvent) {
+            this._processClipboardEvent(evt);
+            evt.preventDefault();
+        }
     }
     }
 
 
     /**
     /**
      * Callback in case of copy event, can be configured.
      * Callback in case of copy event, can be configured.
-     * @param {ClipboardEvent} ev Defines the Clipboard event.
      */
      */
     public onCopyText(ev: ClipboardEvent): void {
     public onCopyText(ev: ClipboardEvent): void {
         this._isTextHighlightOn = false;
         this._isTextHighlightOn = false;
         ev.clipboardData.setData("text/plain", this._highlightedText);
         ev.clipboardData.setData("text/plain", this._highlightedText);
-        this._host.clipboardData = ev.clipboardData;
-        console.log("copy succcessful");
+        this._host.clipboardData = this._highlightedText;
     }
     }
     /**
     /**
      * Callback in case of cut event, can be configured.
      * Callback in case of cut event, can be configured.
-     * @param {ClipboardEvent} ev Defines the Clipboard event.
      */
      */
     public onCutText(ev: ClipboardEvent): void {
     public onCutText(ev: ClipboardEvent): void {
         this.text = this._text.replace(this._highlightedText, "");
         this.text = this._text.replace(this._highlightedText, "");
         this._isTextHighlightOn = false;
         this._isTextHighlightOn = false;
         ev.clipboardData.setData("text/plain", this._highlightedText);
         ev.clipboardData.setData("text/plain", this._highlightedText);
-        this._host.clipboardData = ev.clipboardData;
-        console.log("cut succcessful");
+        this._host.clipboardData = this._highlightedText;
     }
     }
     /**
     /**
      * Callback in case of paste event, can be configured.
      * Callback in case of paste event, can be configured.
-     * @param {ClipboardEvent} ev Defines the Clipboard event.
      */
      */
     public onPasteText(ev: ClipboardEvent): void {
     public onPasteText(ev: ClipboardEvent): void {
         let data: string = "";
         let data: string = "";
-        if (ev.clipboardData) {
+        if (ev.clipboardData && ev.clipboardData.types.indexOf("text/plain") !== -1) {
             data = ev.clipboardData.getData("text/plain");
             data = ev.clipboardData.getData("text/plain");
-            let insertPosition = this._text.length - this._cursorOffset;
-            this.text = this._text.slice(0, insertPosition) + data + this._text.slice(insertPosition);
-            console.log("paste succcessful");
         }
         }
-        if (this._host.clipboardData) {
-            this._host.clipboardData.setData("text/plain", data);
+        else {
+            data = this._host.clipboardData;
         }
         }
+        let insertPosition = this._text.length - this._cursorOffset;
+        this.text = this._text.slice(0, insertPosition) + data + this._text.slice(insertPosition);
     }
     }
 
 
     /**
     /**
@@ -740,6 +743,5 @@ export class InputText extends Control implements IFocusableControl {
         this.onBlurObservable.clear();
         this.onBlurObservable.clear();
         this.onFocusObservable.clear();
         this.onFocusObservable.clear();
         this.onTextChangedObservable.clear();
         this.onTextChangedObservable.clear();
-        // this.onClipboardObservable.clear();
     }
     }
 }
 }