Skip to content

Commit acfa56b

Browse files
author
Pietro Passarelli
committed
Saving progress
can realign only paragraph who add a change, however if all the words were to be changed in paragraph, even with same numbeer of words, it seems to hang. Also had to disable edge cases around merging paragraphs, splitting paragraphs,and deleting across paragraphs. Can only delete within a single paragraph for now
1 parent df6eb0a commit acfa56b

File tree

7 files changed

+118
-137
lines changed

7 files changed

+118
-137
lines changed

docs/notes/insert-slate-functions.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
```js
2+
const breakParagraph = () => {
3+
Editor.insertBreak(editor);
4+
};
5+
const insertTextInaudible = () => {
6+
Transforms.insertText(editor, '[INAUDIBLE]');
7+
};
8+
9+
const handleInsertMusicNote = () => {
10+
Transforms.insertText(editor, ''); // or ♪
11+
};
12+
```

package-lock.json

+6-105
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,19 @@
5858
"@fortawesome/react-fontawesome": "^0.1.5",
5959
"@storybook/addon-storysource": "^5.3.18",
6060
"docx": "4.7.1",
61+
"lodash": "^4.17.20",
6162
"prop-types": "^15.7.2",
6263
"react-bootstrap": "^1.0.1",
6364
"sbd": "^1.0.18",
6465
"slate": "^0.59.0",
6566
"slate-history": "^0.59.0",
6667
"slate-react": "^0.59.0",
6768
"smpte-timecode": "^1.2.3",
68-
"stt-align-node": "^1.1.8"
69+
"stt-align-node": "^1.1.10"
6970
},
7071
"husky": {
7172
"hooks": {
7273
"pre-commit": "pretty-quick --staged"
7374
}
7475
}
75-
}
76+
}

src/components/index.js

+47-11
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,6 @@ export default function SlateTranscriptEditor(props) {
149149
return tmpMediaType;
150150
};
151151

152-
const breakParagraph = () => {
153-
Editor.insertBreak(editor);
154-
};
155-
const insertTextInaudible = () => {
156-
Transforms.insertText(editor, '[INAUDIBLE]');
157-
};
158-
159-
const handleInsertMusicNote = () => {
160-
Transforms.insertText(editor, '♫'); // or ♪
161-
};
162-
163152
const handleSetShowSpeakersCheatShet = () => {
164153
setShowSpeakersCheatShet(!showSpeakersCheatShet);
165154
};
@@ -336,6 +325,7 @@ export default function SlateTranscriptEditor(props) {
336325

337326
// TODO: refacto this function, to be cleaner and easier to follow.
338327
const handleRestoreTimecodes = async (inlineTimecodes = false) => {
328+
console.log('handleRestoreTimecodes');
339329
if (!isContentModified && !inlineTimecodes) {
340330
return value;
341331
}
@@ -416,7 +406,52 @@ export default function SlateTranscriptEditor(props) {
416406
setIsPauseWhiletyping(!isPauseWhiletyping);
417407
};
418408

409+
// TODO: revisit logic for
410+
// - splitting paragraph via enter key
411+
// - merging paragraph via delete
412+
// - merging paragraphs via deleting across paragraphs
419413
const handleOnKeyDown = (event) => {
414+
console.log('event.key', event.key);
415+
if (event.key === 'Enter') {
416+
// intercept Enter, and
417+
event.preventDefault();
418+
console.log('disabling enter/paragraph split while tweaking alignment');
419+
console.log('For now cdisabling enter key to split a paragraph, while figuring out the aligment issue');
420+
return;
421+
const selection = editor.selection;
422+
console.log('selection', selection);
423+
const orderedSelection = [selection.anchor, selection.focus].sort((a, b) => {
424+
return a.path[0] - b.path[0];
425+
});
426+
427+
const selectionStart = orderedSelection[0];
428+
const selectionEnd = orderedSelection[1];
429+
const currentParagraph = editor.children[selectionStart.path[0]];
430+
console.log('selectionStart.path[0]', selectionStart.path[0]);
431+
console.log('currentParagraph', currentParagraph);
432+
// Editor.insertBreak(editor);
433+
// Transforms.splitNodes(editor);
434+
// const element = { type: 'image', url, children: [{ text: '' }] };
435+
Editor.deleteFragment(editor, selectionStart.path[0]);
436+
const { startSec, endSec } = getSelectionNodes(editor, editor.selection);
437+
}
438+
if (event.key === 'Backspace') {
439+
const selection = editor.selection;
440+
console.log('selection', selection);
441+
console.log(selection.anchor.path[0], selection.focus.path[0]);
442+
// across paragraph
443+
if (selection.anchor.path[0] !== selection.focus.path[0]) {
444+
console.log('For now cannot merge paragraph via delete across paragraphs, while figuring out the aligment issue');
445+
event.preventDefault();
446+
return;
447+
}
448+
// beginning of a paragrraph
449+
if (selection.anchor.offset === 0 && selection.focus.offset === 0) {
450+
console.log('For now cannot merge paragraph via delete, while figuring out the aligment issue');
451+
event.preventDefault();
452+
return;
453+
}
454+
}
420455
setIsContentIsModified(true);
421456
if (isPauseWhiletyping) {
422457
// logic for pause while typing
@@ -852,6 +887,7 @@ export default function SlateTranscriptEditor(props) {
852887
disabled={isProcessing}
853888
onClick={async () => {
854889
try {
890+
console.log('faSync');
855891
setIsProcessing(true);
856892
await handleRestoreTimecodes();
857893
} finally {

src/util/export-adapters/slate-to-dpe/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const createDpeParagraphsFromSlateJs = (currentContent, newEntities) => {
4545
*/
4646
const converSlateToDpe = (currentContent, words) => {
4747
const alignedWords = updateTimestampsHelper(currentContent, words);
48+
console.log('alignedWords', alignedWords);
4849
const updatedContent = createDpeParagraphsFromSlateJs(currentContent, alignedWords);
4950
return { words: alignedWords, paragraphs: updatedContent };
5051
};

src/util/export-adapters/slate-to-dpe/update-timestamps/index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const createSlateContentFromSlateJsParagraphs = (currentContent, newEntit
2626
const wordsInBlock = countWords(text);
2727
const blockEntites = newEntities.slice(totalWords, totalWords + wordsInBlock);
2828
let speaker = block.speaker;
29-
console.log('blockEntites', blockEntites);
29+
// console.log('blockEntites', blockEntites);
3030
const start = parseFloat(blockEntites[0].start);
3131
const end = parseFloat(blockEntites[blockEntites.length - 1].end);
3232
const currentParagraph = { start, end };
@@ -60,10 +60,8 @@ export const createSlateContentFromSlateJsParagraphs = (currentContent, newEntit
6060
* @return slateJS value
6161
*/
6262
const updateTimestamps = (currentContent, words) => {
63-
const alignedWords = updateTimestampsHelper(currentContent, words);
64-
// TODO: there seem to be some words without text attribute, so doing a quick fix clean up
65-
66-
const updatedContent = createSlateContentFromSlateJsParagraphs(currentContent, alignedWords);
63+
const updatedContent = updateTimestampsHelper(currentContent, words);
64+
// const updatedContent = createSlateContentFromSlateJsParagraphs(currentContent, alignedWords);
6765
return updatedContent;
6866
};
6967

src/util/export-adapters/slate-to-dpe/update-timestamps/update-timestamps-helper.js

+46-14
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,63 @@
66
import { alignSTT } from 'stt-align-node';
77
// import alignSTT from '../../../stt-align-node';
88
import slateToText from '../../txt';
9-
// Yo
9+
import convertDpeToSlate from '../../../dpe-to-slate';
10+
import { shortTimecode } from '../../../timecode-converter/index.js';
11+
import _ from 'lodash';
12+
// import difference from 'lodash/difference';
13+
14+
function comparator(object, other) {
15+
return _.isEqual(object.children[0].text, other.children[0].text);
16+
}
17+
1018
/**
1119
* Update timestamps usign stt-align module
1220
* @param {*} currentContent - slate js value
1321
* @param {*} words - list of stt words
1422
* @return slateJS value
1523
*/
24+
// TODO: in stt-align-node if all the words are completely diff, it seems to freeze.
25+
// Look into why in stt-align-node github repo etc..
1626
export const updateTimestampsHelper = (currentContent, dpeTranscript) => {
27+
// TODO: figure out if can remove the cloneDeep option
28+
const newCurrentContent = _.cloneDeep(currentContent);
1729
// trying to align only text that changed
1830

19-
// covert t=slate to text to use alignment module
20-
const currentText = slateToText({
21-
value: currentContent,
22-
speakers: false,
23-
timecodes: false,
24-
atlasFormat: false,
31+
// TODO: ideally, you save the slate converted content in the parent component when
32+
// component is initialized so don't need to re-convert this from dpe all the time.
33+
const originalContentSlateFormat = convertDpeToSlate(dpeTranscript);
34+
35+
// TODO: add the ID further upstream to be able to skip this step.
36+
// we are adding the index for the paragraph,to be able to update the words attribute in the paragraph and easily replace that paragraph in the
37+
// slate editor content.
38+
// Obv this wouldn't work, if re-enable the edge cases, disabled above in handleOnKeyDown
39+
const currentSlateContentWithId = currentContent.map((paragraph, index) => {
40+
const newParagraph = { ...paragraph };
41+
newParagraph.id = index;
42+
return newParagraph;
2543
});
26-
const alignedWords = alignSTT(dpeTranscript, currentText);
27-
// clean up
28-
const alignedWordsCleanedUp = alignedWords.filter((word) => {
29-
if (word.text) {
30-
return word;
31-
}
44+
const diffParagraphs = _.differenceWith(currentSlateContentWithId, originalContentSlateFormat, comparator);
45+
46+
diffParagraphs.forEach((diffParagraph) => {
47+
// TODO: figure out if can remove the cloneDeep option
48+
let newDiffParagraph = _.cloneDeep(diffParagraph);
49+
let alignedWordsTest = alignSTT(newDiffParagraph.children[0], newDiffParagraph.children[0].text);
50+
newDiffParagraph.children[0].words = alignedWordsTest;
51+
// also adjust paragraph timecode
52+
newDiffParagraph.start = alignedWordsTest[0].start;
53+
newDiffParagraph.startTimecode = shortTimecode(alignedWordsTest[0].start);
54+
newCurrentContent[newDiffParagraph.id] = newDiffParagraph;
3255
});
33-
return alignedWordsCleanedUp;
56+
57+
// covert slate to text to use alignment module
58+
// const currentText = slateToText({
59+
// value: currentContent,
60+
// speakers: false,
61+
// timecodes: false,
62+
// atlasFormat: false,
63+
// });
64+
// const alignedWords = alignSTT(dpeTranscript, currentText);
65+
return newCurrentContent;
3466
};
3567

3668
export default updateTimestampsHelper;

0 commit comments

Comments
 (0)