mirror of
				https://github.com/skishore/makemeahanzi.git
				synced 2025-10-31 19:07:07 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			52 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| "use strict";
 | |
| 
 | |
| const getStatusLine = (actual, expected) => {
 | |
|   const actual_text = `Selected ${actual} stroke${actual === 1 ? '' : 's'}`;
 | |
|   if (!expected) {
 | |
|     return {cls: 'error', message: `${actual_text}. True number unknown.`};
 | |
|   } else if (actual !== expected) {
 | |
|     return {cls: 'error', message: `${actual_text}, but need ${expected}.`};
 | |
|   }
 | |
|   return {cls: 'success', message: `${actual_text}.`};
 | |
| }
 | |
| 
 | |
| const getStrokePaths = (strokes, include, colors) => {
 | |
|   const result = [];
 | |
|   for (let i = 0; i < strokes.length; i++) {
 | |
|     const stroke = strokes[i];
 | |
|     const color = include[stroke] ? colors[i % colors.length] : 'gray';
 | |
|     result.push({cls: 'selectable', d: stroke, fill: color, stroke: 'black'});
 | |
|   }
 | |
|   return result;
 | |
| }
 | |
| 
 | |
| stages.strokes = class StrokesStage extends stages.AbstractStage {
 | |
|   constructor(glyph) {
 | |
|     super('strokes');
 | |
|     const include = this.include = {};
 | |
|     this.strokes = stroke_extractor.getStrokes(
 | |
|         glyph.stages.path, glyph.stages.bridges).strokes;
 | |
|     this.strokes.map((stroke) => include[stroke] = true);
 | |
|     if (glyph.stages.strokes && glyph.stages.strokes.length > 0 &&
 | |
|         glyph.stages.strokes.filter((x) => !include[x]).length === 0) {
 | |
|       this.strokes.map((stroke) => include[stroke] = false);
 | |
|       glyph.stages.strokes.map((stroke) => include[stroke] = true);
 | |
|     }
 | |
|     glyph.stages.strokes = this.strokes.filter((x) => this.include[x]);
 | |
|   }
 | |
|   handleEvent(glyph, event, template) {
 | |
|     assert(this.include.hasOwnProperty(template.d));
 | |
|     this.include[template.d] = !this.include[template.d];
 | |
|     glyph.stages.strokes = this.strokes.filter((x) => this.include[x]);
 | |
|     Session.set('editor.glyph', glyph);
 | |
|   }
 | |
|   refresh(glyph) {
 | |
|     Session.set('stage.paths',
 | |
|                 getStrokePaths(this.strokes, this.include, this.colors));
 | |
|     const data = cjklib.getCharacterData(glyph.character);
 | |
|     const actual = glyph.stages.strokes.length;
 | |
|     const expected = glyph.metadata.strokes || data.strokes;
 | |
|     Session.set('stage.status', [getStatusLine(actual, expected)]);
 | |
|   }
 | |
| }
 | 
