Created
September 16, 2018 23:16
-
-
Save JakubNer/301e95c2dc5f0bc4e281fafcbcf3c14c to your computer and use it in GitHub Desktop.
Google Web Designer (workaround): insert time in front of labels and events at some point on the timeline.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AT_SECONDS = 0; | |
var INSERT_SECONDS = 0; | |
// Replace the string below with the text of the element containing labels and events that need to be shifted. | |
var STR = ` | |
` | |
// Grab string of timeline labels from attribute 'data-gwd-animation-labels' for an element. Make | |
// timeline space 'at_seconds'. Insert 'insert_seconds' of space. Console output are the new labels. | |
var rejig_labels = function (what, at_seconds, insert_seconds) { | |
var at_millis = Math.floor((at_seconds * 1000) / 20) * 20; | |
var insert_millis = Math.floor((insert_seconds * 1000) / 20) * 20; | |
var whats = what.split(';'); | |
var label_millis_map = {}; | |
var millis_label_map = {}; | |
var output = ""; | |
whats.forEach((val, i) => { | |
var key_val = val.split(','); | |
var millis = key_val[1]; | |
if (millis >= at_millis) { | |
millis = parseInt(millis) + parseInt(insert_millis); | |
} | |
label_millis_map[key_val[0]] = millis; | |
}) | |
Object.keys(label_millis_map).forEach((k,i) => { | |
millis_label_map[label_millis_map[k]] = k; | |
}) | |
Object.keys(millis_label_map).sort((a,b) => parseInt(a) - parseInt(b)).forEach((k,i) => { | |
output = output + (output.length > 0 ? ';' : '') + millis_label_map[k] + ',' + k; | |
}) | |
return output; | |
} | |
var rejig_events = function (buffer, at_seconds, insert_seconds) { | |
var at_millis = Math.floor((at_seconds * 1000) / 20) * 20; | |
var insert_millis = Math.floor((insert_seconds * 1000) / 20) * 20; | |
var matches = {} | |
var eventRegexp = /data-event-name="(.*?)"\s*data-event-time="(.*?)"/g; | |
var match = eventRegexp.exec(buffer); | |
while (match != null) { | |
var value = match[2]; | |
if (value >= at_millis) { | |
value = parseInt(value) + parseInt(insert_millis); | |
} | |
matches[match[1]] = value; | |
match = eventRegexp.exec(buffer); | |
} | |
for (var match in matches) { | |
var replacementRegexp = new RegExp('data-event-name="'+match+'"\\s*data-event-time=".*?"','g'); | |
buffer = buffer.replace(replacementRegexp,'data-event-name="'+match+'" data-event-time="' + matches[match] + '"'); | |
} | |
return buffer; | |
} | |
var rejig = function(str, at_seconds, insert_seconds) { | |
// rejig labels | |
var labelsRegexp = /data-gwd-animation-labels="(.*?)"/g; | |
var match = labelsRegexp.exec(str); | |
if (match != null) { | |
var replacement = rejig_labels(match[1], at_seconds, insert_seconds); | |
var replacementRegexp = new RegExp('data-gwd-animation-labels="(.*?)"','g'); | |
str = str.replace(replacementRegexp, 'data-gwd-animation-labels="' + replacement + '"'); | |
} | |
// rejig events | |
str = rejig_events(str, at_seconds, insert_seconds) | |
return str; | |
} | |
console.log(rejig(STR, AT_SECONDS, INSERT_SECONDS)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Work with the "Code View" in Google Web Designer: into STR (above) copy the HTML for the element with the target animation timeline where you need to shift labels and events by some number of seconds. This is the element with the data-gwd-animation-labels attribute and the event divs with data-event-time attributes. Replace AT_SECONDS and INSERT_SECONDS with desired values in seconds.