-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add initial tooltip.js * Initial computeTooltipPosition * Use Popover component * Naming update * Address warnings * Move useEffect logic to a method for consistency * Use property to store tooltip count * Remove className * Add guard to prevent displaying tooltip if toolbar is closed * Address php linter * Proprogate property * Color popover arrow and add offset * Rewrite endpoints set up as a function for consistency * Address linter * Increment property via window object * Use camelCasing and improve readability * Concentrate all tooltip logic inside CustomTooltip for better code hygene * Disable selector-class-pattern globally * Track whether the tooltip has been displayed for the full 5 secs * Address linter * Clear timeouts if user clicks on W before the tooltip diplays * Address linter * Add comments for php properties
- Loading branch information
Showing
7 changed files
with
239 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"selector-class-pattern": null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* global wikipediapreviewCustomTooltip */ | ||
import { __ } from '@wordpress/i18n'; | ||
import { Popover } from '@wordpress/components'; | ||
import { useEffect, useState } from '@wordpress/element'; | ||
|
||
export const CustomTooltip = ( { | ||
anchorRef, | ||
addingPreview, | ||
} ) => { | ||
const [ displayTooltip, setDisplayTooltip ] = useState( false ); | ||
const [ timeoutIds, setTimeoutIds ] = useState( [] ); | ||
const tooltipDisplayedFullDuration = useState( parseInt( wikipediapreviewCustomTooltip.tooltipDuration ) )[ 0 ]; | ||
const tooltipDisplayedCount = useState( parseInt( wikipediapreviewCustomTooltip.tooltipCount ) )[ 0 ]; | ||
const tooltipDisplayedLimit = 2; | ||
|
||
const updateStoredProperty = ( prop ) => { | ||
fetch( `/wp-json/wikipediapreview/v1/${ prop }/`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
} ); | ||
}; | ||
|
||
const finishDisplayingTooltip = () => { | ||
setDisplayTooltip( false ); | ||
wikipediapreviewCustomTooltip.tooltipDuration = 1; | ||
updateStoredProperty( 'duration' ); | ||
}; | ||
|
||
const clearTimeouts = () => { | ||
timeoutIds.forEach( ( id ) => { | ||
clearTimeout( id ); | ||
} ); | ||
}; | ||
|
||
const waitOneSecThenDisplayTooltip = () => { | ||
const oneSecId = setTimeout( () => { | ||
if ( anchorRef.current ) { | ||
setDisplayTooltip( true ); | ||
updateStoredProperty( 'count' ); | ||
// Increment global tooltip count directly as well | ||
// to ensure count is up to date in between page reloads | ||
wikipediapreviewCustomTooltip.tooltipCount = tooltipDisplayedCount + 1; | ||
waitFiveSecsThenHideTooltip(); | ||
} | ||
}, 1000 ); | ||
setTimeoutIds( ( timeoutIds ) => [ ...timeoutIds, oneSecId ] ); | ||
}; | ||
|
||
const waitFiveSecsThenHideTooltip = () => { | ||
const fiveSecId = setTimeout( () => { | ||
finishDisplayingTooltip(); | ||
}, 5000 ); | ||
setTimeoutIds( ( timeoutIds ) => [ ...timeoutIds, fiveSecId ] ); | ||
}; | ||
|
||
useEffect( () => { | ||
if ( tooltipDisplayedCount < tooltipDisplayedLimit && tooltipDisplayedFullDuration < 1 ) { | ||
waitOneSecThenDisplayTooltip(); | ||
} | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
// Clear all timeouts when unmounting | ||
return () => { | ||
clearTimeouts(); | ||
}; | ||
}, [ timeoutIds ] ); | ||
|
||
useEffect( () => { | ||
if ( addingPreview ) { | ||
clearTimeouts(); | ||
finishDisplayingTooltip(); | ||
} | ||
}, [ addingPreview ] ); | ||
|
||
return ( | ||
<div> | ||
{ displayTooltip && ( | ||
<Popover | ||
anchor={ anchorRef.current } | ||
placement={ 'top' } | ||
noArrow={ false } | ||
offset={ 10 } | ||
className="wikipediapreview-edit-tooltip-popover" | ||
> | ||
<div className="wikipediapreview-edit-tooltip-popover-content"> | ||
<p className="wikipediapreview-edit-tooltip-popover-text">{ __( 'Add Wikipedia Preview' ) }</p> | ||
</div> | ||
</Popover> | ||
) } | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/* | ||
* The number of times the tooltip has been displayed, whether in full duration or not. | ||
* This count can be incremented until we hit a limit (set on the client side). | ||
*/ | ||
DEFINE( 'WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_COUNT', 'wikipediapreview_tooltip_count' ); | ||
|
||
/* | ||
* Whether or not the tooltip has been displayed in full duration at least once, | ||
* this acts as a boolean that's either 0 or 1. When set to 1, we infer the | ||
* Wikipedia Preview plugin has been 'discovered' and hence we do not display it again | ||
*/ | ||
DEFINE( 'WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_DURATION', 'wikipediapreview_tooltip_duration' ); | ||
|
||
function wikipediapreview_get_tooltip_count() { | ||
return get_option( | ||
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_COUNT, | ||
0 | ||
); | ||
} | ||
|
||
function wikipediapreview_increment_tooltip_count() { | ||
$count = wikipediapreview_get_tooltip_count(); | ||
update_option( | ||
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_COUNT, | ||
$count + 1 | ||
); | ||
} | ||
|
||
function wikipediapreview_update_tooltip_duration() { | ||
update_option( | ||
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_DURATION, | ||
1 | ||
); | ||
} | ||
|
||
// For debugging purposes | ||
function wikipediapreview_reset_tooltip_properties() { | ||
update_option( | ||
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_COUNT, | ||
0 | ||
); | ||
update_option( | ||
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_DURATION, | ||
0 | ||
); | ||
} | ||
|
||
|
||
function wikipediapreview_set_rest_endpoint() { | ||
register_rest_route( | ||
'wikipediapreview/v1', | ||
'/count/', | ||
array( | ||
'methods' => 'POST', | ||
'callback' => 'wikipediapreview_increment_tooltip_count', | ||
) | ||
); | ||
register_rest_route( | ||
'wikipediapreview/v1', | ||
'/duration/', | ||
array( | ||
'methods' => 'POST', | ||
'callback' => 'wikipediapreview_update_tooltip_duration', | ||
) | ||
); | ||
register_rest_route( | ||
'wikipediapreview/v1', | ||
'/reset/', | ||
array( | ||
'methods' => 'POST', | ||
'callback' => 'wikipediapreview_reset_tooltip_properties', | ||
) | ||
); | ||
} | ||
|
||
function wikipediapreview_tooltip_enqueue_script() { | ||
$src_link_dir = plugin_dir_url( __FILE__ ) . 'src/link'; | ||
$no_dependencies = array(); | ||
$in_footer = true; | ||
|
||
wp_enqueue_script( | ||
'wikipedia-preview-tooltip', | ||
$src_link_dir . 'tooltip.js', | ||
$no_dependencies, | ||
WIKIPEDIA_PREVIEW_PLUGIN_VERSION, | ||
$in_footer | ||
); | ||
|
||
$options = array( | ||
'tooltipCount' => wikipediapreview_get_tooltip_count(), | ||
'tooltipDuration' => get_option( WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_DURATION, 0 ), | ||
); | ||
|
||
wp_localize_script( 'wikipedia-preview-tooltip', 'wikipediapreviewCustomTooltip', $options ); | ||
} | ||
|
||
add_action( 'enqueue_block_editor_assets', 'wikipediapreview_tooltip_enqueue_script' ); | ||
add_action( 'rest_api_init', 'wikipediapreview_set_rest_endpoint' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters