PluginProbe
Highlighting Code Block / 1.2.4
Highlighting Code Block v1.2.4
2.2.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.4.0 All 41 releases
highlighting-code-block / src / js / code-block / index.js

index.js in Highlighting Code Block 1.2.4, at src/js/code-block/index.js

217 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * WordPress dependencies
3 */
4
5 import { __ } from '@wordpress/i18n';
6 import { registerBlockType, createBlock } from '@wordpress/blocks';
7 import {
8 RichText,
9 InspectorControls,
10 // InnerBlocks,
11 // BlockControls,
12 } from '@wordpress/block-editor';
13
14 import { SelectControl } from '@wordpress/components';
15
16 /**
17 * External dependencies
18 */
19 import classnames from 'classnames';
20 import hcbIcon from './_icon';
21 import { setHeightCodeBlocks, sanitizeCodeblock } from './_utils';
22
23 /**
24 * InspectorControls
25 */
26 import HcbSidePanels from './_panels';
27
28 /**
29 * metadata
30 */
31 import metadata from './block.json';
32
33 /**
34 * テキストドメイン
35 */
36 const textDomain = 'loos-hcb';
37
38 /**
39 * 言語�
40 報をグローバル変数から受け取る。
41 */
42 let hcbLangs = window.hcbLangs;
43 if ('object' !== typeof hcbLangs) {
44 hcbLangs = {html:"HTML",css:"CSS",scss:"SCSS",js:"JavaScript",ts:"TypeScript",php:"PHP",ruby:"Ruby",python:"Python",swift:"Swift",c:"C",csharp:"C#",cpp:"C++",objectivec:"Objective-C",sql:"SQL",json:"JSON",bash:"Bash",git:"Git"};
45 }
46
47 /**
48 * Register Highlighting Code Block
49 */
50 registerBlockType(metadata.name, {
51 icon: hcbIcon,
52 title: metadata.title,
53 category: metadata.category,
54 keywords: metadata.keywords,
55 supports: metadata.supports,
56 attributes: metadata.attributes,
57 transforms: {
58 from: [
59 //どのブロックタイプから変更できるようにするか
60 {
61 type: 'block',
62 blocks: ['core/code'], //整形済みブロック : 'core/preformatted',
63 transform: (attributes) => {
64 return createBlock('loos-hcb/code-block', {
65 code: attributes.content,
66 });
67 },
68 },
69 ],
70 to: [
71 //どのブロックタイプへ変更できるようにするか
72 {
73 type: 'block',
74 blocks: ['core/code'],
75 transform: (attributes) => {
76 return createBlock('core/code', {
77 content: attributes.code,
78 });
79 },
80 },
81 ],
82 },
83
84 edit: (props) => {
85 const { attributes, setAttributes, clientId, className } = props;
86 const {
87 code,
88 // langType,
89 fileName,
90 langName,
91 dataLineNum,
92 isShowLang,
93 } = attributes;
94 const blockClass = classnames('hcb_wrap', 'hcb-block', className);
95
96 // コードの textarea 高さセット
97 setTimeout(() => {
98 // ちょっと�
99 らせないと null になる
100 const hcbBlock = document.getElementById('block-' + clientId);
101 const hcbTextarea = hcbBlock.querySelector('.hcb_textarea');
102 setHeightCodeBlocks(hcbTextarea);
103 }, 10);
104
105 const langList = [
106 {
107 value: '',
108 label: '- Lang Select -',
109 },
110 ];
111
112 Object.keys(hcbLangs).forEach((key) => {
113 langList.push({
114 value: key,
115 label: hcbLangs[key],
116 });
117 });
118
119 //preタグにつけるクラス名を生成して保存
120 // let preClass = 'prism ' + isLineShow + '-numbers lang-' + langType;
121 // setAttributes({ preClass: preClass });
122
123 return (
124 <>
125 <InspectorControls>
126 <HcbSidePanels {...props} />
127 </InspectorControls>
128 <div
129 className={blockClass}
130 data-file={fileName || null}
131 data-lang={langName || null}
132 // data-line={dataLineNum || null}
133 data-show-lang={isShowLang || null}
134 >
135 <textarea
136 className='hcb_textarea'
137 placeholder='Your Code...'
138 value={code}
139 onChange={(e) => {
140 setAttributes({ code: e.target.value });
141 setHeightCodeBlocks(e.target);
142 }}
143 ></textarea>
144 <div className='select_wrap'>
145 <SelectControl
146 value={attributes.langType}
147 options={langList}
148 onChange={(langKey) => {
149 if ('' === langKey) {
150 setAttributes({
151 langType: '',
152 langName: '',
153 });
154 } else {
155 setAttributes({
156 langType: langKey,
157 langName: hcbLangs[langKey],
158 });
159 }
160 }}
161 />
162 <input
163 type='text'
164 className='num_input'
165 value={dataLineNum}
166 placeholder={__('"data-line" value', textDomain)} //data-line属性値
167 onChange={(e) => {
168 setAttributes({ dataLineNum: e.target.value });
169 }}
170 />
171 <input
172 type='text'
173 className='filename_input'
174 value={fileName}
175 placeholder={__('file name', textDomain)} //ファイル名
176 onChange={(e) => {
177 setAttributes({ fileName: e.target.value });
178 }}
179 />
180 </div>
181 </div>
182 </>
183 );
184 },
185 save: ({ attributes }) => {
186 const {
187 code,
188 langType,
189 fileName,
190 langName,
191 dataLineNum,
192 isLineShow,
193 isShowLang,
194 } = attributes;
195
196 // preタグにつけるクラス
197 const preClass = 'prism ' + isLineShow + '-numbers lang-' + langType;
198
199 return (
200 <div className='hcb_wrap'>
201 <pre
202 className={preClass}
203 data-file={fileName || null}
204 data-lang={langName || null}
205 data-line={dataLineNum || null}
206 data-show-lang={isShowLang || null}
207 >
208 <RichText.Content
209 tagName='code'
210 value={sanitizeCodeblock(code)}
211 />
212 </pre>
213 </div>
214 );
215 },
216 });
217