Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change layer name #963

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/editor/components/components/Sidebar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { cloneEntity, removeSelectedEntity } from '../../lib/entity';
import {
cloneEntity,
removeSelectedEntity,
renameEntity
} from '../../lib/entity';
import { Button } from '../components';
import ComponentsContainer from './ComponentsContainer';
import Events from '../../lib/Events';
Expand Down Expand Up @@ -98,6 +102,12 @@ export default class Sidebar extends React.Component {
<></>
) : (
<div id="sidebar-buttons">
<Button
variant={'toolbtn'}
onClick={() => renameEntity(entity)}
>
Rename
</Button>
<Button
variant={'toolbtn'}
onClick={() => cloneEntity(entity)}
Expand Down
61 changes: 61 additions & 0 deletions src/editor/lib/commands/EntityRenameCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Events from '../Events.js';
import { Command } from '../command.js';
import { createUniqueId } from '../entity.js';

export class EntityRenameCommand extends Command {
constructor(editor, entity) {
super(editor);

this.type = 'entityrename';
this.name = 'Rename Entity';
this.updatable = true; // Allow updating for consecutive renames

if (!entity.id) {
entity.id = createUniqueId();
}
this.entityId = entity.id;
this.oldName = entity.getAttribute('data-layer-name') || '';
this.newName = null; // Will be set during execute
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be set in the constructor otherwise you can't undo/redo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo/redo works in my testing?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right it works, this.newName is set so it's fine afterwards

}

execute() {
const entity = document.getElementById(this.entityId);
if (!entity) return;

// If newName hasn't been set (first execution), prompt for it
if (this.newName === null) {
const promptedName = prompt('Enter new name for entity', this.oldName);
// If user cancels or enters empty name, abort
if (!promptedName) return;
this.newName = promptedName;
}

// Apply the new name
entity.setAttribute('data-layer-name', this.newName);
Events.emit('entityrenamed', {
entity,
oldName: this.oldName,
newName: this.newName
});
AFRAME.INSPECTOR.selectEntity(entity);
}

undo() {
const entity = document.getElementById(this.entityId);
if (entity) {
// Restore the old name
entity.setAttribute('data-layer-name', this.oldName);
Events.emit('entityrenamed', {
entity,
oldName: this.newName,
newName: this.oldName
});
AFRAME.INSPECTOR.selectEntity(entity);
}
}

update(command) {
// Handle consecutive renames by updating the newName
this.newName = command.newName;
}
}
2 changes: 2 additions & 0 deletions src/editor/lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EntityCloneCommand } from './EntityCloneCommand.js';
import { EntityCreateCommand } from './EntityCreateCommand.js';
import { EntityRemoveCommand } from './EntityRemoveCommand.js';
import { EntityUpdateCommand } from './EntityUpdateCommand.js';
import { EntityRenameCommand } from './EntityRenameCommand.js';

export const commandsByType = new Map();
commandsByType.set('componentadd', ComponentAddCommand);
Expand All @@ -12,3 +13,4 @@ commandsByType.set('entityclone', EntityCloneCommand);
commandsByType.set('entitycreate', EntityCreateCommand);
commandsByType.set('entityremove', EntityRemoveCommand);
commandsByType.set('entityupdate', EntityUpdateCommand);
commandsByType.set('entityrename', EntityRenameCommand);
9 changes: 9 additions & 0 deletions src/editor/lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ export function cloneEntity(entity) {
return AFRAME.INSPECTOR.execute('entityclone', entity);
}

/**
* Rename an entity, inserting it after the cloned one.
* @param {Element} entity Entity to clone
* @returns {Element} The clone
*/
export function renameEntity(entity) {
return AFRAME.INSPECTOR.execute('entityrename', entity);
}

/**
* Clone an entity, inserting it after the cloned one. This is the implementation of the entityclone command.
* @param {Element} entity Entity to clone
Expand Down
Loading