Write custom transform file for design tokens from Figma in Style Dictionary for Flutter - flutter

I have barebone Flutter 2.5 project and Figma design tokens which were exported via Figma Tokens.
Those design tokens look like this:
project\style_dictionary\properties\tokens.json
{
"borderWidth": {
"100": {
"value": "1.5",
"type": "borderWidth"
}
},
"opacity": {
"basic": {
"100": {
"value": "0.04",
"type": "opacity"
}
}
},
"colors": {
"basic": {
"red": {
"50": {
"value": "#ffebee",
"type": "color"
}
}
}
}
}
and Style Dictionary config which looks like this
project\style_dictionary\config.json
{
"source": [
"properties/*.json"
],
"platforms": {
"flutter": {
"transformGroup": "flutter",
"buildPath": "../lib/unique_file/",
"files": [
{
"destination": "style_dictionary.dart",
"format": "flutter/class.dart",
"className": "StyleDictionary"
}
]
},
"flutter-separate": {
"transformGroup": "flutter-separate",
"buildPath": "../lib/unique_file/",
"files": [
{
"destination": "style_dictionary_color.dart",
"format": "flutter/class.dart",
"className": "StyleDictionaryColor",
"type": "color",
"filter": {
"attributes": {
"category": "colors"
}
}
}
]
}
}
}
Running style-dictionary build in CMD in style_dictionary will generate next file
project\lib\unique_file\style_dictionary_color.dart
import 'dart:ui';
class StyleDictionaryColor {
StyleDictionaryColor._();
static const basicRed50 = #ffebee;
}
But in should be like that at the end
project\lib\unique_file\style_dictionary_color.dart
import 'dart:ui';
class StyleDictionaryColor {
StyleDictionaryColor._();
static const basicRed50 = Color(0xFFFFEBEE);
}
Question:
How and where can I create Style Dictionary transforms file to create right dart file with Color type of the static variable?
I cannot modify exported design tokens.

Create a project\build.js as a custom Transforms file. Logic of creation was used from default Flutter color transforms and Documentation
const StyleDictionary = require('style-dictionary')
const baseConfig = require('./style_dictionary/config.json')
const Color = require('tinycolor2')
StyleDictionary.registerTransform({
name: 'colors/hex8flutter',
type: 'value',
matcher: prop => {
return prop.attributes.category === 'colors'
},
transformer: prop => {
var str = Color(prop.value).toHex8().toUpperCase();
return `Color(0x${str.slice(6)}${str.slice(0, 6)})`;
},
})
const StyleDictionaryExtended = StyleDictionary.extend(baseConfig)
StyleDictionaryExtended.buildAllPlatforms()
Modify project\style_dictionary\config.json so it can be executed from project directory and include new transforms - "transformColorsToColor" together with other transforms from Flutter
{
"source": [
"style_dictionary/properties/*.json"
],
"platforms": {
"flutter": {
"transforms": ["attribute/cti", "name/cti/camel", "color/hex8flutter", "size/flutter/remToDouble", "content/flutter/literal", "asset/flutter/literal", "font/flutter/literal", "colors/hex8flutter"],
"buildPath": "lib/unique_file/",
"files": [
{
"destination": "style_dictionary.dart",
"format": "flutter/class.dart",
"className": "StyleDictionary"
}
]
},
"flutter-separate": {
"transforms": ["attribute/cti", "name/ti/camel", "color/hex8flutter", "size/flutter/remToDouble", "content/flutter/literal", "asset/flutter/literal", "font/flutter/literal", "colors/hex8flutter"],
"buildPath": "lib/unique_file/",
"files": [
{
"destination": "style_dictionary_color.dart",
"format": "flutter/class.dart",
"className": "StyleDictionaryColor",
"type": "color",
"filter": {
"attributes": {
"category": "colors"
}
}
}
]
}
}
}
Run npm init with all default answers
Run npm install --save tinycolor2
Run node build.js

Related

Implement I18n localization in Strapi local plugins

I generated a local plugin and created an article model using:
"pluginOptions": {
"i18n": {
"localized": true
}
},
inside his article.settings.json file, in order to make some specific fields translatables using the Internationalization(I18N) plugin
Problem is, while running the command:
strapi develop --watch-admin
I end up having the following errors:
error Something went wrong in the model "Article" with the attribute "localizations"
error TypeError: Cannot read property "uid" of undefined
Removing the "pluginOptions" instead, gives my local plugin running without any translatable field or articles__translations pivot that should be generated into my mysql database
"pluginOptions" is the very same parameter that gets generated into the model settings creating a collection type using the Content-Types Builder, but I can't have it to work while using it for a local plugin.
Here is my article.settings.json:
plugins/blog/models/article.settings.json
{
"kind": "collectionType",
"collectionName": "articles",
"info": {
"name": "article"
},
"options": {
"draftAndPublish": false,
"timestamps": true,
"populateCreatorFields": true,
"increments": true,
"comment": ""
},
"pluginOptions": {
"i18n": {
"localized": true
}
},
"attributes": {
"title": {
"pluginOptions": {
"i18n": {
"localized": true
}
},
"type": "string",
"required": true,
"maxLength": 255,
"minLength": 3
},
"slug": {
"pluginOptions": {
"i18n": {
"localized": true
}
},
"type": "uid",
"targetField": "title",
"required": true
},
"featured": {
"pluginOptions": {
"i18n": {
"localized": false
}
},
"type": "boolean",
"default": false
},
"published_date": {
"pluginOptions": {
"i18n": {
"localized": false
}
},
"type": "datetime"
},
}
}
You can use the content-type-builder plugin as a workaround. You would not create the content type under the content-types folder but create it programmatically.
As an example of a very simple tag content type:
{
"singularName": "tag",
"pluralName": "tags",
"displayName": "tag",
"description": "",
"draftAndPublish": false,
"pluginOptions": {
"i18n": {
"localized": true
}
},
"attributes": {
"label": {
"type": "string",
"pluginOptions": {
"i18n": {
"localized": true
}
},
"unique": true
}
}
}
Note, this schema of the json is a bit different from the ones in plugin/server/content-types.
Then you can create the content type programmatically like this:
import { Strapi } from "#strapi/strapi";
import tag from "../content-types/tag.json";
import page from "../content-types/page.json";
export default ({ strapi }: { strapi: Strapi }) => ({
async createContentComponent() {
if (!tag) return null;
try {
const components: any = [];
const contentType = await strapi
.plugin("content-type-builder")
.services["content-types"].createContentType({
contentType: tag,
components,
});
return contentType;
} catch (e) {
console.log("error", e);
return null;
}
},
});
This is exactly how the admin creates content types using the content builder UI.
And it works using the pluginOptions.i18n.localized: true.
One approach would be to do this, e.g., on the bootstrap phase of the plugin. Here you could also check whether or not the contents are created or not.
As a bonus, you can also create components that otherwise would not work.
Hope that helps.
Links:
Create components programmatically in a plugin: https://github.com/strapi/strapi-plugin-seo/blob/main/server/services/seo.js
Create content types:
https://github.com/strapi/strapi/blob/88caa92f878a068926255dd482180202f53fcdcc/packages/core/content-type-builder/server/controllers/content-types.js#L48
EDIT:
You could also keep the original schema and use this fn to transform it - at least for now as long as the other approach is not working:
https://github.com/strapi/strapi/blob/1eab2fb08c7a4d3d40a5a7ff3b2f137ce0afcf8a/packages/core/content-type-builder/server/services/content-types.js#L37

Configuring wch3 hash function in mcrouter

I came across this page discussing different hash functions that mcrouter can use, but could not find an example of how a hash function can be configured if you do not want to use the default ch3. In my case, i would like to use the "wch3" with a balanced (50/50) weight between two nodes in a pool. How can i manually change the default to configure wch3?
Thanks in advance.
Here is an example that can help you:
{
"pools": {
"cold": {
"servers": [
"memc_1:11211",
"memc_2:11211"
]
},
"warm": {
"servers": [
"memc_11:11211",
"memc_12:11211"
]
}
},
"route": {
"type": "OperationSelectorRoute",
"operation_policies": {
"get": {
"type": "WarmUpRoute",
"cold": "PoolRoute|cold",
"warm": "PoolRoute|warm",
"exptime": 20
}
},
"default_policy": {
"type": "AllSyncRoute",
"children": [{
"type": "PoolRoute",
"pool": "cold",
"hash": {
"hash_func": "WeightedCh3",
"weights": [
1,
1
]
}
},
{
"type": "PoolRoute",
"pool": "warm",
"hash": {
"hash_func": "WeightedCh3",
"weights": [
1,
1
]
}
}
]
}
}
}
You can adjust the weight in the range [0.0, 1.0].

elastic4s: how to add analyzer/filter for german_phonebook to analysis?

How do I add the following german_phonebook analyzer to elastic search using elastic4s?
"index": {
"analysis": {
"analyzer": {
"german": {
"filter": [
"lowercase",
"german_stop",
"german_normalization",
"german_stemmer"
],
"tokenizer": "standard"
},
"german_phonebook": {
"filter": [
"german_phonebook"
],
"tokenizer": "keyword"
},
"mySynonyms": {
"filter": [
"lowercase",
"mySynonymFilter"
],
"tokenizer": "standard"
}
},
"filter": {
"german_phonebook": {
"country": "CH",
"language": "de",
"type": "icu_collation",
"variant": "#collation=phonebook"
},
"german_stemmer": {
"language": "light_german",
"type": "stemmer"
},
"german_stop": {
"stopwords": "_german",
"type": "stop"
},
"mySynonymFilter": {
"synonyms": [
"swisslift,lift"
],
"type": "synonym"
}
}
},
The core question here is which filter to use for the german_phonebook filter of type icu_collation?
...
Following the answer I came up with this code:
case class GPhonebook() extends TokenFilterDefinition {
val filterType = "phonebook"
def name = "german_phonebook"
override def build(source: XContentBuilder): Unit = {
source.field("tokenizer", "keyword")
source.field("country", "CH")
source.field("language", "de")
source.field("type", "icu_collation")
source.field("variant", "#collation=phonebook")
}
}
The analyzer definition looks like this now:
CustomAnalyzerDefinition(
"german_phonebook",
KeywordTokenizer("myKeywordTokenizer2"),
GPhonebook()
)
What you really want is someway to say
CustomTokenFilter("german_phonebook) or BuiltInTokenFilter("german_phonebook") but you can't (I'll add that).
So for now, you need to extend TokenFilterDefinition.
Eg, Something like
case class GPhonebook extends TokenFilterDefinition {
val filterType = "phonebook"
override def build(source: XContentBuilder): Unit = {
// set extra params in here
}
}

How to pick up data from json with multiple level with scala

I want to pick up data from the json, but i can not code it in scala, i can only write it in php, because it is very simple to use, but i have no idea how can i do the same thing in scala. please help me out.
{
"home": {
"type": "literal",
"options": {
"route": "\/",
"defaults": {
"controller": "Apiv1\\Controller\\Index",
"action": "index"
}
}
},
"praise": {
"type": "literal",
"options": {
"route": "\/apiv1\/praise",
"defaults": {
"controller": "Apiv1\\Controller\\Praise",
"action": "index"
}
},
"may_terminate": true,
"child_routes": {
"status": {
"type": "literal",
"options": {
"route": "\/status",
"defaults": {
"action": "status"
}
}
}
}
},
"admin": {
"type": "literal",
"options": {
"route": "\/admin",
"defaults": {
"controller": "Admin\\Controller\\Index",
"action": "index"
}
},
"may_terminate": true,
"child_routes": {
"routes": {
"type": "literal",
"options": {
"route": "\/routes",
"defaults": {
"controller": "Admin\\Controller\\Routes",
"action": "index"
}
},
"may_terminate": true,
"child_routes": {
"list": {
"type": "literal",
"options": {
"route": "\/list",
"defaults": {
"action": "list"
}
}
}
}
}
}
}
}
I want to pick up the route field from the json, and i would like to list all routes
here is my php version function
function parseRoute($a, $pre = '', &$urls)
{
foreach ($a as $k => $v) {
$route = $pre . $v['options']['route'];
$urls[] = $route;
if (isset($v['child_routes']) && is_array($v['child_routes'])) {
$this->parseRoute($v['child_routes'], $route, $urls);
}
$route = null;
}
}
$urls = array();
var_dump(parseRoute(json_decode($data), '', $urls));
The best thing to do is to use one of the existing JSON libraries for Scala. Here is related question describing your options:
What JSON library to use in Scala?
Next is an example of using Lift-JSON. See the link, there are many examples. Here is what you can do:
import net.liftweb.json._
val rawJson = """{
"home":{
"type":"literal",
"options":{
"route":"/",
"defaults":{
"controller":"Apiv1\\Controller\\Index",
"action":"index"
}
}
},
"praise":{
"type":"literal",
"options":{
"route":"/apiv1/praise",
"defaults":{
"controller":"Apiv1\\Controller\\Praise",
"action":"index"
}
},
"may_terminate":true,
"child_routes":{
"status":{
"type":"literal",
"options":{
"route":"/status",
"defaults":{
"action":"status"
}
}
}
}
},
"admin":{
"type":"literal",
"options":{
"route":"/admin",
"defaults":{
"controller":"Admin\\Controller\\Index",
"action":"index"
}
},
"may_terminate":true,
"child_routes":{
"routes":{
"type":"literal",
"options":{
"route":"/routes",
"defaults":{
"controller":"Admin\\Controller\\Routes",
"action":"index"
}
},
"may_terminate":true,
"child_routes":{
"list":{
"type":"literal",
"options":{
"route":"/list",
"defaults":{
"action":"list"
}
}
}
}
}
}
}
}"""
val json = parse(rawJson)
val routeList = json \\ "route"
// which returns a structure like this:
JObject(List(JField(route,JString(/)), JField(route,JString(/apiv1/praise)), JField(route,JString(/status)), JField(route,JString(/admin)), JField(route,JString(/routes)), JField(route,JString(/list))))
// which is a JSON object
// if you want a list of routes as strings as in your example extract them this way now:
val routeList: List[String] = routeObject \\ classOf[JString]
// which returns:
List[String] = List(/, /apiv1/praise, /status, /admin, /routes, /list)
Note that I'm assuming that routes can be at any level of nesting in JSON doc (you might want to enforce certain level or parent of the 'route'). I also assume that all routes are Strings and ignore the ones which are not - if you want more options you can pattern match on the type of route using derivative classes of JValue: JArray, JString, JObject, etc.

Disable Copy in CCP context menu for JsTree

How can I disable the "Copy" (but not cut / paste) functionality of the jsTree right click context menu?
This pretty much did the trick.
$("#housingTree").jstree({
"plugins": ["themes", "html_data", "ui", "crrm", "hotkeys", "contextmenu"],
"core": { "initially_open": ["phtml_1"] },
"contextmenu": {
"items": function ($node) {
return {
"Rename": {
"label": "Rename",
"action": function (obj) { this.rename(obj); }
},
"Create": {
"label": "Create",
"action": function (obj) { this.create(obj); }
},
"Delete": {
"label": "Delete",
"action": function (obj) { this.remove(obj); }
},
"Cut": {
"label": "Cut",
"action": function (obj) { this.cut(obj); }
},
"Paste": {
"label": "Paste",
"action": function (obj) { this.paste(obj); }
}
};
}
}
})
i dont know if the action functions are the default functions or custom functions, but that didnt work for me... anyway your post did set me in the right path! thanks!
this is how i did it, after finding another post:
"contextmenu": {
"items": function ($node) {
var tree = $("#html1Tree").jstree(true);
return {
"Rename": {
"label": "Rename",
"action": function (obj) {
tree.edit($node);
}
},
"Create": {
"label": "Create",
"action": function (obj) {
$node = tree.create_node($node);
tree.edit($node);
}
}
};
}
}
jsTree and Context Menu: modify items
The shorter approach could be
"contextmenu": {
"items": function(node) {
var defaultItems = $.jstree.defaults.contextmenu.items();
console.log("default items : "+JSON.stringify(defaultItems));
delete defaultItems.ccp.submenu.copy;
return defaultItems;
}
},
You can console.log(defaultItems). It wil print json representation of object. You can modify other properties also.
this is my easiest option.
All main code placed in "contextmenu.items" block.
$('#c-list').jstree({
"core": {
"themes": {"responsive": false},
"check_callback": true,
},
"types": {
"default": {
"icon": "fa fa-folder text-warning fa-lg"
},
"file": {
"icon": "fa fa-file text-warning fa-lg"
}
},
"contextmenu":{
'items' : function(node) {
var items = $.jstree.defaults.contextmenu.items();
items.ccp = false;
return items;
}
},
"plugins": ["contextmenu", "dnd", "types", "search", "wholerow","checkbox"]
});