|
| 1 | +import React, { useState, useEffect } from 'react' |
| 2 | +import FormSaveOrReview from './FormSaveOrReview' |
| 3 | +import * as Html from '../../Services/Html' |
| 4 | +import * as Text from '../../Services/Text' |
| 5 | +import { getIssueHtml } from '../../Services/Html' |
| 6 | + |
| 7 | +export default function InlineCSSForm ({ |
| 8 | + t, |
| 9 | + settings, |
| 10 | + activeIssue, |
| 11 | + handleIssueSave, |
| 12 | + isDisabled, |
| 13 | + handleActiveIssue, |
| 14 | + markAsReviewed, |
| 15 | + setMarkAsReviewed |
| 16 | + }) { |
| 17 | + |
| 18 | + const [isStyleToggleChecked, setisStyleToggleChecked] = useState(false) |
| 19 | + const [isImportantToggleChecked, setIsImportantToggleChecked] = useState(false) |
| 20 | + |
| 21 | + const [formErrors, setFormErrors] = useState([]) |
| 22 | + |
| 23 | + const getInlineCSS = () => { |
| 24 | + let html = activeIssue.sourceHtml |
| 25 | + if(activeIssue.status > 0 && activeIssue.newHtml){ |
| 26 | + html = activeIssue.newHtml |
| 27 | + } |
| 28 | + |
| 29 | + let element = Html.toElement(html) |
| 30 | + |
| 31 | + let elementInlineCSS = element.style; |
| 32 | + let elementInlineCSSText = elementInlineCSS.cssText |
| 33 | + elementInlineCSSText = elementInlineCSSText.split(";") |
| 34 | + return elementInlineCSSText |
| 35 | + } |
| 36 | + |
| 37 | + const handleRemoveInlineStylesCheck = () => { |
| 38 | + setisStyleToggleChecked(!isStyleToggleChecked) |
| 39 | + } |
| 40 | + |
| 41 | + const handleImportantCheckbox = () => { |
| 42 | + setIsImportantToggleChecked(!isImportantToggleChecked) |
| 43 | + } |
| 44 | + |
| 45 | + useEffect (() => { |
| 46 | + updateHtmlContent() |
| 47 | + checkFormErrors() |
| 48 | + }, [isStyleToggleChecked, isImportantToggleChecked, markAsReviewed]) |
| 49 | + |
| 50 | + const updateHtmlContent = () => { |
| 51 | + let issue = activeIssue |
| 52 | + issue.isModified = true |
| 53 | + |
| 54 | + if (markAsReviewed) { |
| 55 | + issue.newHtml = issue.initialHtml |
| 56 | + handleActiveIssue(issue) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + let html = getIssueHtml(activeIssue) |
| 61 | + let updatedElement = Html.toElement(html) |
| 62 | + |
| 63 | + let updatedInlineStyles = removeInlineStyles() |
| 64 | + |
| 65 | + updatedElement.setAttribute("style", updatedInlineStyles.join(';')) |
| 66 | + issue.newHtml = Html.toString(updatedElement) |
| 67 | + handleActiveIssue(issue) |
| 68 | + } |
| 69 | + |
| 70 | + const checkFormErrors = () => { |
| 71 | + let tempErrors = [] |
| 72 | + |
| 73 | + if(!isStyleToggleChecked && !isImportantToggleChecked){ |
| 74 | + tempErrors.push({text:'', type: 'error'}) |
| 75 | + } |
| 76 | + |
| 77 | + setFormErrors(tempErrors) |
| 78 | + } |
| 79 | + |
| 80 | + const removeInlineStyles = () => { |
| 81 | + let tempstyles = getInlineCSS() |
| 82 | + |
| 83 | + if(isStyleToggleChecked){ |
| 84 | + let isInStylesLH = tempstyles.findIndex(item => item.includes("line-height")); |
| 85 | + if(isInStylesLH>-1){ |
| 86 | + tempstyles.splice(isInStylesLH, 1) |
| 87 | + } |
| 88 | + let isInStylesWS = tempstyles.findIndex(item => item.includes("word-spacing")); |
| 89 | + if(isInStylesWS>-1){ |
| 90 | + tempstyles.splice(isInStylesWS, 1) |
| 91 | + } |
| 92 | + let isInStylesLS = tempstyles.findIndex(item => item.includes("letter-spacing")); |
| 93 | + if(isInStylesLS>-1){ |
| 94 | + tempstyles.splice(isInStylesLS, 1) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if(isImportantToggleChecked){ |
| 99 | + let isInStylesLH = tempstyles.findIndex(item => item.includes("line-height")); |
| 100 | + if(isInStylesLH>-1){ |
| 101 | + tempstyles[isInStylesLH] = tempstyles[isInStylesLH].replace('!important', '').trim() |
| 102 | + } |
| 103 | + let isInStylesWS = tempstyles.findIndex(item => item.includes("word-spacing")); |
| 104 | + if(isInStylesWS>-1){ |
| 105 | + tempstyles[isInStylesWS] = tempstyles[isInStylesWS].replace('!important', '').trim() |
| 106 | + } |
| 107 | + let isInStylesLS = tempstyles.findIndex(item => item.includes("letter-spacing")); |
| 108 | + if(isInStylesLS>-1){ |
| 109 | + tempstyles[isInStylesLS] = tempstyles[isInStylesLS].replace('!important', '').trim() |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return tempstyles |
| 114 | + } |
| 115 | + |
| 116 | + const updateCheckboxes = () => { |
| 117 | + let tempstyles = getInlineCSS() |
| 118 | + |
| 119 | + let isInStylesLH = tempstyles.findIndex(item => item.includes("line-height")); |
| 120 | + let isInStylesWS = tempstyles.findIndex(item => item.includes("word-spacing")); |
| 121 | + let isInStylesLS = tempstyles.findIndex(item => item.includes("letter-spacing")); |
| 122 | + let tempimp = "!important" |
| 123 | + |
| 124 | + if((isInStylesLH<=-1 && isInStylesWS<=-1 && isInStylesLS<=-1)){ |
| 125 | + setisStyleToggleChecked(true) |
| 126 | + } |
| 127 | + else{ |
| 128 | + setisStyleToggleChecked(false) |
| 129 | + } |
| 130 | + |
| 131 | + if( |
| 132 | + (tempstyles[isInStylesLH]?.includes(tempimp))|| |
| 133 | + (tempstyles[isInStylesWS]?.includes(tempimp))|| |
| 134 | + (tempstyles[isInStylesLS]?.includes(tempimp))) { |
| 135 | + setIsImportantToggleChecked(false) |
| 136 | + } |
| 137 | + else{ |
| 138 | + setIsImportantToggleChecked(true) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + useEffect(() => { |
| 143 | + if (!activeIssue) { |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + updateCheckboxes() |
| 148 | + checkFormErrors() |
| 149 | + }, [activeIssue]) |
| 150 | + |
| 151 | + const handleSubmit = () => { |
| 152 | + if (markAsReviewed || formErrors.length === 0) { |
| 153 | + handleIssueSave(activeIssue) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return ( |
| 158 | + <> |
| 159 | + <div className="flex-row justify-content-start gap-1 mt-2"> |
| 160 | + <input |
| 161 | + type="checkbox" |
| 162 | + id="removeInlineStylesCheck" |
| 163 | + name="removeInlineStylesCheck" |
| 164 | + tabIndex="0" |
| 165 | + disabled={isDisabled} |
| 166 | + checked={isStyleToggleChecked} |
| 167 | + onChange={handleRemoveInlineStylesCheck} /> |
| 168 | + <label htmlFor="removeInlineStylesCheck" className="instructions">{t('form.invalid_css.label.remove_style')}</label> |
| 169 | + </div> |
| 170 | + <div className="flex-row justify-content-start gap-1 mt-2"> |
| 171 | + <input |
| 172 | + type="checkbox" |
| 173 | + id="removeImportantStylesCheck" |
| 174 | + name="removeImportantStylesCheck" |
| 175 | + tabIndex="0" |
| 176 | + disabled={isDisabled} |
| 177 | + checked={isImportantToggleChecked} |
| 178 | + onChange={handleImportantCheckbox} /> |
| 179 | + <label htmlFor="removeImportantStylesCheck" className="instructions">{t('form.invalid_css.label.remove_important')}</label> |
| 180 | + </div> |
| 181 | + <FormSaveOrReview |
| 182 | + t={t} |
| 183 | + settings={settings} |
| 184 | + activeIssue={activeIssue} |
| 185 | + isDisabled={isDisabled} |
| 186 | + handleSubmit={handleSubmit} |
| 187 | + formErrors={formErrors} |
| 188 | + markAsReviewed={markAsReviewed} |
| 189 | + setMarkAsReviewed={setMarkAsReviewed} /> |
| 190 | + </> |
| 191 | + ) |
| 192 | +} |
0 commit comments