Files
blockly/core/variable_model.ts
Christopher Allen b0a7c004a9 refactor(build): Delete Closure Library (#7415)
* fix(build): Restore erroneously-deleted filter function

  This was deleted in PR #7406 as it was mainly being used to
  filter core/ vs. test/mocha/ deps into separate deps files -
  but it turns out also to be used for filtering error
  messages too.  Oops.

* refactor(tests): Migrate advanced compilation test to ES Modules

* refactor(build): Migrate main.js to TypeScript

  This turns out to be pretty straight forward, even if it would
  cause crashing if one actually tried to import this module
  instead of just feeding it to Closure Compiler.

* chore(build): Remove goog.declareModuleId calls

  Replace goog.declareModuleId calls with a comment recording the
  former module ID for posterity (or at least until we decide
  how to reformat the renamings file.

* chore(tests): Delete closure/goog/*

  For the moment we still need something to serve as base.js for
  the benefit of closure-make-deps, so we keep a vestigial
  base.js around, containing only the @provideGoog declaration.

* refactor(build): Remove vestigial base.js

  By changing slightly the command line arguments to
  closure-make-deps and closure-calculate-chunks the need to have
  any base.js is eliminated.

* chore: Typo fix for PR #7415
2023-08-31 00:24:47 +01:00

80 lines
2.3 KiB
TypeScript

/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Components for the variable model.
*
* @class
*/
// Former goog.module ID: Blockly.VariableModel
// Unused import preserved for side-effects. Remove if unneeded.
import './events/events_var_create.js';
import * as idGenerator from './utils/idgenerator.js';
import type {Workspace} from './workspace.js';
/**
* Class for a variable model.
* Holds information for the variable including name, ID, and type.
*
* @see {Blockly.FieldVariable}
*/
export class VariableModel {
type: string;
private readonly id_: string;
/**
* @param workspace The variable's workspace.
* @param name The name of the variable. This is the user-visible name (e.g.
* 'my var' or '私の変数'), not the generated name.
* @param opt_type The type of the variable like 'int' or 'string'.
* Does not need to be unique. Field_variable can filter variables based
* on their type. This will default to '' which is a specific type.
* @param opt_id The unique ID of the variable. This will default to a UUID.
*/
constructor(
public workspace: Workspace,
public name: string,
opt_type?: string,
opt_id?: string,
) {
/**
* The type of the variable, such as 'int' or 'sound_effect'. This may be
* used to build a list of variables of a specific type. By default this is
* the empty string '', which is a specific type.
*
* @see {Blockly.FieldVariable}
*/
this.type = opt_type || '';
/**
* A unique ID for the variable. This should be defined at creation and
* not change, even if the name changes. In most cases this should be a
* UUID.
*/
this.id_ = opt_id || idGenerator.genUid();
}
/** @returns The ID for the variable. */
getId(): string {
return this.id_;
}
/**
* A custom compare function for the VariableModel objects.
*
* @param var1 First variable to compare.
* @param var2 Second variable to compare.
* @returns -1 if name of var1 is less than name of var2, 0 if equal, and 1 if
* greater.
* @internal
*/
static compareByName(var1: VariableModel, var2: VariableModel): number {
return var1.name.localeCompare(var2.name, undefined, {sensitivity: 'base'});
}
}