From 2022ed774392c031798b6185c7036573a64942f3 Mon Sep 17 00:00:00 2001 From: Ale M Date: Fri, 17 Jun 2022 15:26:11 +0200 Subject: [PATCH] Add `name` attribute to Document object --- Source/dom/models/Document.js | 11 +++++++++++ Source/dom/models/__tests__/Document.test.js | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/Source/dom/models/Document.js b/Source/dom/models/Document.js index 26c031ef..3c989f2d 100644 --- a/Source/dom/models/Document.js +++ b/Source/dom/models/Document.js @@ -589,6 +589,17 @@ Document.define('path', { }, }) +Document.define('name', { + exportable: true, + get() { + if (this.path !== undefined) { + return this.path.split('/').pop() + } else { + return 'Untitled.sketch' + } + }, +}) + /** * A list of document colors * diff --git a/Source/dom/models/__tests__/Document.test.js b/Source/dom/models/__tests__/Document.test.js index 62b55fe6..03733187 100644 --- a/Source/dom/models/__tests__/Document.test.js +++ b/Source/dom/models/__tests__/Document.test.js @@ -407,3 +407,11 @@ test('can remove all swatches in Document', (_context, document) => { document.swatches = [] expect(document.swatches.length).toBe(0) }) + +test('can get Document name', () => { + const document = new Document() + expect(document.name).toBe('Untitled.sketch') + document.save('test.sketch', {}, () => { + expect(document.name).toBe('test.sketch') + }) +})