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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import React from "react";
import { addons } from "@storybook/addons";
// eslint-disable-next-line no-unused-vars
import { AddonPanel } from "@storybook/components";
import { FLUENT_CHANGED, FLUENT_SET_STRINGS } from "./constants.mjs";
export class FluentPanel extends React.Component {
constructor(props) {
super(props);
this.channel = addons.getChannel();
this.state = {
name: null,
strings: [],
};
}
componentDidMount() {
const { api } = this.props;
api.on(FLUENT_CHANGED, this.handleFluentChanged);
}
componentWillUnmount() {
const { api } = this.props;
api.off(FLUENT_CHANGED, this.handleFluentChanged);
}
handleFluentChanged = strings => {
let storyData = this.props.api.getCurrentStoryData();
let fileName = `${storyData.component}.ftl`;
this.setState(state => ({ ...state, strings, fileName }));
};
onInput = e => {
this.setState(state => {
let strings = [];
for (let [key, value] of state.strings) {
if (key == e.target.name) {
strings.push([key, e.target.value]);
} else {
strings.push([key, value]);
}
}
let stringified = strings
.map(([key, value]) => `${key} = ${value}`)
.join("\n");
this.channel.emit(FLUENT_SET_STRINGS, stringified);
const { fluentStrings } = this.props.api.getGlobals();
this.props.api.updateGlobals({
fluentStrings: { ...fluentStrings, [state.fileName]: strings },
});
return { ...state, strings };
});
};
render() {
const { api, active } = this.props;
const { strings } = this.state;
return (
<AddonPanel active={!!active} api={api}>
{strings.map(([identifier, value]) => (
<div key={identifier}>
<label>
{identifier} =
<textarea
name={identifier}
onInput={this.onInput}
defaultValue={value}
></textarea>
</label>
</div>
))}
</AddonPanel>
);
}
}
|