From 997e4ef9d7f79dd638cbcf6f2152f7b983fe646b Mon Sep 17 00:00:00 2001 From: Lionel Han <47760119+IGoByJoe@users.noreply.github.com> Date: Thu, 6 Aug 2020 09:55:08 -0700 Subject: fixed everything before merge (not cleaned) --- src/Utils.ts | 9 +++ src/client/views/nodes/AudioBox.tsx | 133 ++++++++++++++++++------------------ 2 files changed, 75 insertions(+), 67 deletions(-) (limited to 'src') diff --git a/src/Utils.ts b/src/Utils.ts index a01a94134..3f82aa3eb 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -378,6 +378,15 @@ export function timenow() { return now.toLocaleDateString() + ' ' + h + ':' + m + ' ' + ampm; } +export function formatTime(time: number) { + time = Math.round(time); + const hours = Math.floor(time / 60 / 60); + const minutes = Math.floor(time / 60) - (hours * 60); + const seconds = time % 60; + + return hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0'); +} + export function aggregateBounds(boundsList: { x: number, y: number, width?: number, height?: number }[], xpad: number, ypad: number) { const bounds = boundsList.map(b => ({ x: b.x, y: b.y, r: b.x + (b.width || 0), b: b.y + (b.height || 0) })).reduce((bounds, b) => ({ x: Math.min(b.x, bounds.x), y: Math.min(b.y, bounds.y), diff --git a/src/client/views/nodes/AudioBox.tsx b/src/client/views/nodes/AudioBox.tsx index 682aaaeed..1d060120e 100644 --- a/src/client/views/nodes/AudioBox.tsx +++ b/src/client/views/nodes/AudioBox.tsx @@ -4,14 +4,14 @@ import { observer } from "mobx-react"; import "./AudioBox.scss"; import { Cast, DateCast, NumCast, FieldValue, ScriptCast } from "../../../fields/Types"; import { AudioField, nullAudio } from "../../../fields/URLField"; -import { ViewBoxBaseComponent, ViewBoxAnnotatableComponent } from "../DocComponent"; +import { ViewBoxAnnotatableComponent } from "../DocComponent"; import { makeInterface, createSchema } from "../../../fields/Schema"; import { documentSchema } from "../../../fields/documentSchemas"; -import { Utils, returnTrue, emptyFunction, returnOne, returnTransparent, returnFalse, returnZero } from "../../../Utils"; +import { Utils, returnTrue, emptyFunction, returnOne, returnTransparent, returnFalse, returnZero, formatTime } from "../../../Utils"; import { runInAction, observable, reaction, IReactionDisposer, computed, action, trace, toJS } from "mobx"; import { DateField } from "../../../fields/DateField"; import { SelectionManager } from "../../util/SelectionManager"; -import { Doc, DocListCast } from "../../../fields/Doc"; +import { Doc, DocListCast, Opt } from "../../../fields/Doc"; import { ContextMenuProps } from "../ContextMenuItem"; import { ContextMenu } from "../ContextMenu"; import { Id } from "../../../fields/FieldSymbols"; @@ -21,22 +21,11 @@ import { Docs, DocUtils } from "../../documents/Documents"; import { ComputedField, ScriptField } from "../../../fields/ScriptField"; import { Networking } from "../../Network"; import { LinkAnchorBox } from "./LinkAnchorBox"; -import { FormattedTextBox } from "./formattedText/FormattedTextBox"; -import { RichTextField } from "../../../fields/RichTextField"; -import { AudioResizer } from "./AudioResizer"; import { List } from "../../../fields/List"; -import { LabelBox } from "./LabelBox"; -import { Transform } from "../../util/Transform"; import { Scripting } from "../../util/Scripting"; -import { ColorBox } from "./ColorBox"; import Waveform from "react-audio-waveform" import axios from "axios" - -// testing testing - -interface Window { - MediaRecorder: MediaRecorder; -} +const _global = (window /* browser */ || global /* node */) as any; declare class MediaRecorder { // whatever MediaRecorder has @@ -74,19 +63,18 @@ export class AudioBox extends ViewBoxAnnotatableComponent = []; _first: boolean = false; - _buckets: Array = new Array(); + _dragging = false; + _count: Array = []; + _timeline: Opt; + _duration = 0; private _isPointerDown = false; private _currMarker: any; + @observable _position: number = 0; + @observable _buckets: Array = new Array(); @observable private _height: number = NumCast(this.layoutDoc.height); - @observable private _finish: boolean = false; - @observable private _dragging: boolean = false; - @observable private _duration = 0; - @observable private _rect: Array = []; - // @observable private _buckets: Array = new Array(); - @observable private _paused: boolean = false; @observable private static _scrubTime = 0; @observable private _repeat: boolean = false; @@ -97,9 +85,10 @@ export class AudioBox extends ViewBoxAnnotatableComponent) { super(props); + + // onClick play script if (!AudioBox.RangeScript) { AudioBox.RangeScript = ScriptField.MakeScript(`scriptContext.playFrom((this.audioStart), (this.audioEnd))`, { scriptContext: "any" })!; } @@ -107,8 +96,6 @@ export class AudioBox extends ViewBoxAnnotatableComponent SelectionManager.SelectedDocuments(), selected => { const sel = selected.length ? selected[0].props.Document : undefined; let link; if (sel) { + // for determining if the link is created after recording (since it will use linkTIme rather than creation date) DocListCast(sel.links).map((l, i) => { let la1 = l.anchor1 as Doc; let la2 = l.anchor2 as Doc; @@ -163,13 +153,14 @@ export class AudioBox extends ViewBoxAnnotatableComponent AudioBox._scrubTime, (time) => this.layoutDoc.playOnSelect && this.playFromTime(AudioBox._scrubTime)); } + // for updating the timecode timecodeChanged = () => { const htmlEle = this._ele; if (this.audioState !== "recording" && htmlEle) { @@ -264,15 +256,15 @@ export class AudioBox extends ViewBoxAnnotatableComponent this.audioState = "recording"); setTimeout(this.updateRecordTime, 0); this._recorder.start(); - setTimeout(() => this._recorder && this.stopRecording(), 60 * 1000); // stop after an hour + setTimeout(() => this._recorder && this.stopRecording(), 60 * 60 * 1000); // stop after an hour } specificContextMenu = (e: React.MouseEvent): void => { const funcs: ContextMenuProps[] = []; - funcs.push({ description: (this.layoutDoc.playOnSelect ? "Don't play" : "Play") + " when document selected", event: () => this.layoutDoc.playOnSelect = !this.layoutDoc.playOnSelect, icon: "expand-arrows-alt" }); + funcs.push({ description: (this.layoutDoc.playOnSelect ? "Don't play" : "Play") + " when link is selected", event: () => this.layoutDoc.playOnSelect = !this.layoutDoc.playOnSelect, icon: "expand-arrows-alt" }); funcs.push({ description: (this.layoutDoc.hideMarkers ? "Don't hide" : "Hide") + " markers", event: () => this.layoutDoc.hideMarkers = !this.layoutDoc.hideMarkers, icon: "expand-arrows-alt" }); funcs.push({ description: (this.layoutDoc.hideLabels ? "Don't hide" : "Hide") + " labels", event: () => this.layoutDoc.hideLabels = !this.layoutDoc.hideLabels, icon: "expand-arrows-alt" }); - funcs.push({ description: (this.layoutDoc.playOnClick ? "Don't play" : "Play") + " onClick", event: () => this.layoutDoc.playOnClick = !this.layoutDoc.playOnClick, icon: "expand-arrows-alt" }); + funcs.push({ description: (this.layoutDoc.playOnClick ? "Don't play" : "Play") + " markers onClick", event: () => this.layoutDoc.playOnClick = !this.layoutDoc.playOnClick, icon: "expand-arrows-alt" }); ContextMenu.Instance?.addItem({ description: "Options...", subitems: funcs, icon: "asterisk" }); } @@ -364,10 +356,10 @@ export class AudioBox extends ViewBoxAnnotatableComponent { + .then(response => { let audioData = response.data; - await audioCtx.decodeAudioData(audioData, buffer => { + audioCtx.decodeAudioData(audioData, action(buffer => { let decodedAudioData = buffer.getChannelData(0); const NUMBER_OF_BUCKETS = 100; let bucketDataSize = Math.floor(decodedAudioData.length / NUMBER_OF_BUCKETS); @@ -386,7 +378,7 @@ export class AudioBox extends ViewBoxAnnotatableComponent { - const hours = Math.floor(time / 60 / 60); - const minutes = Math.floor(time / 60) - (hours * 60); - const seconds = time % 60; - - return hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0'); - } @action onHover = () => { @@ -719,21 +702,32 @@ export class AudioBox extends ViewBoxAnnotatableComponent; } + timelineRef = (timeline: HTMLDivElement) => { + const observer = new _global.ResizeObserver(action((entries: any) => { + for (const entry of entries) { + this.update(entry.contentRect.width, entry.contentRect.height); + this._position = entry.contentRect.width; + } + })); + timeline && observer.observe(timeline); + + this._timeline = timeline; + } + @action - update = () => { - if (this.layoutDoc._height) { + update = (width: number, height: number) => { + if (height) { this._height = 0.8 * NumCast(this.layoutDoc._height); - console.log(document.getElementById("timeline")?.clientWidth); - let width = document.getElementById("timeline")?.clientWidth; let canvas2 = document.getElementsByTagName("canvas")[0]; if (canvas2) { let oldWidth = canvas2.width; @@ -747,6 +741,8 @@ export class AudioBox extends ViewBoxAnnotatableComponent {!this.path ?
@@ -795,7 +796,7 @@ export class AudioBox extends ViewBoxAnnotatableComponent
-
{this.formatTime(Math.round(NumCast(this.layoutDoc.currentTimecode)))}
+
{formatTime(Math.round(NumCast(this.layoutDoc.currentTimecode)))}
: @@ -803,13 +804,13 @@ export class AudioBox extends ViewBoxAnnotatableComponent} : -
+
{/*onClick={this.layoutDoc.playOnSelect ? this.onPlay : undefined}*/}
{/*
*/} -
{ e.stopPropagation(); e.preventDefault(); }} onDoubleClick={e => this.change} +
{ e.stopPropagation(); e.preventDefault(); }} onDoubleClick={e => this.change} onPointerDown={e => { e.stopPropagation(); e.preventDefault(); @@ -823,8 +824,7 @@ export class AudioBox extends ViewBoxAnnotatableComponent
- {console.log(this.peaks)} + {/* {console.log(this.peaks)} */} {/* { this.playFrom(NumCast(m.audioStart), NumCast(m.audioEnd)); e.stopPropagation() }} > +
{ this.playFrom(NumCast(m.audioStart), NumCast(m.audioEnd)); e.stopPropagation() }} >
this.onPointerDown(e, m, true)}>
Doc.linkFollowHighlight(la1)} - onPointerDown={e => { if (e.button === 0 && !e.ctrlKey) { const wasPaused = this.audioState === "paused"; this.playFrom(linkTime); this.pause(); e.stopPropagation(); e.preventDefault(); } }} /> + onPointerDown={e => { if (e.button === 0 && !e.ctrlKey) { const wasPaused = this.audioState === "paused"; this.playFrom(linkTime); e.stopPropagation(); e.preventDefault(); } }} />
; })} -
{ e.stopPropagation(); e.preventDefault(); }} style={{ left: `${NumCast(this.layoutDoc.currentTimecode) / NumCast(this.dataDoc.duration, 1) * 100}%` }} /> +
{ e.stopPropagation(); e.preventDefault(); }} style={{ left: `${NumCast(this.layoutDoc.currentTimecode) / NumCast(this.dataDoc.duration, 1) * 100}%`, pointerEvents: "none" }} /> {this.audio}
- {this.formatTime(Math.round(NumCast(this.layoutDoc.currentTimecode)))} + {formatTime(Math.round(NumCast(this.layoutDoc.currentTimecode)))}
- {this.formatTime(Math.round(NumCast(this.dataDoc.duration)))} + {formatTime(Math.round(NumCast(this.dataDoc.duration)))}
@@ -947,5 +947,4 @@ export class AudioBox extends ViewBoxAnnotatableComponent; } } - -// Scripting.addGlobal(function playFrom(audioDoc: Doc, start: number, end: number) { return audioDoc.playFrom(start, end); }) \ No newline at end of file +Scripting.addGlobal(function formatToTime(time: number): any { return formatTime(time); }); \ No newline at end of file -- cgit v1.2.3-70-g09d2