Skip to content
This repository was archived by the owner on Nov 2, 2024. It is now read-only.

Commit 62807ad

Browse files
committed
Another attempt at deprecation...
1 parent 08cf9bf commit 62807ad

File tree

6 files changed

+84
-67
lines changed

6 files changed

+84
-67
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
"scripts": {
2020
"start": "wp-scripts start",
2121
"build": "wp-scripts build"
22+
},
23+
"dependencies": {
24+
"lodash.omit": "^4.5.0"
2225
}
2326
}

src/block-attributes.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const blockAttributes = {
2+
content: {
3+
type: 'string',
4+
source: 'html',
5+
selector: 'code',
6+
},
7+
datalang: {
8+
type: 'string',
9+
selector: 'code',
10+
source: 'attribute',
11+
attribute: 'data-lang',
12+
},
13+
lineNumbers: {
14+
type: 'boolean',
15+
},
16+
title: {
17+
type: 'string',
18+
source: 'attribute',
19+
selector: 'pre',
20+
attribute: 'title',
21+
},
22+
};
23+
24+
export default blockAttributes;

src/deprecated.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import omit from 'lodash.omit';
2+
3+
const deprecated = {
4+
// old attributes
5+
attributes: {
6+
content: {
7+
type: 'string',
8+
source: 'html',
9+
selector: 'code',
10+
},
11+
language: {
12+
type: 'string',
13+
selector: 'code',
14+
source: 'attribute',
15+
attribute: 'lang',
16+
},
17+
},
18+
migrate: ( attributes ) => {
19+
return {
20+
datalang: attributes.language,
21+
content: attributes.content,
22+
};
23+
},
24+
// old save function
25+
save: ( { attributes } ) => {
26+
let cls = '';
27+
cls = attributes.language ? 'language-' + attributes.language : '';
28+
cls = attributes.lineNumbers ? cls + ' line-numbers' : cls;
29+
30+
return (
31+
<pre className="wp-block-code">
32+
<code lang={ attributes.language } className={ cls }>
33+
{ attributes.content }
34+
</code>
35+
</pre>
36+
);
37+
},
38+
};
39+
40+
export default deprecated;

src/edit.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import { __ } from '@wordpress/i18n';
2121

2222
/* global mkaz_code_syntax_languages, mkaz_code_syntax_default_lang, Prism */
2323

24-
const edit = ( { attributes, className, setAttributes } ) => {
24+
const Edit = ( { attributes, className, setAttributes } ) => {
2525
useEffect( () => {
26-
if ( ! attributes.language && mkaz_code_syntax_default_lang ) {
27-
setAttributes( { language: mkaz_code_syntax_default_lang } );
26+
if ( ! attributes.datalang && mkaz_code_syntax_default_lang ) {
27+
setAttributes( { datalang: mkaz_code_syntax_default_lang } );
2828
}
29-
}, [ attributes.language ] );
29+
}, [ attributes.datalang ] );
3030

3131
// Onload fetch color scheme option
3232
useEffect( () => {
@@ -61,7 +61,7 @@ const edit = ( { attributes, className, setAttributes } ) => {
6161
];
6262

6363
const updateColorScheme = ( colorScheme ) => {
64-
let path = '/mkaz/code-syntax/v1/set/color-scheme/' + colorScheme;
64+
const path = '/mkaz/code-syntax/v1/set/color-scheme/' + colorScheme;
6565
apiFetch( { path } )
6666
.then( () => {
6767
setAttributes( { colorScheme } );
@@ -71,10 +71,6 @@ const edit = ( { attributes, className, setAttributes } ) => {
7171
} );
7272
};
7373

74-
let cls = '';
75-
cls = attributes.language ? 'language-' + attributes.language : '';
76-
cls = attributes.lineNumbers ? cls + ' line-numbers' : cls;
77-
7874
// shared props for text areas
7975
const textAreaProps = {
8076
value: attributes.content || '',
@@ -96,7 +92,7 @@ const edit = ( { attributes, className, setAttributes } ) => {
9692
<PanelBody title={ __( 'Settings' ) }>
9793
<SelectControl
9894
label={ __( 'Language' ) }
99-
value={ attributes.language }
95+
value={ attributes.datalang }
10096
options={ [
10197
{
10298
label: __(
@@ -113,8 +109,8 @@ const edit = ( { attributes, className, setAttributes } ) => {
113109
} )
114110
)
115111
) }
116-
onChange={ ( language ) =>
117-
setAttributes( { language } )
112+
onChange={ ( datalang ) =>
113+
setAttributes( { datalang } )
118114
}
119115
/>
120116
<ToggleControl
@@ -204,11 +200,11 @@ const edit = ( { attributes, className, setAttributes } ) => {
204200

205201
{ /* Language label, uses wp-block class to keep within editor bounds */ }
206202
<div className="wp-block mkaz-code-syntax-block__lang_label">
207-
{ mkaz_code_syntax_languages[ attributes.language ] }
203+
{ mkaz_code_syntax_languages[ attributes.datalang ] }
208204
</div>
209205
</>
210206
</>
211207
);
212208
};
213209

214-
export default edit;
210+
export default Edit;

src/index.js

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { addFilter } from '@wordpress/hooks';
1111
/**
1212
* Internal dependencies
1313
*/
14+
import blockAttributes from './block-attributes';
15+
import deprecated from './deprecated';
1416
import edit from './edit';
1517
import save from './save';
1618

@@ -19,61 +21,13 @@ const addSyntaxToCodeBlock = ( settings ) => {
1921
return settings;
2022
}
2123

22-
const blockAttributes = {
23-
...settings.attributes,
24-
language: {
25-
type: 'string',
26-
selector: 'code',
27-
source: 'attribute',
28-
attribute: 'data-lang',
29-
},
30-
lineNumbers: {
31-
type: 'boolean',
32-
},
33-
title: {
34-
type: 'string',
35-
source: 'attribute',
36-
selector: 'pre',
37-
attribute: 'title',
38-
},
39-
};
40-
4124
return {
4225
...settings,
43-
44-
attributes: blockAttributes,
45-
46-
deprecated: [{
47-
// old attributes
48-
attributes: {
49-
...blockAttributes,
50-
language: {
51-
type: 'string',
52-
selector: 'code',
53-
source: 'attribute',
54-
attribute: 'lang',
55-
},
56-
},
57-
migrate: ( attributes ) => { return attributes; },
58-
// old save function
59-
save: ( { attributes } ) => {
60-
let cls = '';
61-
cls = ( attributes.language ) ? 'language-' + attributes.language : '';
62-
cls = ( attributes.lineNumbers ) ? cls + ' line-numbers' : cls;
63-
return (
64-
<pre title={ attributes.title }>
65-
<code lang={ attributes.language } className={ cls }>
66-
{ attributes.content }
67-
</code>
68-
</pre>
69-
);
70-
},
71-
}],
72-
26+
attributes: { ...blockAttributes },
27+
deprecated: [ deprecated ],
7328
edit,
7429
save,
7530
};
76-
7731
};
7832

7933
// Register Filter

src/save.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { RichText, useBlockProps } from '@wordpress/block-editor';
55

66
const save = ( { attributes } ) => {
77
let cls = '';
8-
cls = attributes.language ? 'language-' + attributes.language : '';
8+
cls = attributes.datalang ? 'language-' + attributes.datalang : '';
99
cls = attributes.lineNumbers ? cls + ' line-numbers' : cls;
1010

1111
// WP 5.6 / GB 9.2
@@ -17,7 +17,7 @@ const save = ( { attributes } ) => {
1717
<RichText.Content
1818
tagName="code"
1919
value={ attributes.content }
20-
lang={ attributes.language }
20+
data-lang={ attributes.datalang }
2121
className={ cls }
2222
/>
2323
</pre>
@@ -28,7 +28,7 @@ const save = ( { attributes } ) => {
2828
// Backward compatibility < WP 5.6
2929
return (
3030
<pre className="wp-block-code" title={ attributes.title }>
31-
<code data-lang={ attributes.language } className={ cls }>
31+
<code data-lang={ attributes.datalang } className={ cls }>
3232
{ attributes.content }
3333
</code>
3434
</pre>

0 commit comments

Comments
 (0)