Angular 7 - How does work the HTML5 Fullscreen API ? I've a lot of errors
Asked Answered
S

2

9

I use Angular 7 and I would like have a button for put my app in fullscreen. I use the HTML5 Fullscreen API and I've make 2 functions :

openfullscreen() {
    // Trigger fullscreen
    console.log('gg');
    if (document.documentElement.requestFullscreen) {
      document.documentElement.requestFullscreen();
    } else if (document.documentElement.mozRequestFullScreen) { /* Firefox */
      document.documentElement.mozRequestFullScreen();
    } else if (document.documentElement.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
      document.documentElement.webkitRequestFullscreen();
    } else if (document.documentElement.msRequestFullscreen) { /* IE/Edge */
      document.documentElement.msRequestFullscreen();
    }
    this.isfullscreen = true;
  }

  closefullscreen(){
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.mozCancelFullScreen) { /* Firefox */
      document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
      document.webkitExitFullscreen();
    } else if (document.msExitFullscreen) { /* IE/Edge */
      document.msExitFullscreen();
    }
    this.isfullscreen = false;
  }

It worked in the beginning but I had a lot of error :

error TS2339: Property 'mozRequestFullScreen' does not exist on type 'HTMLElement'.

error TS2339: Property 'mozRequestFullScreen' does not exist on type 'HTMLElement'.

error TS2339: Property 'webkitRequestFullscreen' does not exist on type 'HTMLElement'.

error TS2339: Property 'webkitRequestFullscreen' does not exist on type 'HTMLElement'.

error TS2551: Property 'msRequestFullscreen' does not exist on type 'HTMLElement'. Did you mean 'requestFullscreen'?

error TS2551: Property 'msRequestFullscreen' does not exist on type 'HTMLElement'. Did you mean 'requestFullscreen'?

error TS2339: Property 'mozCancelFullScreen' does not exist on type 'Document'.

error TS2339: Property 'mozCancelFullScreen' does not exist on type 'Document'.

error TS2339: Property 'webkitExitFullscreen' does not exist on type 'Document'.

error TS2339: Property 'webkitExitFullscreen' does not exist on type 'Document'.

error TS2551: Property 'msExitFullscreen' does not exist on type 'Document'. Did you mean 'exitFullscreen'?

error TS2551: Property 'msExitFullscreen' does not exist on type 'Document'. Did you mean 'exitFullscreen'?

When I restart my code I've the same errors but, in addition, I've that :

Failed to compile.

And my app don't work. How I can compile without errors?

Storekeeper answered 17/1, 2019 at 19:14 Comment(6)
the angular document type doesn't provide those methods or properties. You either need to write a type extension that does provide them or probably just use a lib like screenfull.js which does all this garbage for you. don't reinvent the wheel.Counsel
Thank's, but this can work with the HTML5 API, and the problem is just the Angular cli seen an error, this work but the angular cli stop the compiling.Spitball
Possible duplicate of FullScreen Request on Angular 2/4Clayborne
Read what i said again. The problem is the typing. The accepted answer does exactly what i said, it extends the typing.Counsel
Ok. Excuse me, I did not understand as you were talking about screenfull.js. Sory.Spitball
https://mcmap.net/q/524219/-how-do-i-get-typescript-to-stop-complaining-about-functions-it-doesn-39-t-know-aboutDalessio
T
20

You can tell typescript about the methods that you're going to use by using the as keyword to cast the interface of document and document.documentElement.

Like this:

const docElmWithBrowsersFullScreenFunctions = document.documentElement as HTMLElement & {
  mozRequestFullScreen(): Promise<void>;
  webkitRequestFullscreen(): Promise<void>;
  msRequestFullscreen(): Promise<void>;
};

const docWithBrowsersExitFunctions = document as Document & {
  mozCancelFullScreen(): Promise<void>;
  webkitExitFullscreen(): Promise<void>;
  msExitFullscreen(): Promise<void>;
};

Please note that this just prevents compile error and you still should check if the methods exist like you did.

So your methods will be like this:

openfullscreen() {
  // Trigger fullscreen
  const docElmWithBrowsersFullScreenFunctions = document.documentElement as HTMLElement & {
    mozRequestFullScreen(): Promise<void>;
    webkitRequestFullscreen(): Promise<void>;
    msRequestFullscreen(): Promise<void>;
  };

  if (docElmWithBrowsersFullScreenFunctions.requestFullscreen) {
    docElmWithBrowsersFullScreenFunctions.requestFullscreen();
  } else if (docElmWithBrowsersFullScreenFunctions.mozRequestFullScreen) { /* Firefox */
    docElmWithBrowsersFullScreenFunctions.mozRequestFullScreen();
  } else if (docElmWithBrowsersFullScreenFunctions.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
    docElmWithBrowsersFullScreenFunctions.webkitRequestFullscreen();
  } else if (docElmWithBrowsersFullScreenFunctions.msRequestFullscreen) { /* IE/Edge */
    docElmWithBrowsersFullScreenFunctions.msRequestFullscreen();
  }
  this.isfullscreen = true;
}

closefullscreen(){
  const docWithBrowsersExitFunctions = document as Document & {
    mozCancelFullScreen(): Promise<void>;
    webkitExitFullscreen(): Promise<void>;
    msExitFullscreen(): Promise<void>;
  };
  if (docWithBrowsersExitFunctions.exitFullscreen) {
    docWithBrowsersExitFunctions.exitFullscreen();
  } else if (docWithBrowsersExitFunctions.mozCancelFullScreen) { /* Firefox */
    docWithBrowsersExitFunctions.mozCancelFullScreen();
  } else if (docWithBrowsersExitFunctions.webkitExitFullscreen) { /* Chrome, Safari and Opera */
    docWithBrowsersExitFunctions.webkitExitFullscreen();
  } else if (docWithBrowsersExitFunctions.msExitFullscreen) { /* IE/Edge */
    docWithBrowsersExitFunctions.msExitFullscreen();
  }
  this.isfullscreen = false;
}
Terrigenous answered 17/1, 2019 at 19:35 Comment(1)
@JérémyChamboultou I updated my answer. I didn't test it so please let me know if that worksTerrigenous
C
5

Defining types:

type ExitFullscreen = typeof document.exitFullscreen
type RequestFullscreen = typeof document.documentElement.requestFullscreen

declare global {
  interface Document {
    webkitExitFullscreen: ExitFullscreen;
    mozCancelFullScreen: ExitFullscreen;
    msExitFullscreen: ExitFullscreen;
  }

  interface HTMLElement {
    webkitRequestFullscreen: RequestFullscreen;
    mozRequestFullScreen: RequestFullscreen;
    msRequestFullscreen: RequestFullscreen;
  }
}
Cymar answered 10/5, 2022 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.