2018-05-25 14:30:27 +02:00
const React = require ( 'react' ) ;
2024-04-11 09:35:20 +02:00
import { CSSProperties } from 'react' ;
2018-05-25 14:30:27 +02:00
const { connect } = require ( 'react-redux' ) ;
2018-09-28 20:24:57 +02:00
const { clipboard } = require ( 'electron' ) ;
2023-01-19 19:19:06 +02:00
import ExtensionBadge from './ExtensionBadge' ;
2021-08-07 13:22:37 +02:00
import { themeStyle } from '@joplin/lib/theme' ;
import { _ } from '@joplin/lib/locale' ;
import ClipperServer from '@joplin/lib/ClipperServer' ;
import Setting from '@joplin/lib/models/Setting' ;
2021-08-23 19:47:07 +02:00
import EncryptionService from '@joplin/lib/services/e2ee/EncryptionService' ;
2021-09-04 19:11:29 +02:00
import { AppState } from '../app.reducer' ;
2018-05-25 14:30:27 +02:00
class ClipperConfigScreenComponent extends React . Component {
2023-03-06 16:22:01 +02:00
public constructor ( ) {
2018-09-28 20:24:57 +02:00
super ( ) ;
this . copyToken_click = this . copyToken_click . bind ( this ) ;
}
2023-03-06 16:22:01 +02:00
private disableClipperServer_click() {
2018-05-25 14:30:27 +02:00
Setting . setValue ( 'clipperServer.autoStart' , false ) ;
2021-08-07 13:22:37 +02:00
void ClipperServer . instance ( ) . stop ( ) ;
2018-05-25 14:30:27 +02:00
}
2023-03-06 16:22:01 +02:00
private enableClipperServer_click() {
2018-05-25 14:30:27 +02:00
Setting . setValue ( 'clipperServer.autoStart' , true ) ;
2021-08-07 13:22:37 +02:00
void ClipperServer . instance ( ) . start ( ) ;
2018-05-25 14:30:27 +02:00
}
2023-03-06 16:22:01 +02:00
private copyToken_click() {
2018-09-28 20:24:57 +02:00
clipboard . writeText ( this . props . apiToken ) ;
alert ( _ ( 'Token has been copied to the clipboard!' ) ) ;
}
2023-03-06 16:22:01 +02:00
private renewToken_click() {
2021-04-07 20:27:16 +02:00
if ( confirm ( _ ( 'Are you sure you want to renew the authorisation token?' ) ) ) {
void EncryptionService . instance ( )
. generateApiToken ( )
2022-09-30 18:23:14 +02:00
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
2021-06-22 20:57:04 +02:00
. then ( ( token ) = > {
2021-04-07 20:27:16 +02:00
Setting . setValue ( 'api.token' , token ) ;
} ) ;
}
}
2023-03-06 16:22:01 +02:00
public render() {
2020-09-15 15:01:07 +02:00
const theme = themeStyle ( this . props . themeId ) ;
2018-05-25 14:30:27 +02:00
2024-04-11 09:35:20 +02:00
const containerStyle : CSSProperties = { . . . theme . containerStyle ,
overflowY : 'scroll' ,
2022-01-09 17:42:27 +02:00
// padding: theme.configScreenPadding,
2023-06-01 13:02:36 +02:00
backgroundColor : theme.backgroundColor3 } ;
2018-11-08 00:37:13 +02:00
2023-06-01 13:02:36 +02:00
const buttonStyle = { . . . theme . buttonStyle , marginRight : 10 } ;
2018-05-25 14:30:27 +02:00
const stepBoxStyle = {
2019-09-11 01:53:01 +02:00
border : '1px solid' ,
borderColor : theme.dividerColor ,
2018-05-25 14:30:27 +02:00
padding : 15 ,
paddingTop : 0 ,
marginBottom : 15 ,
2018-11-08 00:37:13 +02:00
backgroundColor : theme.backgroundColor ,
2018-05-25 14:30:27 +02:00
} ;
2021-04-07 20:27:16 +02:00
const tokenStyle = {
fontFamily : 'monospace' ,
} ;
2020-03-14 01:46:14 +02:00
const webClipperStatusComps = [ ] ;
2018-05-25 14:30:27 +02:00
if ( this . props . clipperServerAutoStart ) {
2019-07-29 14:13:23 +02:00
webClipperStatusComps . push (
< p key = "text_1" style = { theme . textStyle } >
< b > { _ ( 'The web clipper service is enabled and set to auto-start.' ) } < / b >
2023-08-22 12:58:53 +02:00
< / p > ,
2019-07-29 14:13:23 +02:00
) ;
2018-05-25 14:30:27 +02:00
if ( this . props . clipperServer . startState === 'started' ) {
2019-07-29 14:13:23 +02:00
webClipperStatusComps . push (
< p key = "text_2" style = { theme . textStyle } >
{ _ ( 'Status: Started on port %d' , this . props . clipperServer . port ) }
2023-08-22 12:58:53 +02:00
< / p > ,
2019-07-29 14:13:23 +02:00
) ;
2018-05-25 14:30:27 +02:00
} else {
2019-07-29 14:13:23 +02:00
webClipperStatusComps . push (
< p key = "text_3" style = { theme . textStyle } >
{ _ ( 'Status: %s' , this . props . clipperServer . startState ) }
2023-08-22 12:58:53 +02:00
< / p > ,
2019-07-29 14:13:23 +02:00
) ;
2018-05-25 14:30:27 +02:00
}
2019-07-29 14:13:23 +02:00
webClipperStatusComps . push (
2019-09-11 01:53:01 +02:00
< button key = "disable_button" style = { buttonStyle } onClick = { this . disableClipperServer_click } >
2019-07-29 14:13:23 +02:00
{ _ ( 'Disable Web Clipper Service' ) }
2023-08-22 12:58:53 +02:00
< / button > ,
2019-07-29 14:13:23 +02:00
) ;
2018-05-25 14:30:27 +02:00
} else {
2019-07-29 14:13:23 +02:00
webClipperStatusComps . push (
< p key = "text_4" style = { theme . textStyle } >
{ _ ( 'The web clipper service is not enabled.' ) }
2023-08-22 12:58:53 +02:00
< / p > ,
2019-07-29 14:13:23 +02:00
) ;
webClipperStatusComps . push (
< button key = "enable_button" style = { buttonStyle } onClick = { this . enableClipperServer_click } >
{ _ ( 'Enable Web Clipper Service' ) }
2023-08-22 12:58:53 +02:00
< / button > ,
2019-07-29 14:13:23 +02:00
) ;
2018-05-25 14:30:27 +02:00
}
2024-04-11 09:35:20 +02:00
const apiTokenStyle : CSSProperties = { . . . theme . textStyle ,
color : theme.colorFaded ,
2018-09-28 20:24:57 +02:00
wordBreak : 'break-all' ,
paddingTop : 10 ,
2023-06-01 13:02:36 +02:00
paddingBottom : 10 } ;
2018-09-28 20:24:57 +02:00
2018-05-25 14:30:27 +02:00
return (
< div >
2018-11-08 00:37:13 +02:00
< div style = { containerStyle } >
2020-09-15 15:01:07 +02:00
< div >
2023-06-01 13:02:36 +02:00
< p style = { { . . . theme . textStyle , marginTop : 0 } } > { _ ( 'Joplin Web Clipper allows saving web pages and screenshots from your browser to Joplin.' ) } < / p >
2018-09-28 20:24:57 +02:00
< p style = { theme . textStyle } > { _ ( 'In order to use the web clipper, you need to do the following:' ) } < / p >
< div style = { stepBoxStyle } >
< p style = { theme . h1Style } > { _ ( 'Step 1: Enable the clipper service' ) } < / p >
< p style = { theme . textStyle } > { _ ( 'This service allows the browser extension to communicate with Joplin. When enabling it your firewall may ask you to give permission to Joplin to listen to a particular port.' ) } < / p >
2019-07-29 14:13:23 +02:00
< div > { webClipperStatusComps } < / div >
2018-09-28 20:24:57 +02:00
< / div >
< div style = { stepBoxStyle } >
< p style = { theme . h1Style } > { _ ( 'Step 2: Install the extension' ) } < / p >
< p style = { theme . textStyle } > { _ ( 'Download and install the relevant extension for your browser:' ) } < / p >
2020-02-05 00:09:34 +02:00
< div style = { { display : 'flex' , flexDirection : 'row' } } >
2020-09-15 15:01:07 +02:00
< ExtensionBadge themeId = { this . props . themeId } type = "firefox" url = "https://addons.mozilla.org/en-US/firefox/addon/joplin-web-clipper/" / >
< ExtensionBadge style = { { marginLeft : 10 } } themeId = { this . props . themeId } type = "chrome" url = "https://chrome.google.com/webstore/detail/joplin-web-clipper/alofnhikmmkdbbbgpnglcpdollgjjfek" / >
2018-09-28 20:24:57 +02:00
< / div >
2018-05-25 14:30:27 +02:00
< / div >
2018-09-28 20:24:57 +02:00
< div style = { stepBoxStyle } >
< p style = { theme . h1Style } > { _ ( 'Advanced options' ) } < / p >
< p style = { theme . textStyle } > { _ ( 'Authorisation token:' ) } < / p >
2019-07-29 14:13:23 +02:00
< p style = { apiTokenStyle } >
2021-04-07 20:27:16 +02:00
< span style = { tokenStyle } > { this . props . apiToken } { ' ' } < / span >
2019-07-29 14:13:23 +02:00
< a style = { theme . urlStyle } href = "#" onClick = { this . copyToken_click } >
{ _ ( 'Copy token' ) }
< / a >
< / p >
2018-09-28 20:24:57 +02:00
< p style = { theme . textStyle } > { _ ( 'This authorisation token is only needed to allow third-party applications to access Joplin.' ) } < / p >
2021-04-07 20:27:16 +02:00
< div >
< button key = "renew_button" style = { buttonStyle } onClick = { this . renewToken_click } >
{ _ ( 'Renew token' ) }
< / button >
< / div >
2018-05-25 14:30:27 +02:00
< / div >
< / div >
< / div >
< / div >
) ;
}
}
2021-08-07 13:22:37 +02:00
const mapStateToProps = ( state : AppState ) = > {
2018-05-25 14:30:27 +02:00
return {
2020-09-15 15:01:07 +02:00
themeId : state.settings.theme ,
2018-05-25 14:30:27 +02:00
clipperServer : state.clipperServer ,
clipperServerAutoStart : state.settings [ 'clipperServer.autoStart' ] ,
2018-09-28 20:24:57 +02:00
apiToken : state.settings [ 'api.token' ] ,
2018-05-25 14:30:27 +02:00
} ;
} ;
const ClipperConfigScreen = connect ( mapStateToProps ) ( ClipperConfigScreenComponent ) ;
2021-08-07 13:22:37 +02:00
export default ClipperConfigScreen ;