Best JavaScript code snippet using storybook-root
SiqStoryTellerCtrl.ts
Source:SiqStoryTellerCtrl.ts
1import {SiqStoryTwist} from 'siqstory-journalist/.src/types';2import {SiqStoryNode} from 'siqstory-journalist/.src/types';3let nodeIdToTextNode = {};4class ClickBubble {5 downtime: number;6 element: HTMLElement;7 constructor() {8 var self = this;9 self.downtime = Date.now();10 self.element = document.createElement('div');11 self.element.classList.add('click-bubble');12 setTimeout(function() {13 self.element.classList.add('down');14 });15 }16 up() {17 var self = this;18 setTimeout(function() {19 self.element.classList.remove('down');20 if (self.element.parentNode) {21 self.element.parentNode.removeChild(self.element);22 }23 }, Math.max(100 - (Date.now() - this.downtime), 0))24 }25}26function isElement(node: Node): node is Element {27 return node && (<Element>node).setAttribute !== undefined;28}29function isTextInput(node: Node): node is HTMLInputElement {30 return node && (<HTMLInputElement>node).value !== undefined;31}32export class SiqStoryTellerCtrl {33 private iframe: HTMLIFrameElement;34 private element: HTMLElement;35 private pointer: HTMLElement;36 private storyIndex: number = 0;37 private story: SiqStoryTwist[];38 private currentClickBubble: ClickBubble;39 constructor($element, LibrarySrvc) {40 var self = this;41 self.iframe = $element.find('iframe')[0];42 self.element = $element.find('.story-teller')[0];43 self.pointer = $element.find('.story-teller-mouse-cursor')[0];44 // clear it out cause it's easier to go from the root45 let idocument = self.iframe.contentDocument;46 idocument.open();47 idocument.write("<!DOCTYPE html>");48 idocument.write("<html>");49 idocument.write("<head></head>");50 idocument.write("<body>this is the iframe</body>");51 idocument.write("</html>");52 idocument.close();53 self.iframe.contentDocument.removeChild(self.iframe.contentDocument.childNodes[1]);54 LibrarySrvc.getLatestStory().then(function(story: SiqStoryTwist[]) {55 self.story = story;56 self.playNextStoryFrame();57 });58 }59 playNextStoryFrame() {60 // put this in a timeout just to get freaking stack traces ANGULAR61 let self = this;62 let thisTwist = self.story[self.storyIndex];63 if (!thisTwist) {64 return;65 }66 let lastTwist = self.story[self.storyIndex - 1];67 let nextFrameDelay = Math.min(Math.ceil(thisTwist.timeSincePageLoad - (lastTwist && lastTwist.timeSincePageLoad || 0)), 1000);68 setTimeout(function() {69 let twist = thisTwist;70 let targetNode = twist.targetNode && self.findNodeByNodeId(twist.targetNode.nodeId);71 switch (twist.type) {72 case 'childList':73 if (twist.addedNodes) {74 twist.addedNodes.forEach(function(storyNode: SiqStoryNode) {75 if (!targetNode) {76 console.warn('could not find targetNode for addition', JSON.stringify(twist.targetNode));77 return;78 }79 let node = self.createNode(storyNode);80 if (node) {81 targetNode.appendChild(node);82 } else if (storyNode.nodeType === 1 || storyNode.nodeType === 3) {83 throw new Error('couldnt make node for element or text node');84 }85 });86 }87 if (twist.removedNodes) {88 twist.removedNodes.forEach(function(storyNode: SiqStoryNode) {89 if (!targetNode) {90 console.log('could not find targetNode for removal', JSON.stringify(twist.targetNode));91 return;92 }93 let removeNode;94 if (storyNode.nodeType === 3 || storyNode.nodeType === 8) {95 removeNode = self.findTextNode(storyNode, targetNode);96 delete nodeIdToTextNode[storyNode.nodeId];97 } else {98 removeNode = self.findNodeByNodeId(storyNode.nodeId, targetNode);99 }100 if (removeNode) {101 if (removeNode.parentNode !== targetNode) {102 console.log('removeNode isnt the child of the target at this point....', storyNode);103 return;104 }105 targetNode.removeChild(removeNode);106 }107 });108 }109 break;110 case 'attributes':111 if (isElement(targetNode)) {112 targetNode.setAttribute(twist.attributeName, twist.attributeValue);113 }114 break;115 case 'resize':116 self.element.style.width = (self.iframe.width = twist.width.toString()) + 'px';117 self.element.style.height = (self.iframe.height = twist.height.toString()) + 'px';118 break;119 case 'event':120 switch (twist.eventType) {121 case 'mousemove':122 var top = twist.clientY + 'px';123 var left = twist.clientX + 'px';124 self.pointer.style.top = top;125 self.pointer.style.left = left;126 if (self.currentClickBubble) {127 self.currentClickBubble.element.style.top = top;128 self.currentClickBubble.element.style.left = left;129 }130 break;131 case 'mousedown':132 self.pointer.classList.add('mousedown');133 var top = twist.clientY + 'px';134 var left = twist.clientX + 'px';135 self.currentClickBubble = new ClickBubble();136 self.currentClickBubble.element.style.top = top;137 self.currentClickBubble.element.style.left = left;138 self.element.appendChild(self.currentClickBubble.element);139 break;140 case 'mouseup':141 self.pointer.classList.remove('mousedown');142 self.currentClickBubble.up();143 self.currentClickBubble = null;144 break;145 case 'input':146 if (isTextInput(targetNode)) {147 targetNode.value = twist.textValue;148 }149 break;150 }151 break;152 }153 self.storyIndex++;154 self.playNextStoryFrame();155 }, nextFrameDelay);156 }157 createNode(storyNode: SiqStoryNode): Node {158 if (storyNode.nodeType === 3 || storyNode.nodeType === 8) {159 if (nodeIdToTextNode[storyNode.nodeId]) {160 return nodeIdToTextNode[storyNode.nodeId];161 }162 var textNode;163 if (storyNode.nodeType === 3) {164 textNode = this.iframe.contentDocument.createTextNode(storyNode.nodeValue);165 } else if (storyNode.nodeType === 8) {166 textNode = this.iframe.contentDocument.createComment(storyNode.nodeValue);167 }168 textNode['__siqStoryNodeId'] = storyNode.nodeId;169 nodeIdToTextNode[storyNode.nodeId] = textNode;170 return textNode;171 }172 if (storyNode.nodeType === 1) {173 let node = (<Element>this.findNodeByNodeId(storyNode.nodeId));174 if (!node) {175 node = this.iframe.contentDocument.createElement(storyNode.tagName);176 node.setAttribute('siq-story-node-id', storyNode.nodeId);177 if (storyNode.attributes) {178 Object.keys(storyNode.attributes).forEach(function(attributeName) {179 if (attributeName === 'siqStoryCSS') {180 node.innerHTML = storyNode.attributes[attributeName];181 } else {182 try {183 node.setAttribute(attributeName, storyNode.attributes[attributeName]);184 } catch (e) {185 console.log('got weird attribute', attributeName, storyNode.attributes[attributeName])186 }187 }188 });189 }190 }191 return node;192 }193 }194 findTextNode(storyNode, targetNode): Node {195 if (nodeIdToTextNode[storyNode.nodeId]) {196 return nodeIdToTextNode[storyNode.nodeId];197 }198 }199 findNodeByNodeId(nodeId, ancestorNode?: Element | Document): Element | Document {200 if (nodeId === 'document') {201 return this.iframe.contentDocument;202 }203 if (nodeId === 'body') {204 return this.iframe.contentDocument.body;205 }206 if (nodeId !== undefined) {207 ancestorNode = ancestorNode || this.iframe.contentDocument;208 return ancestorNode.querySelector('[siq-story-node-id="' + nodeId + '"]');209 }210 }...
video-story.ts
Source:video-story.ts
1export interface StoryInfo {2 title: string3 nodeID: number4 cid: number5}6export class StoryChoice implements StoryInfo {7 title: string8 nodeID: number9 cid: number10 default: boolean11 constructor(rawObject: { node_id: number, option: string, cid: number, is_default: number }) {12 this.title = rawObject.option13 this.nodeID = rawObject.node_id14 this.cid = rawObject.cid15 this.default = rawObject.is_default === 116 }17}18export class StoryNode implements StoryInfo {19 title: string20 nodeID: number21 aid: number22 cid: number23 graphVersion: number24 choices: StoryChoice[]25 choiceTime: number26 constructor(rawObject: { node_id: number, title: string, cid: number }, aid: number, graphVersion: number) {27 this.title = rawObject.title28 this.nodeID = rawObject.node_id29 this.aid = aid30 this.cid = rawObject.cid31 this.graphVersion = graphVersion32 this.choices = []33 this.choiceTime = -134 }35 async getChoices() {36 if (this.choices.length > 0) {37 return38 }39 const url = `https://api.bilibili.com/x/stein/nodeinfo?aid=${this.aid}&node_id=${this.nodeID}&graph_version=504`40 const json = await Ajax.getJsonWithCredentials(url)41 if (json.code !== 0) {42 console.error(`è·åé项失败: ${json.message}`)43 return44 }45 this.choices = json.data.edges.choices.map((it: any) => new StoryChoice(it)) as StoryChoice[]46 }47}48export class Story {49 startingNode: StoryNode50 nodeList: StoryNode[]51 constructor(nodeList: StoryNode[], startingNode?: StoryNode) {52 this.nodeList = nodeList53 if (startingNode) {54 this.startingNode = startingNode55 }56 else {57 [this.startingNode] = nodeList58 }59 }60 async getAllChoices() {61 return await Promise.all(this.nodeList.map(node => node.getChoices()))62 }63}64export const getStoryNodes = async (aid: number, graphVersion: number) => {65 const url = `https://api.bilibili.com/x/stein/nodeinfo?aid=${aid}&graph_version=${graphVersion}`66 const json = await Ajax.getJsonWithCredentials(url)67 if (json.code !== 0) {68 return json.message as string69 }70 const nodeList = json.data.story_list.map((it: any) => new StoryNode(it, aid, graphVersion)) as StoryNode[]71 const startingNode = nodeList.find(it => it.nodeID === json.data.node_id)72 if (!startingNode) {73 return 'è·åèµ·å§ç»ç¹å¤±è´¥'74 }75 startingNode.choiceTime = json.data.edges.show_time76 const choices = json.data.edges.choices.map((it: any) => new StoryChoice(it)) as StoryChoice[]77 startingNode.choices = choices78 return new Story(nodeList, startingNode)79}80export default {81 export: {82 StoryChoice,83 StoryNode,84 Story,85 getStoryNodes,86 }...
Using AI Code Generation
1import { storybookRoot } from 'storybook-root';2storybookRoot.storyNode('test', 'test', 'test');3import { storybookRoot } from 'storybook-root';4storybookRoot('test', 'test', 'test');5import { storybookRoot } from 'storybook-root';6storybookRoot('test2', 'test2', 'test2');7import { storybookRoot } from 'storybook-root';8storybookRoot('test3', 'test3', 'test3');9import { storybookRoot } from 'storybook-root';10storybookRoot('test4', 'test4', 'test4');11import { storybookRoot } from 'storybook-root';12storybookRoot('test5', 'test5', 'test5');13import { storybookRoot } from 'storybook-root';14storybookRoot('test6', 'test6', 'test6');15import { storybookRoot } from 'storybook-root';16storybookRoot('test7', 'test7', 'test7');17import { storybookRoot } from 'storybook-root';18storybookRoot('test8', 'test8', 'test8');19import { storybookRoot } from 'storybook-root';20storybookRoot('test9', 'test9', 'test9');21import { storybookRoot } from 'storybook-root';22storybookRoot('test10', 'test10', 'test10');23import { storybookRoot } from 'storybook-root';24storybookRoot('test
Using AI Code Generation
1var storybook = require('storybook-root');2var storyNode = storybook.storyNode;3var story = storyNode('testStory', 'testStory');4story.add('test', function() {5 console.log('test');6});7var storybook = require('storybook-root');8var storyNode = storybook.storyNode;9var story = storyNode('testStory', 'testStory');10story.add('test2', function() {11 console.log('test2');12});13var storybook = require('storybook-root');14var storyNode = storybook.storyNode;15var story = storyNode('testStory', 'testStory');16story.add('test3', function() {17 console.log('test3');18});19var storybook = require('storybook-root');20var storyNode = storybook.storyNode;21var story = storyNode('testStory', 'testStory');22story.add('test4', function() {23 console.log('test4');24});25var storybook = require('storybook-root');26var storyNode = storybook.storyNode;27var story = storyNode('testStory', 'testStory');28story.add('test5', function() {29 console.log('test5');30});31var storybook = require('storybook-root');32var storyNode = storybook.storyNode;33var story = storyNode('testStory', 'testStory');34story.add('test6', function() {35 console.log('test6');36});37var storybook = require('storybook-root');38var storyNode = storybook.storyNode;39var story = storyNode('testStory', 'testStory');40story.add('test7', function() {41 console.log('test7');42});43var storybook = require('storybook-root');44var storyNode = storybook.storyNode;45var story = storyNode('testStory', 'testStory');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!