Skip to content

Commit

Permalink
fixed prettierignore and reformatted everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Gyger committed Mar 25, 2020
1 parent b146c9f commit a1643c7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 76 deletions.
10 changes: 5 additions & 5 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.idea
/.idea
*.iml
img
internals
node_modules
package-lock.json
/img
/internals
/node_modules
/package-lock.json

/index.d.ts
/index.d.ts.map
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"prepublishOnly": "npm run build",
"preversion": "npm run test",
"version": "auto-changelog --breaking-pattern \"BREAKING CHANGE:\" -p && git add CHANGELOG.md",
"postversion": "git push origin master --tags && npm publish"
"postversion": "git push origin master --tags && npm publish",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\""
},
"author": "Florian Gyger <info@floriangyger.ch>",
"license": "MIT",
Expand Down
56 changes: 38 additions & 18 deletions src/internals/components/WordMarker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ interface WordMarkerProps {

type MapFunction = (child: React.ReactNode) => React.ReactNode;

const deepMap = (children: React.ReactNode, map: MapFunction): React.ReactNode => {
const deepMap = (
children: React.ReactNode,
map: MapFunction
): React.ReactNode => {
return React.Children.map(children, child => {
if (React.isValidElement(child) && child.props.children) {
return React.cloneElement(child, {
children: deepMap(child.props.children, map),
children: deepMap(child.props.children, map)
});
}
return map(child);
});
};

export const markChildText = (children: React.ReactNode, markedWordIndex: number): React.ReactNode => {
export const markChildText = (
children: React.ReactNode,
markedWordIndex: number
): React.ReactNode => {
const newChildren = [];

let currentIndex = 0;
Expand All @@ -39,15 +45,19 @@ export const markChildText = (children: React.ReactNode, markedWordIndex: number
return child;
}

const hasWhiteSpaceBeforeFirstWord = (child as string).match(/^\s/) !== null;
const hasWhiteSpaceAfterLastWord = (child as string).match(/.*\s$/) !== null;
const hasWhiteSpaceBeforeFirstWord =
(child as string).match(/^\s/) !== null;
const hasWhiteSpaceAfterLastWord =
(child as string).match(/.*\s$/) !== null;

const whitespaceOrNewlineRegex = /[\s\n]/;
const words = (child as string).split(whitespaceOrNewlineRegex).filter(word => word.trim().length > 0);
const words = (child as string)
.split(whitespaceOrNewlineRegex)
.filter(word => word.trim().length > 0);

const doesChildContainHighlightedWord =
markedWordIndex >= currentIndex &&
markedWordIndex < currentIndex + words.length;
markedWordIndex >= currentIndex &&
markedWordIndex < currentIndex + words.length;

if (!doesChildContainHighlightedWord) {
currentIndex += words.length;
Expand All @@ -56,22 +66,30 @@ export const markChildText = (children: React.ReactNode, markedWordIndex: number

const wordIndexInsideChild = markedWordIndex - currentIndex;
const textBeforeHighlightedWord = words
.slice(0, wordIndexInsideChild)
.join(" ");
.slice(0, wordIndexInsideChild)
.join(" ");
const textAfterHighlightedWord = words
.slice(wordIndexInsideChild + 1)
.join(" ");
.slice(wordIndexInsideChild + 1)
.join(" ");
const highlightedWordComponent = React.createElement(
"mark",
null,
words[wordIndexInsideChild]
"mark",
null,
words[wordIndexInsideChild]
);

currentIndex += words.length;
return [
...[`${hasWhiteSpaceBeforeFirstWord ? " " : ""}${textBeforeHighlightedWord ? `${textBeforeHighlightedWord} ` : ""}`],
...[
`${hasWhiteSpaceBeforeFirstWord ? " " : ""}${
textBeforeHighlightedWord ? `${textBeforeHighlightedWord} ` : ""
}`
],
highlightedWordComponent,
...[`${textAfterHighlightedWord ? ` ${textAfterHighlightedWord}` : ""}${hasWhiteSpaceAfterLastWord ? " " : ""}`],
...[
`${textAfterHighlightedWord ? ` ${textAfterHighlightedWord}` : ""}${
hasWhiteSpaceAfterLastWord ? " " : ""
}`
]
];
}
return child;
Expand All @@ -87,7 +105,9 @@ const WordMarker: React.FunctionComponent<WordMarkerProps> = props => {
>(props.children);

React.useEffect(() => {
setManipulatedChildren(markChildText(props.children, props.markedWordIndex));
setManipulatedChildren(
markChildText(props.children, props.markedWordIndex)
);
}, [props.children, props.markedWordIndex]);

return <>{manipulatedChildren}</>;
Expand Down
14 changes: 9 additions & 5 deletions src/internals/hooks/UseSpeechMarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const useSpeechMarks = (
word: ""
};
const [currentWord, setCurrentWord] = React.useState<CurrentWord>(
noWordSelected
noWordSelected
);
const timeoutHandles = React.useRef<NodeJS.Timeout[]>([]);

Expand All @@ -46,10 +46,14 @@ const useSpeechMarks = (
return !isSsmlTag;
})
.map((speechMark: SpeechMark, index: number) =>
setTimeout(() => setCurrentWord({
index,
word: speechMark.value
}), speechMark.time)
setTimeout(
() =>
setCurrentWord({
index,
word: speechMark.value
}),
speechMark.time
)
)
);
};
Expand Down
32 changes: 22 additions & 10 deletions src/internals/utils/extractSpeechOutputBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export interface SpeechOutputBlock {
text: string;
}

const extractSpeechOutputId = (startNode: Node, speechOutputComponentName: string) => {
const extractSpeechOutputId = (
startNode: Node,
speechOutputComponentName: string
) => {
const value = startNode.value as string;
const regex = new RegExp(`<${speechOutputComponentName}.*id="([^"]*)".*>`);
const matches = value.match(regex);
Expand All @@ -24,28 +27,37 @@ const extractSpeechOutputId = (startNode: Node, speechOutputComponentName: strin
}
};

const extractSpeechOutputBlocks = (mdxAst: Node, speechOutputComponentName: string): SpeechOutputBlock[] => {
const extractSpeechOutputBlocks = (
mdxAst: Node,
speechOutputComponentName: string
): SpeechOutputBlock[] => {
const speechOutputBlocks: SpeechOutputBlock[] = [];

const isStartNode = (node: unknown): node is Node => {
const value = (node as Node).value as string;
return (node as Node).type === "jsx" && value.startsWith(`<${speechOutputComponentName}`);
const value = (node as Node).value as string;
return (
(node as Node).type === "jsx" &&
value.startsWith(`<${speechOutputComponentName}`)
);
};

const isEndNode = (node: Node) => {
const value = node.value as string;
return node.type === "jsx" && value === `</${speechOutputComponentName}>`;
};
const isEndNode = (node: Node) => {
const value = node.value as string;
return node.type === "jsx" && value === `</${speechOutputComponentName}>`;
};

visit<Node>(
mdxAst,
isStartNode,
isStartNode,
(startNode: Node, startNodeIndex: number, parent: Node) => {
const relatedEndNode = findAfter(parent, startNode, isEndNode);
const nodesToGetTextFrom = between(parent, startNode, relatedEndNode);
const text = nodesToGetTextFrom.map(getSsmlFromMdxAst).join("");

const speechOutputId = extractSpeechOutputId(startNode, speechOutputComponentName);
const speechOutputId = extractSpeechOutputId(
startNode,
speechOutputComponentName
);
// TODO: also get voice parameter props and use them for generation (and check if they changed?)

speechOutputBlocks.push({
Expand Down
61 changes: 24 additions & 37 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"es6"
],
"sourceMap": true,
"jsx": "react",
"noUnusedLocals": true,
"forceConsistentCasingInFileNames": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"declaration": true,
"declarationMap": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"typeRoots": [
"./node_modules/@types"
],
"types": [
"node",
"jest"
],
"esModuleInterop": true,
"noEmit": false,
"noEmitOnError": true,
"outDir": ".",
"skipLibCheck": true
},
"include": [
"./src/**/*"
],
"exclude": [
"./node_modules/*",
"./src/__tests__/**/*"
]
"compilerOptions": {
"target": "es5",
"lib": ["dom", "es6"],
"sourceMap": true,
"jsx": "react",
"noUnusedLocals": true,
"forceConsistentCasingInFileNames": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"declaration": true,
"declarationMap": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"typeRoots": ["./node_modules/@types"],
"types": ["node", "jest"],
"esModuleInterop": true,
"noEmit": false,
"noEmitOnError": true,
"outDir": ".",
"skipLibCheck": true
},
"include": ["./src/**/*"],
"exclude": ["./node_modules/*", "./src/__tests__/**/*"]
}

0 comments on commit a1643c7

Please sign in to comment.