Repository navigation
Expand file tree
/
Copy pathcode.js
More file actions
84 lines (84 loc) · 2.67 KB
/
Copy pathcode.js
File metadata and controls
84 lines (84 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"use strict";
const writingGuidelines = [
{
pattern: /[ヲ-゚]/g,
message: "半角カナは使用せず、全角カタカナを使用してください"
},
{
pattern: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
message: "サロゲート文字(絵文字など)は使用しないでください"
}
];
function checkGuidelines(text) {
const violations = [];
for (const rule of writingGuidelines) {
if (rule.pattern.test(text)) {
violations.push(rule.message);
}
}
return violations;
}
figma.showUI(__html__, {
width: 300,
height: 400
});
figma.ui.onmessage = (msg) => {
if (msg.type === 'count-text') {
const selection = figma.currentPage.selection;
if (selection.length === 0) {
figma.ui.postMessage({
type: 'count-result',
total: 0,
details: []
});
return;
}
// テキストノードのみを事前にフィルタリング
const textNodes = selection.filter(node => node.type === "TEXT");
if (textNodes.length === 0) {
figma.ui.postMessage({
type: 'count-result',
total: 0,
details: []
});
return;
}
let totalCount = 0;
const results = [];
// テキストノードのみを効率的に処理
for (const textNode of textNodes) {
const characters = textNode.characters;
const count = characters.length;
// 一度の文字列走査で全ての情報を収集
let halfWidthSpaces = 0;
let fullWidthSpaces = 0;
let lineBreaks = 0;
for (let i = 0; i < count; i++) {
const char = characters[i];
if (char === ' ')
halfWidthSpaces++;
else if (char === ' ')
fullWidthSpaces++;
else if (char === '\n')
lineBreaks++;
}
// ガイドラインチェックを実行
const violations = checkGuidelines(characters);
totalCount += count;
results.push({
name: textNode.name || "無名のテキスト",
count: count,
halfWidthSpaces: halfWidthSpaces,
fullWidthSpaces: fullWidthSpaces,
lineBreaks: lineBreaks,
violations: violations
});
}
// UIに結果を送信
figma.ui.postMessage({
type: 'count-result',
total: totalCount,
details: results
});
}
};