Skip to content

Commit

Permalink
ok, oops, this one is supposed to be StyleMisuse
Browse files Browse the repository at this point in the history
  • Loading branch information
panbed committed Jan 16, 2025
1 parent 712d294 commit eddadac
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 1 deletion.
183 changes: 183 additions & 0 deletions assets/js/Components/Forms/StyleMisuseForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import React, { act, useEffect, useState } from 'react'
import { View } from '@instructure/ui-view'
import { TextInput } from '@instructure/ui-text-input'
import { Button } from '@instructure/ui-buttons'
import { IconCheckMarkLine } from '@instructure/ui-icons'
import { Checkbox } from '@instructure/ui-checkbox'
import { Spinner } from '@instructure/ui-spinner'
import * as Html from '../../Services/Html'
import * as Contrast from '../../Services/Contrast'

export default function StyleMisuseForm(props) {
const [useBold, setUseBold] = useState(isBold())
const [useItalics, setUseItalics] = useState(isItalicized())
const [removeStyling, setRemoveStyling] = useState(false)
const [checkBoxErrors, setCheckBoxErrors] = useState([])
const [styleAttribute, setStyleAttribute] = useState(Html.getAttribute(Html.getIssueHtml(props.activeIssue), "style"))

console.log(styleAttribute)

let formErrors = []

useEffect(() => {
updatePreview()
}, [])

useEffect(() => {
setUseBold(isBold())
setUseItalics(isItalicized())
setCheckBoxErrors([])

formErrors = []
}, [props.activeIssue])

useEffect(() => {
updatePreview()
}, [useBold, useItalics, removeStyling])

function handleBoldToggle() {
setUseBold(!useBold)
updatePreview()
}

function handleItalicsToggle() {
setUseItalics(!useItalics)
updatePreview()
}

function handleStyleToggle() {
setRemoveStyling(!removeStyling)
console.log("style tag:")
console.log(styleAttribute)
updatePreview()
}

function handleSubmit() {
let issue = props.activeIssue

if (cssEmphasisIsValid(issue)) {
let issue = props.activeIssue
issue.newHtml = Contrast.convertHtmlRgb2Hex(issue.newHtml)
props.handleIssueSave(issue)
}
else {
// push errors
formErrors = []
formErrors.push({ text: `${props.t('form.contrast.must_select')}` , type: 'error' })

setCheckBoxErrors(formErrors)
}
}

function processHtml(html) {
let element = Html.toElement(html)

// Clean up tags
Html.removeTag(element, 'strong')
Html.removeTag(element, 'em')

element.innerHTML = (useBold) ? `<strong>${element.innerHTML}</strong>` : element.innerHTML
element.innerHTML = (useItalics) ? `<em>${element.innerHTML}</em>` : element.innerHTML

if (removeStyling) {
Html.removeAttribute(element, "style")
}
else {
Html.setAttribute(element, "style", styleAttribute)
}

return Html.toString(element)
}

function updatePreview() {
let issue = props.activeIssue
const html = Html.getIssueHtml(props.activeIssue)

issue.newHtml = processHtml(html)
props.handleActiveIssue(issue)
}

function isBold() {
const issue = props.activeIssue
const metadata = (issue.metadata) ? JSON.parse(issue.metadata) : {}
const html = Html.getIssueHtml(props.activeIssue)
const element = Html.toElement(html)

return ((Html.hasTag(element, 'strong')) || (metadata.fontWeight === 'bold'))
}

function isItalicized() {
const issue = props.activeIssue
const metadata = (issue.metadata) ? JSON.parse(issue.metadata) : {}
const html = Html.getIssueHtml(props.activeIssue)
const element = Html.toElement(html)

return ((Html.hasTag(element, 'em')) || (metadata.fontStyle == 'italic'))
}

function hasStyleTag() {
const html = Html.getIssueHtml(props.activeIssue)
const element = Html.toElement(html)

console.log("checking style attribute")
console.log(Html.getAttribute(element, "style"))

return true

// return (Html.getAttribute(element, "style") != null)
}

function cssEmphasisIsValid(issue) {
if (issue.scanRuleId === 'style_color_misuse') {
if (!useBold && !useItalics) {
return false
}
}
return true
}

const pending = (props.activeIssue && (props.activeIssue.pending == '1'))
const buttonLabel = (pending) ? 'form.processing' : 'form.submit'

return (
<View as="div" padding="0 x-small">
<div id="flash-messages" role="alert"></div>
<View as="div" margin="small 0">
<Checkbox label={props.t('form.contrast.bolden_text')}
checked={useBold}
onChange={handleBoldToggle}>
</Checkbox>
</View>

<View as="div" margin="small 0">
<Checkbox label={props.t('form.contrast.italicize_text')}
checked={useItalics}
onChange={handleItalicsToggle}
messages={checkBoxErrors}>
</Checkbox>
</View>

<View as="div" margin="small 0">
{/* TOOD: use props.t */}
<Checkbox label={"Remove styling from element"}
checked={removeStyling}
onChange={handleStyleToggle}
messages={checkBoxErrors}>
</Checkbox>
</View>

<View as="div" margin="medium 0">
<Button color="primary" onClick={handleSubmit} interaction={(!pending && props.activeIssue.status !== 2) ? 'enabled' : 'disabled'}>
{('1' == pending) && <Spinner size="x-small" renderTitle={buttonLabel} />}
{props.t(buttonLabel)}
</Button>
{props.activeIssue.recentlyUpdated &&
<View margin="0 small">
<IconCheckMarkLine color="success" />
<View margin="0 x-small">{props.t('label.fixed')}</View>
</View>
}
</View>
</View>
)
}
4 changes: 4 additions & 0 deletions assets/js/Services/Ufixit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Video from '../Components/Forms/Video'
import LinkForm from '../Components/Forms/LinkForm'
import EmphasisForm from '../Components/Forms/EmphasisForm'
import LabelForm from '../Components/Forms/LabelForm'
import QuoteForm from '../Components/Forms/QuoteForm'
import StyleMisuseForm from '../Components/Forms/StyleMisuseForm'

const UfixitForms = {
// phpAlly rules
Expand Down Expand Up @@ -39,6 +41,8 @@ const UfixitForms = {
text_contrast_sufficient: ContrastForm,
text_block_heading: HeadingStyleForm,
heading_content_exists: HeadingEmptyForm,
// text_quoted_correctly: QuoteForm,
style_color_misuse: StyleMisuseForm,
}

export function returnIssueForm(activeIssue) {
Expand Down
2 changes: 1 addition & 1 deletion translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@
"rule.desc.VideosHaveAutoGeneratedCaptions": "<p>Captions that are machine-generated by a service like YouTube are rarely if ever fully accurate and should not be relied upon for educational use.</p>",

"rule.desc.text_contrast_sufficient": "<p>Text color should be easily viewable and should not be the only indicator of meaning or function. Color balance should have at least a 4.5:1 ratio for small text and 3:1 ratio for large text. Warning: using UDOIT to fix one section of text may invalidate the contrast in nested sections of text that are not the same color.</p>",

"rule.example.AnchorLinksToSoundFilesNeedTranscripts": "<h5>Wrong</h5><code>&lt;a href='interview.mp3'&gt;Listen to the interview&lt;/a&gt;</code><h5>Right</h5><code>&lt;a href='interview.mp3'&gt;Listen to the interview&lt;/a&gt; &lt;a href='transcript.html'&gt;(transcript)&lt;/a&gt;</code>",
"rule.example.BlinkIsNotUsed": "<h5>Wrong</h5><p><code>&lt;blink&gt;Please read me!&lt;/blink&gt;</code></p><h5>Right</h5><p><code>&lt;strong&gt;Please read me!&lt;/strong&gt;</code></p>",
"rule.example.ImageAltNotPlaceholder": "<h5>Wrong</h5><p><code>&lt;img src='dog.jpg' alt='image'&gt;</code></p><h5>Right</h5><p><code>&lt;img src='dog.jpg' alt='A photograph of a dog'&gt;</code></p>",
Expand Down

0 comments on commit eddadac

Please sign in to comment.