'use strict';(function(f){"object"==typeof exports&&"object"==typeof module?f(require("../../lib/codemirror"),require("./matchesonscrollbar")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","./matchesonscrollbar"],f):f(CodeMirror)})(function(f){function q(a){this.options={};for(var b in g)this.options[b]=(a&&a.hasOwnProperty(b)?a:g)[b];this.matchesonscroll=this.overlay=this.timeout=null;this.active=!1}function h(a){var b=a.state.matchHighlighter;(b.active||a.hasFocus())&&k(a, b)}function l(a){var b=a.state.matchHighlighter;b.active||(b.active=!0,k(a,b))}function k(a,b){clearTimeout(b.timeout);b.timeout=setTimeout(function(){m(a)},b.options.delay)}function n(a,b,d,c){var e=a.state.matchHighlighter;a.addOverlay(e.overlay=r(b,d,c));e.options.annotateScrollbar&&a.showMatchesOnScrollbar&&(e.matchesonscroll=a.showMatchesOnScrollbar(d?new RegExp("\\b"+b+"\\b"):b,!1,{className:"CodeMirror-selection-highlight-scrollbar"}))}function p(a){var b=a.state.matchHighlighter;b.overlay&& (a.removeOverlay(b.overlay),b.overlay=null,b.matchesonscroll&&(b.matchesonscroll.clear(),b.matchesonscroll=null))}function m(a){a.operation(function(){var b=a.state.matchHighlighter;p(a);if(!a.somethingSelected()&&b.options.showToken){for(var d=!0===b.options.showToken?/[\w$]/:b.options.showToken,c=a.getCursor(),e=a.getLine(c.line),f=c=c.ch;c&&d.test(e.charAt(c-1));)--c;for(;f=b.options.minChars&&n(a,d,!1,b.options.style))}})}function r(a,b,d){return{token:function(c){var e;if(e=c.match(a))(e= !b)||(e=(!c.start||!b.test(c.string.charAt(c.start-1)))&&(c.pos==c.string.length||!b.test(c.string.charAt(c.pos))));if(e)return d;c.next();c.skipTo(a.charAt(0))||c.skipToEnd()}}}var g={style:"matchhighlight",minChars:2,delay:100,wordsOnly:!1,annotateScrollbar:!1,showToken:!1,trim:!0};f.defineOption("highlightSelectionMatches",!1,function(a,b,d){d&&d!=f.Init&&(p(a),clearTimeout(a.state.matchHighlighter.timeout),a.state.matchHighlighter=null,a.off("cursorActivity",h),a.off("focus",l));if(b){b=a.state.matchHighlighter= new q(b);if(a.hasFocus())b.active=!0,m(a);else a.on("focus",l);a.on("cursorActivity",h)}})});import { Context as TemplatesContext } from '../context/templates'; import useScreenshot, { SCREENSHOT_STATUS_SUCCEED, SCREENSHOT_STATUS_FAILED } from 'modules/screenshots/app/assets/js/hooks/use-screenshot'; /** * Wrapper function that was made to take screenshots specific for template. * it will capture a screenshot and update the templates context with the new screenshot. * * @param {any} templateType */ export default function useTemplatesScreenshot( templateType = null ) { const { updateTemplateItemState, templates } = React.useContext( TemplatesContext ); const templatesForScreenshot = Object.values( templates ).filter( ( template ) => shouldScreenshotTemplate( template, templateType ), ); // Start to capture screenshots. const screenshot = useScreenshot( templatesForScreenshot ); // Update the thumbnail url when screenshot created. React.useEffect( () => { screenshot.posts .filter( ( post ) => post.status === SCREENSHOT_STATUS_SUCCEED ) .forEach( ( post ) => updateTemplateItemState( post.id, { thumbnail: post.imageUrl } ) ); }, [ screenshot.succeed ] ); // Update the screenshot url that was failed. // When the user will hit the route on the second time it will avoid trying to take another screenshot. React.useEffect( () => { screenshot.posts .filter( ( post ) => post.status === SCREENSHOT_STATUS_FAILED ) .forEach( ( post ) => updateTemplateItemState( post.id, { screenshot_url: null } ) ); }, [ screenshot.failed ] ); return screenshot; } /** * Filter handler. * will remove all the drafts and private and also will filter by template type if exists. * * @param {any} template * @param {any} templateType * @return {boolean} should screenshot template */ function shouldScreenshotTemplate( template, templateType = null ) { if ( templateType ) { return false; } return 'publish' === template.status && ! template.thumbnail && template.screenshot_url; }