addSprite (input) {
        const errorPrefix = 'Sprite Upload Error:';
        if (typeof input === 'object' && !(input instanceof ArrayBuffer) &&
          !ArrayBuffer.isView(input)) {
            // If the input is an object and not any ArrayBuffer
            // or an ArrayBuffer view (this includes all typed arrays and DataViews)
            // turn the object into a JSON string, because we suspect
            // this is a project.json as an object
            // validate expects a string or buffer as input
            // TODO not sure if we need to check that it also isn't a data view
            input = JSON.stringify(input);
        }

        const validationPromise = new Promise((resolve, reject) => {
            const validate = require('scratch-parser');
            // The second argument of true below indicates to the parser/validator
            // that the given input should be treated as a single sprite and not
            // an entire project
            validate(input, true, (error, res) => {
                if (error) return reject(error);
                resolve(res);
            });
        });

        return validationPromise
            .then(validatedInput => {
                this.emit('addSprite*', {name: validatedInput[0].name});
                const projectVersion = validatedInput[0].projectVersion;
                if (projectVersion === 2) {
                    return this._addSprite2(validatedInput[0], validatedInput[1]);
                }
                if (projectVersion === 3) {
                    return this._addSprite3(validatedInput[0], validatedInput[1]);
                }
                return Promise.reject(`${errorPrefix} Unable to verify sprite version.`);
            })
            .then(() => this.runtime.emitProjectChanged())
            .catch(error => {
                // Intentionally rejecting here (want errors to be handled by caller)
                if (error.hasOwnProperty('validationError')) {
                    return Promise.reject(JSON.stringify(error));
                }
                return Promise.reject(`${errorPrefix} ${error}`);
            });
    }
/**
     * Add a sprite, this could be .sprite2 or .sprite3. Unpack and validate
     * such a file first.
     * @param {string | object} input A json string, object, or ArrayBuffer representing the project to load.
     * @return {!Promise} Promise that resolves after targets are installed.
     */
Add a sprite, this could be .sprite2 or .sprite3. Unpack and validate
such a file first.
@param {string | object} input A json string, object, or ArrayBuffer representing the project to load.
@return {!Promise} Promise that resolves after targets are installed.
Add a sprite this could be . sprite2 or . sprite3 . Unpack and validate such a file first . @param { string | object } input A json string object or ArrayBuffer representing the project to load . @return { !Promise } Promise that resolves after targets are installed .
addSprite (input) {
        const errorPrefix = 'Sprite Upload Error:';
        if (typeof input === 'object' && !(input instanceof ArrayBuffer) &&
          !ArrayBuffer.isView(input)) {
            input = JSON.stringify(input);
        }
        const validationPromise = new Promise((resolve, reject) => {
            const validate = require('scratch-parser');
            validate(input, true, (error, res) => {
                if (error) return reject(error);
                resolve(res);
            });
        });
        return validationPromise
            .then(validatedInput => {
                this.emit('addSprite*', {name: validatedInput[0].name});
                const projectVersion = validatedInput[0].projectVersion;
                if (projectVersion === 2) {
                    return this._addSprite2(validatedInput[0], validatedInput[1]);
                }
                if (projectVersion === 3) {
                    return this._addSprite3(validatedInput[0], validatedInput[1]);
                }
                return Promise.reject(`${errorPrefix} Unable to verify sprite version.`);
            })
            .then(() => this.runtime.emitProjectChanged())
            .catch(error => {
                if (error.hasOwnProperty('validationError')) {
                    return Promise.reject(JSON.stringify(error));
                }
                return Promise.reject(`${errorPrefix} ${error}`);
            });
    }
addSprite ( input ) { const errorPrefix = 'Sprite Upload Error:' ; if ( typeof input === 'object' && ! ( input instanceof ArrayBuffer ) && ! ArrayBuffer . isView ( input ) ) { input = JSON . stringify ( input ) ; } const validationPromise = new Promise ( ( resolve , reject ) => { const validate = require ( 'scratch-parser' ) ; validate ( input , true , ( error , res ) => { if ( error ) return reject ( error ) ; resolve ( res ) ; } ) ; } ) ; return validationPromise . then ( validatedInput => { this . emit ( 'addSprite*' , { name : validatedInput [ 0 ] . name } ) ; const projectVersion = validatedInput [ 0 ] . projectVersion ; if ( projectVersion === 2 ) { return this . _addSprite2 ( validatedInput [ 0 ] , validatedInput [ 1 ] ) ; } if ( projectVersion === 3 ) { return this . _addSprite3 ( validatedInput [ 0 ] , validatedInput [ 1 ] ) ; } return Promise . reject ( ` ${ errorPrefix } ` ) ; } ) . then ( ( ) => this . runtime . emitProjectChanged ( ) ) . catch ( error => { if ( error . hasOwnProperty ( 'validationError' ) ) { return Promise . reject ( JSON . stringify ( error ) ) ; } return Promise . reject ( ` ${ errorPrefix } ${ error } ` ) ; } ) ; }
Add a sprite, this could be .sprite2 or .sprite3.
Add a sprite this could be . sprite2 or . sprite3 .
// If the input is an object and not any ArrayBuffer // or an ArrayBuffer view (this includes all typed arrays and DataViews) // turn the object into a JSON string, because we suspect // this is a project.json as an object // validate expects a string or buffer as input // TODO not sure if we need to check that it also isn't a data view // The second argument of true below indicates to the parser/validator // that the given input should be treated as a single sprite and not // an entire project // Intentionally rejecting here (want errors to be handled by caller)