Skip to content

Commit

Permalink
Update to 1.5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
webaxones committed May 13, 2024
1 parent 554c365 commit 822d89f
Show file tree
Hide file tree
Showing 16 changed files with 540 additions and 370 deletions.
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-edit-post', 'wp-i18n', 'wp-notices', 'wp-plugins'), 'version' => 'a29d6e29e9d190fb2551');
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-edit-post', 'wp-i18n', 'wp-notices', 'wp-plugins'), 'version' => '408be787993bd68bb94f');
2 changes: 1 addition & 1 deletion build/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion consistency.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Consistency
* Plugin URI: https://www.webaxones.com
* Description: Fixes typographical consistency
* Version: 1.5.3
* Version: 1.5.4
* Requires at least: 6.1
* Requires PHP: 7.4
* Author: Loïc Antignac
Expand Down
23 changes: 14 additions & 9 deletions includes/Config/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,25 @@ class Rules implements ValueInterface
*/
protected array $localizedRules = [
'quote' => [ 'fr-FR', 'fr-BE', 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB' ],
'ellipsis' => [ 'fr_FR', 'fr_BE', 'en_US', 'en_AU', 'en_CA', 'en_NZ', 'en_ZA', 'en_GB', 'de_DE', 'de_AT', 'de_CH', 'ro_RO' ],
'breakingSpace' => [ 'fr-FR', 'fr-BE' ],
'2hyphens' => [ 'fr-FR', 'fr-BE', 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB', 'de-DE', 'de-AT', 'de-CH', 'ro-RO' ],
'ordinalNumberSuffix' => [ 'fr-FR', 'fr-BE', 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB' ],
'regularToCurlyQuotes' => [ 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB' ],
'regularToGermanQuotes' => [ 'de_DE', 'de_AT', 'ro_RO' ],
'regularToGermanBookStyleQuotes' => [ 'de_DE', 'de_AT' ],
'regularToGermanQuotes' => [ 'de-DE', 'de-AT', 'ro-RO' ],
'regularToGermanBookStyleQuotes' => [ 'de-DE', 'de-AT' ],
'regularToFrenchQuotes' => [ 'fr-FR', 'fr-BE' ],
'regularToFrenchQuotesWithoutSpaces' => [ 'de_CH' ],
'regularToFrenchQuotesWithoutSpaces' => [ 'de-CH' ],
'breakingSpace' => [ 'fr-FR', 'fr-BE' ],
'noSpaceBefore' => [ 'fr-FR', 'fr-BE' ],
'spaceBefore' => [ 'en_US', 'en_AU', 'en_CA', 'en_NZ', 'en_ZA', 'en_GB', 'de_DE', 'de_AT', 'de_CH', 'ro_RO' ],
'2hyphens' => [ 'fr_FR', 'fr_BE', 'en_US', 'en_AU', 'en_CA', 'en_NZ', 'en_ZA', 'en_GB', 'de_DE', 'de_AT', 'de_CH', 'ro_RO' ],
'ordinalNumberSuffix' => [ 'fr-FR', 'fr-BE', 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB' ],
'spaceBefore' => [ 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB', 'de-DE', 'de-AT', 'de-CH', 'ro-RO' ],
'noBreakingSpaceAfter' => [ 'fr-FR', 'fr-BE', 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB' ],
'noNonBreakingSpaceAfter' => [ 'fr-FR', 'fr-BE' ],
'capitalizeFirstSentenceLetter' => [ 'fr_FR', 'fr_BE', 'en_US', 'en_AU', 'en_CA', 'en_NZ', 'en_ZA', 'en_GB', 'de_DE', 'de_AT', 'de_CH', 'ro_RO' ],
'capitalizeFirstSentenceLetter' => [ 'fr-FR', 'fr-BE', 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB', 'de-DE', 'de-AT', 'de-CH', 'ro-RO' ],
'etcThreeDots' => [ 'fr-FR', 'fr-BE' ],
'etcTwoDots' => [ 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB' ],
'etcEllipsis' => [ 'fr-FR', 'fr-BE' ],
'ellipsis' => [ 'fr-FR', 'fr-BE', 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB', 'de-DE', 'de-AT', 'de-CH', 'ro-RO' ],
'symbolBasedOn1Character' => [ 'fr-FR', 'fr-BE', 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB', 'de-DE', 'de-AT', 'de-CH', 'ro-RO' ],
'symbolBasedOn2Characters' => [ 'fr-FR', 'fr-BE', 'en-US', 'en-AU', 'en-CA', 'en-NZ', 'en-ZA', 'en-GB', 'de-DE', 'de-AT', 'de-CH', 'ro-RO' ],
];

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "consistency",
"version": "1.5.3",
"version": "1.5.4",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: webaxones
Tags: block editor, typography, gutenberg
Requires at least: 6.1
Tested up to: 6.5.2
Stable tag: 1.5.3
Stable tag: 1.5.4
Requires PHP: 7.4
License: GPL-3.0-or-later
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Expand Down Expand Up @@ -67,6 +67,11 @@ The locales supported by this plugin are:

== Changelog ==

= 1.5.4 =
* Sort settings by categories in sidebar
* Add some rules for symbols
* Fix settings saving that was broken in last version

= 1.5.3 =
* Fix missing stable tag

Expand Down
6 changes: 3 additions & 3 deletions src/app/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const { getBlockName, getBlockAttributes } = select( 'core/block-editor' )
export const isUsedByLocale = settingSlug => {

const currentLocale = getCurrentLocale()
const theRegex = rules?.find( x => x.name === settingSlug )
const theRegex = rules?.find( x => x.slug === settingSlug )

if ( undefined !== theRegex && theRegex?.locales?.includes( currentLocale ) ) {
return true
Expand Down Expand Up @@ -81,7 +81,7 @@ export const blockCanTechnicallyBeChecked = currentBlockId => {
*/
export const regDealWithPair = reg => {

if ( regsWithPair.includes( reg.name ) ) {
if ( regsWithPair.includes( reg.slug ) ) {
return true
}
return false
Expand All @@ -94,7 +94,7 @@ export const regDealWithPair = reg => {
*/
export const checkIfAMemoryLeakHasOccuredAndStopProcessing = currentBlockId => {

if ( global.consistencyLoop >= 150 ) {
if ( global.consistencyLoop >= 100 ) {
aMemoryLeakHasOccured( currentBlockId )
}

Expand Down
41 changes: 26 additions & 15 deletions src/app/fixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ const { updateBlock, selectionChange, updateBlockAttributes } = dispatch( 'core/
* @param {boolean} props.isPasting - Indicates whether the content is being pasted.
*/
export const fixIt = props => {

const { currentBlockId, isPasting, settings } = props

// Get the regex of all rules
let theRegs = rules.filter( reg => true === settings?.find( s => s.slug === reg.name )?.value )
let theRegs = rules.filter( reg => true === settings?.find( s => s.slug === reg.slug )?.value )

// Get the current block
const block = getBlock( currentBlockId )
Expand All @@ -48,9 +47,14 @@ export const fixIt = props => {
// Get the attributes of the current block
let blockAttributes = getBlockAttributes( currentBlockId )

// We don't apply several fixes on the same typing event
let contentUpdated = false

// Loop on regular expressions
Object.entries( theRegs ).forEach( ( [ _, reg ] ) => {


// Stop correction if block content isn't concerned by the current site locale (language)
if ( ! isUsedByLocale( reg.slug ) || contentUpdated ) return
global.consistencyLoop ++

// If the loop is too long, we stop it to avoid infinite loop
Expand Down Expand Up @@ -85,21 +89,22 @@ export const fixIt = props => {

// If the rule depends on previous characters, we need to separate the string taking those characters into account
const captureGroups = textContent.match( reg.mask )
if( null === captureGroups || 0 === captureGroups.length ) return
const lengthToGoBack = captureGroups[0].length || 1
console.log('captureGroups', captureGroups);

if( null === captureGroups || 0 === captureGroups.length ) return
const lengthToGoBack = captureGroups[0].length || 1
// Split the string to process only the part from the cursor position to the end
firstPart = blockContent.substring( 0, cursorPositionInsideHTML - lengthToGoBack )
lastPart = blockContent.substring( cursorPositionInsideHTML - lengthToGoBack, blockContent.length )

console.log('firstPart', firstPart);
console.log('lastPart', lastPart);
// If first part of the string matches but not the lastPart, it means that a character has been typed uncorrected voluntarily before, so it should not be taken into account
isConcerned = reg.mask.test( textContent ) && reg.mask.test( lastPart )

}

// Stop correction if block content isn't concerned by the regex nor by the current site locale (language)
if ( ! isConcerned || ! isUsedByLocale( reg.name ) ) return

// Stop correction if block content isn't concerned by the regex
if ( ! isConcerned ) return
// Pairing characters need specific process for the replacement
if ( regDealWithPair( reg ) ) {
replaceWithThis = getReplacementStringForPairs( reg, blockContent, replaceWithThis )
Expand Down Expand Up @@ -127,15 +132,17 @@ export const fixIt = props => {
...block,
attributes: { ...block.attributes, content: blockContent }
} )
contentUpdated = true
}

// Cursor repositioning:
if ( 0 === reg.nbMoved || 0 === cursorPosition || isPasting ) return
if ( 0 === cursorPosition || isPasting ) return

// If the replaced string had more characters than the new string, the cursor has moved forward, so it must be moved back
// Eg: ... replaced with … removes 2 characters
if ( reg.nbMoved < 0 ) {
selectionChange( currentBlockId, selectionStart.attributeKey, cursorPosition - 1, cursorPosition + reg.nbMoved )
// selectionChange( currentBlockId, selectionStart.attributeKey, cursorPosition - 1, cursorPosition + reg.nbMoved )
selectionChange( currentBlockId, selectionStart.attributeKey, cursorPosition + reg.nbMoved, cursorPosition + reg.nbMoved )
}

// If the replaced string had fewer characters than the new string, the cursor has moved backwards, so it must be moved forward
Expand All @@ -144,6 +151,10 @@ export const fixIt = props => {
selectionChange( currentBlockId, selectionStart.attributeKey, cursorPosition + 1 + reg.nbMoved, cursorPosition + reg.nbMoved )
}

if ( 0 === reg.nbMoved ) {
selectionChange( currentBlockId, selectionStart.attributeKey, cursorPosition, cursorPosition )
}

} )

global.consistencyLoop = 0
Expand All @@ -158,7 +169,7 @@ export const fixAll = props => {
const { settings } = props

// Get the regex of all rules
let theRegs = rules.filter( reg => true === settings?.find( s => s.slug === reg.name )?.value )
let theRegs = rules.filter( reg => true === settings?.find( s => s.slug === reg.slug )?.value )

// Get all blocks generated by pasting (which does not integrate innerBlocks)
const allBlocks = getBlocks()
Expand All @@ -180,17 +191,17 @@ export const fixAll = props => {
Object.entries( theRegs ).forEach( ( [ _, reg ] ) => {

// If the rule is not used by the locale, we do nothing
if ( ! isUsedByLocale( reg.name ) ) return
if ( ! isUsedByLocale( reg.slug ) ) return

// If the rule is a pair rule, we use a specific regex
if ( regsWithPair.includes( reg.name ) ) {
if ( regsWithPair.includes( reg.slug ) ) {
const singleCharacterOfPair = reg.mask.toString().match( /(?<=\/).+?(?=\/)/g )[0]
const realReg = new RegExp( `(?<!\=)${singleCharacterOfPair}(?!>)([^${singleCharacterOfPair}]*)(?<!\=)${singleCharacterOfPair}(?!>)`, 'g' )
newContent = newContent.replaceAll( realReg, reg.replace )
}

// If the rule is not a pair rule, we use the regex as it is
if ( ! regsWithPair.includes( reg.name ) ) {
if ( ! regsWithPair.includes( reg.slug ) ) {
const stringRegex = reg.mask.toString()
const regWithGlobalFlag = new RegExp( stringRegex.substring( 1, stringRegex.length - 1 ), 'g' )
newContent = newContent.replaceAll( regWithGlobalFlag, reg.replace )
Expand Down
8 changes: 8 additions & 0 deletions src/app/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
* @author Loïc Antignac.
*/

/**
* WordPress dependencies
*/
import { select, dispatch } from '@wordpress/data'

const { getBlock } = select( 'core/block-editor' )
const { updateBlock } = dispatch( 'core/block-editor' )

/**
* Get specific replacement string for pairing characters by checking if we are on opening one or closing one
*
Expand Down
54 changes: 54 additions & 0 deletions src/components/GlobalSettingPanel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Summary: GlobalSettingPanel component.
*
* @description This file contains the GlobalSettingPanel component used to display the plugin's global settings which contains the global correction rules in sidebar for administrators.
* @author Loïc Antignac.
*/

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n'
import { Panel, PanelHeader, PanelBody, PanelRow } from '@wordpress/components'

/**
* External dependencies
*/
import { GlobalSettingToggle } from './GlobalSettingToggle'
import { rules } from '../config/rules'
import { categories } from '../config/categories'

/**
* Define the GlobalSettingPanel component
*/
const GlobalSettingPanel = () => (
<Panel className='GlobalSettingPanel'>
<PanelHeader><strong>{ __( 'Global correction rules', 'consistency' ) }</strong></PanelHeader>
{
[...categories].map( ( cat, key ) => {
return ( <PanelBody
key={ key }
title={ __( cat.label, 'consistency' ) }
initialOpen={ false }
>
{
[...rules]
.filter(rule => rule.category === cat.slug)
.map( ( rule, key ) => (
<GlobalSettingToggle
key={ key }
settingSlug={ rule.slug }
settingName={ rule.name }
settingDescription={ {
__html: rule.description
} }
/>
) )
}
</PanelBody> )
} )
}
</Panel>
)

export default GlobalSettingPanel
Loading

0 comments on commit 822d89f

Please sign in to comment.