Repository navigation
Expand file tree
/
Copy pathcode.ts
More file actions
107 lines (91 loc) · 2.61 KB
/
Copy pathcode.ts
File metadata and controls
107 lines (91 loc) · 2.61 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
interface WritingGuidelineRule {
pattern: RegExp;
message: string;
}
const writingGuidelines: WritingGuidelineRule[] = [
{
pattern: /[ヲ-゚]/g,
message: "半角カナは使用せず、全角カタカナを使用してください"
},
{
pattern: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
message: "サロゲート文字(絵文字など)は使用しないでください"
}
];
function checkGuidelines(text: string): string[] {
const violations: string[] = [];
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: { type: string }) => {
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") as TextNode[];
if (textNodes.length === 0) {
figma.ui.postMessage({
type: 'count-result',
total: 0,
details: []
});
return;
}
let totalCount = 0;
const results: {
name: string,
count: number,
halfWidthSpaces: number,
fullWidthSpaces: number,
lineBreaks: number,
violations: string[]
}[] = [];
// テキストノードのみを効率的に処理
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
});
}
};