2021-07-14 10:42:16 +00:00
|
|
|
import { Data, Page, PageState, TLDocument } from 'types'
|
2021-07-09 08:59:43 +00:00
|
|
|
import { decompress, compress } from 'utils'
|
2021-06-11 10:11:42 +00:00
|
|
|
import state from './state'
|
2021-07-01 14:03:02 +00:00
|
|
|
import { uniqueId } from 'utils/utils'
|
2021-06-12 19:40:26 +00:00
|
|
|
import * as idb from 'idb-keyval'
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-07-07 13:02:48 +00:00
|
|
|
const CURRENT_VERSION = 'code_slate_0.0.9'
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-12 19:40:26 +00:00
|
|
|
function storageId(fileId: string, label: string, id?: string) {
|
|
|
|
return [CURRENT_VERSION, fileId, label, id].filter(Boolean).join('_')
|
2021-06-07 11:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Storage {
|
2021-06-23 12:46:16 +00:00
|
|
|
previousSaveHandle?: any // FileSystemHandle
|
2021-06-11 10:31:31 +00:00
|
|
|
|
2021-07-07 13:53:03 +00:00
|
|
|
firstLoad(data: Data, roomId = 'TESTING') {
|
2021-07-07 12:46:46 +00:00
|
|
|
const lastOpenedFileId =
|
|
|
|
roomId || localStorage.getItem(`${CURRENT_VERSION}_lastOpened`)
|
2021-06-12 19:40:26 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// 1. Load Document from Local Storage
|
|
|
|
// Using the "last opened file id" in local storage.
|
|
|
|
if (lastOpenedFileId !== null) {
|
2021-06-25 11:14:42 +00:00
|
|
|
// Load state from local storage
|
|
|
|
const savedState = localStorage.getItem(
|
|
|
|
storageId(lastOpenedFileId, 'document-state', lastOpenedFileId)
|
2021-06-16 12:09:45 +00:00
|
|
|
)
|
2021-06-12 19:40:26 +00:00
|
|
|
|
2021-07-07 12:46:46 +00:00
|
|
|
if (!savedState) {
|
2021-06-25 11:14:42 +00:00
|
|
|
// If no state with that document was found, create a fresh random id.
|
2021-07-07 12:46:46 +00:00
|
|
|
data.document.id = roomId ? roomId : uniqueId()
|
2021-06-16 12:09:45 +00:00
|
|
|
} else {
|
2021-06-25 11:14:42 +00:00
|
|
|
// If we did find a state and document, load it into state.
|
2021-07-14 08:04:07 +00:00
|
|
|
const restoredState: Data = JSON.parse(decompress(savedState))
|
|
|
|
|
|
|
|
// Lose the settings, these are meant to be stable
|
|
|
|
delete restoredState.settings
|
2021-06-16 12:09:45 +00:00
|
|
|
|
|
|
|
// Merge restored data into state.
|
2021-07-14 08:04:07 +00:00
|
|
|
Object.assign(data, restoredState)
|
2021-06-16 12:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-12 19:40:26 +00:00
|
|
|
|
2021-07-07 12:46:46 +00:00
|
|
|
this.load(data)
|
2021-06-16 12:09:45 +00:00
|
|
|
}
|
2021-06-12 19:40:26 +00:00
|
|
|
|
2021-06-27 12:12:28 +00:00
|
|
|
saveAppStateToLocalStorage = (data: Data) => {
|
|
|
|
localStorage.setItem(
|
|
|
|
storageId(data.document.id, 'document-state', data.document.id),
|
|
|
|
compress(JSON.stringify(data))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
saveDocumentToLocalStorage(data: Data) {
|
|
|
|
const document = this.getCompleteDocument(data)
|
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
storageId(data.document.id, 'document', data.document.id),
|
|
|
|
compress(JSON.stringify(document))
|
|
|
|
)
|
2021-07-07 12:46:46 +00:00
|
|
|
|
|
|
|
localStorage.setItem(`${CURRENT_VERSION}_lastOpened`, data.document.id)
|
2021-06-11 10:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
getCompleteDocument = (data: Data) => {
|
|
|
|
// Create a safely mutable copy of the data
|
|
|
|
const document: TLDocument = { ...data.document }
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// Try to find the document's pages and page states in local storage.
|
|
|
|
Object.keys(document.pages).forEach((pageId) => {
|
|
|
|
const savedPage = localStorage.getItem(
|
|
|
|
storageId(document.id, 'page', pageId)
|
|
|
|
)
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
if (savedPage !== null) {
|
|
|
|
document.pages[pageId] = JSON.parse(decompress(savedPage))
|
|
|
|
}
|
|
|
|
})
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
return document
|
2021-06-07 11:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
savePageState = (data: Data) => {
|
|
|
|
localStorage.setItem(
|
|
|
|
storageId(data.document.id, 'lastPageState', data.document.id),
|
|
|
|
JSON.stringify(data.pageStates[data.currentPageId])
|
2021-06-11 22:06:09 +00:00
|
|
|
)
|
2021-06-16 12:09:45 +00:00
|
|
|
}
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
loadDocumentFromJson(data: Data, json: string) {
|
|
|
|
const restoredDocument: { document: TLDocument; pageState: PageState } =
|
|
|
|
JSON.parse(json)
|
|
|
|
|
|
|
|
data.document = restoredDocument.document
|
2021-07-07 13:53:03 +00:00
|
|
|
data.pageStates[restoredDocument.pageState.id] = restoredDocument.pageState
|
2021-06-16 12:09:45 +00:00
|
|
|
|
|
|
|
// Save pages to local storage, possibly overwriting unsaved local copies
|
|
|
|
Object.values(data.document.pages).forEach((page) => {
|
|
|
|
localStorage.setItem(
|
|
|
|
storageId(data.document.id, 'page', page.id),
|
|
|
|
compress(JSON.stringify(page))
|
|
|
|
)
|
|
|
|
})
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
localStorage.setItem(
|
|
|
|
storageId(data.document.id, 'lastPageState', data.document.id),
|
|
|
|
JSON.stringify(restoredDocument.pageState)
|
|
|
|
)
|
2021-06-12 19:40:26 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// Save the new file as the last opened document id
|
|
|
|
localStorage.setItem(`${CURRENT_VERSION}_lastOpened`, data.document.id)
|
|
|
|
|
|
|
|
this.load(data)
|
2021-06-11 10:11:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
load(data: Data) {
|
|
|
|
// Once we've loaded data either from local storage or json, run through these steps.
|
|
|
|
data.pageStates = {}
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// 2. Load Pages from Local Storage
|
|
|
|
// Try to find the document's pages and page states in local storage.
|
|
|
|
Object.keys(data.document.pages).forEach((pageId) => {
|
2021-06-11 10:11:42 +00:00
|
|
|
const savedPage = localStorage.getItem(
|
|
|
|
storageId(data.document.id, 'page', pageId)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (savedPage !== null) {
|
2021-06-16 12:09:45 +00:00
|
|
|
// If we've found a page in local storage, set it into state.
|
|
|
|
data.document.pages[pageId] = JSON.parse(decompress(savedPage))
|
2021-06-11 10:11:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
const savedPageState = localStorage.getItem(
|
|
|
|
storageId(data.document.id, 'pageState', pageId)
|
|
|
|
)
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
if (savedPageState !== null) {
|
|
|
|
// If we've found a page state in local storage, set it into state.
|
|
|
|
data.pageStates[pageId] = JSON.parse(decompress(savedPageState))
|
|
|
|
} else {
|
|
|
|
// Or else create a new one.
|
|
|
|
data.pageStates[pageId] = {
|
|
|
|
id: pageId,
|
2021-07-09 08:59:43 +00:00
|
|
|
selectedIds: [],
|
2021-06-16 12:09:45 +00:00
|
|
|
camera: {
|
|
|
|
point: [0, 0],
|
|
|
|
zoom: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// 3. Restore the last page state
|
|
|
|
// Using the "last page state" in local storage.
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-07-07 12:46:46 +00:00
|
|
|
try {
|
|
|
|
const savedPageState = localStorage.getItem(
|
|
|
|
storageId(data.document.id, 'lastPageState', data.document.id)
|
|
|
|
)
|
2021-06-16 12:09:45 +00:00
|
|
|
const pageState = JSON.parse(decompress(savedPageState))
|
2021-07-07 12:46:46 +00:00
|
|
|
|
|
|
|
if (!data.document.pages[pageState.id]) {
|
|
|
|
throw new Error('Page state id not in document')
|
|
|
|
}
|
|
|
|
|
2021-07-09 08:59:43 +00:00
|
|
|
pageState.selectedIds = []
|
2021-06-16 12:09:45 +00:00
|
|
|
data.pageStates[pageState.id] = pageState
|
|
|
|
data.currentPageId = pageState.id
|
2021-07-07 12:46:46 +00:00
|
|
|
} catch (e) {
|
|
|
|
data.pageStates[data.currentPageId] = {
|
|
|
|
id: data.currentPageId,
|
2021-07-09 08:59:43 +00:00
|
|
|
selectedIds: [],
|
2021-07-07 12:46:46 +00:00
|
|
|
camera: {
|
|
|
|
point: [0, 0],
|
|
|
|
zoom: 1,
|
|
|
|
},
|
|
|
|
}
|
2021-06-16 12:09:45 +00:00
|
|
|
}
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-06-27 12:12:28 +00:00
|
|
|
// 4. Save the current app state / document
|
2021-06-16 12:09:45 +00:00
|
|
|
// The document is now "full" and ready. Whether we've restored a
|
|
|
|
// document or created a new one, save the entire current document.
|
2021-06-27 12:12:28 +00:00
|
|
|
this.saveDocumentToLocalStorage(data)
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-06-27 12:12:28 +00:00
|
|
|
// 4.1
|
|
|
|
// Also save the app state.
|
|
|
|
this.saveAppStateToLocalStorage(data)
|
2021-06-25 11:14:42 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// 4.1
|
|
|
|
// Also save out copies of each page separately.
|
|
|
|
Object.values(data.document.pages).forEach((page) => {
|
|
|
|
// Save page
|
|
|
|
localStorage.setItem(
|
|
|
|
storageId(data.document.id, 'page', page.id),
|
|
|
|
compress(JSON.stringify(page))
|
|
|
|
)
|
|
|
|
})
|
2021-06-13 13:24:03 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// Save the last page state
|
|
|
|
const currentPageState = data.pageStates[data.currentPageId]
|
2021-06-13 13:24:03 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
localStorage.setItem(
|
|
|
|
storageId(data.document.id, 'lastPageState', data.document.id),
|
|
|
|
JSON.stringify(currentPageState)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Finally, save the current document id as the "last opened" document id.
|
2021-06-11 22:06:09 +00:00
|
|
|
localStorage.setItem(`${CURRENT_VERSION}_lastOpened`, data.document.id)
|
2021-06-16 12:09:45 +00:00
|
|
|
|
|
|
|
// 5. Prepare the new state.
|
|
|
|
// Clear out the other pages from state.
|
|
|
|
Object.values(data.document.pages).forEach((page) => {
|
|
|
|
if (page.id !== data.currentPageId) {
|
|
|
|
page.shapes = {}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-07 12:46:46 +00:00
|
|
|
// Load the current page
|
|
|
|
this.loadPage(data, data.document.id, data.currentPageId)
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// Update camera for the new page state
|
|
|
|
document.documentElement.style.setProperty(
|
|
|
|
'--camera-zoom',
|
|
|
|
data.pageStates[data.currentPageId].camera.zoom.toString()
|
|
|
|
)
|
2021-06-11 10:11:42 +00:00
|
|
|
}
|
2021-06-11 22:06:09 +00:00
|
|
|
/* ---------------------- Pages --------------------- */
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-06-12 19:40:26 +00:00
|
|
|
async loadPreviousHandle() {
|
2021-06-23 12:46:16 +00:00
|
|
|
const handle = await idb.get('previous_handle')
|
|
|
|
|
2021-06-12 19:40:26 +00:00
|
|
|
if (handle !== undefined) {
|
|
|
|
this.previousSaveHandle = handle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 22:06:09 +00:00
|
|
|
savePage(data: Data, fileId = data.document.id, pageId = data.currentPageId) {
|
2021-06-16 12:09:45 +00:00
|
|
|
const page = data.document.pages[pageId]
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-06-11 22:06:09 +00:00
|
|
|
// Save page
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
localStorage.setItem(
|
|
|
|
storageId(fileId, 'page', pageId),
|
|
|
|
compress(JSON.stringify(page))
|
|
|
|
)
|
2021-06-11 10:11:42 +00:00
|
|
|
|
2021-06-11 22:06:09 +00:00
|
|
|
// Save page state
|
2021-06-21 21:35:28 +00:00
|
|
|
const currentPageState = data.pageStates[pageId]
|
2021-06-11 22:06:09 +00:00
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
storageId(fileId, 'pageState', pageId),
|
2021-06-12 19:40:26 +00:00
|
|
|
JSON.stringify({
|
|
|
|
...currentPageState,
|
2021-07-09 08:59:43 +00:00
|
|
|
selectedIds: [...currentPageState.selectedIds],
|
2021-06-12 19:40:26 +00:00
|
|
|
})
|
2021-06-11 10:31:31 +00:00
|
|
|
)
|
2021-06-11 10:11:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 10:42:16 +00:00
|
|
|
getPageFromLocalStorage(
|
|
|
|
data: Data,
|
|
|
|
fileId = data.document.id,
|
|
|
|
pageId = data.currentPageId
|
|
|
|
): Page {
|
|
|
|
if (typeof window === 'undefined') return
|
|
|
|
if (typeof localStorage === 'undefined') return
|
|
|
|
|
|
|
|
let page: Page
|
|
|
|
|
|
|
|
try {
|
|
|
|
const savedPage = localStorage.getItem(storageId(fileId, 'page', pageId))
|
|
|
|
if (savedPage === null) {
|
|
|
|
throw Error('That page is not in local storage.')
|
|
|
|
}
|
|
|
|
|
|
|
|
page = JSON.parse(decompress(savedPage))
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Could not load a page with the id', pageId)
|
|
|
|
}
|
|
|
|
|
|
|
|
return page
|
|
|
|
}
|
|
|
|
|
|
|
|
getPageStateFromLocalStorage(
|
|
|
|
data: Data,
|
|
|
|
fileId = data.document.id,
|
|
|
|
pageId = data.currentPageId
|
|
|
|
): PageState {
|
|
|
|
if (typeof window === 'undefined') return
|
|
|
|
if (typeof localStorage === 'undefined') return
|
|
|
|
|
|
|
|
let pageState: PageState
|
|
|
|
|
|
|
|
try {
|
|
|
|
const savedPageState = localStorage.getItem(
|
|
|
|
storageId(fileId, 'pageState', pageId)
|
|
|
|
)
|
|
|
|
if (savedPageState === null) {
|
|
|
|
throw Error('That page state is not in local storage.')
|
|
|
|
}
|
|
|
|
|
|
|
|
pageState = JSON.parse(decompress(savedPageState))
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Could not load a page state with the id', pageId)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pageState
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
loadPage(data: Data, fileId = data.document.id, pageId = data.currentPageId) {
|
2021-06-11 22:06:09 +00:00
|
|
|
if (typeof window === 'undefined') return
|
|
|
|
if (typeof localStorage === 'undefined') return
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
data.currentPageId = pageId
|
2021-06-11 22:06:09 +00:00
|
|
|
|
2021-07-07 12:46:46 +00:00
|
|
|
try {
|
|
|
|
// If we have a page in local storage, move it into state
|
|
|
|
const savedPage = localStorage.getItem(storageId(fileId, 'page', pageId))
|
2021-07-14 10:42:16 +00:00
|
|
|
|
|
|
|
if (savedPage === null) {
|
|
|
|
// Why would the page be null?
|
|
|
|
// TODO: Find out why the page would be null.
|
|
|
|
|
|
|
|
data.document.pages[pageId] = {
|
|
|
|
id: pageId,
|
|
|
|
type: 'page',
|
|
|
|
childIndex: Object.keys(data.document.pages).length,
|
|
|
|
name: 'New Page',
|
|
|
|
shapes: {},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data.document.pages[pageId] = JSON.parse(decompress(savedPage))
|
|
|
|
}
|
2021-07-07 12:46:46 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.warn('Could not load a page with the id', pageId)
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// If we don't have a page, create a new page
|
2021-06-11 22:06:09 +00:00
|
|
|
data.document.pages[pageId] = {
|
2021-06-11 10:11:42 +00:00
|
|
|
id: pageId,
|
|
|
|
type: 'page',
|
2021-06-16 12:09:45 +00:00
|
|
|
childIndex: Object.keys(data.document.pages).length,
|
|
|
|
name: 'New Page',
|
2021-06-11 10:11:42 +00:00
|
|
|
shapes: {},
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// Get saved page state from local storage
|
2021-06-11 10:11:42 +00:00
|
|
|
const savedPageState = localStorage.getItem(
|
|
|
|
storageId(fileId, 'pageState', pageId)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (savedPageState !== null) {
|
2021-06-16 12:09:45 +00:00
|
|
|
// If we have a page, move it into state
|
2021-06-11 22:06:09 +00:00
|
|
|
const restored: PageState = JSON.parse(savedPageState)
|
|
|
|
data.pageStates[pageId] = restored
|
2021-06-11 10:11:42 +00:00
|
|
|
} else {
|
2021-06-11 22:06:09 +00:00
|
|
|
data.pageStates[pageId] = {
|
2021-06-16 12:09:45 +00:00
|
|
|
id: pageId,
|
2021-06-11 10:11:42 +00:00
|
|
|
camera: {
|
|
|
|
point: [0, 0],
|
|
|
|
zoom: 1,
|
|
|
|
},
|
2021-07-09 08:59:43 +00:00
|
|
|
selectedIds: [],
|
2021-06-11 10:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// Save the last page state
|
|
|
|
localStorage.setItem(
|
|
|
|
storageId(fileId, 'lastPageState'),
|
|
|
|
JSON.stringify(data.pageStates[pageId])
|
|
|
|
)
|
2021-06-11 22:06:09 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
// Now clear out the other pages from state.
|
|
|
|
Object.values(data.document.pages).forEach((page) => {
|
|
|
|
if (page.id !== data.currentPageId) {
|
|
|
|
page.shapes = {}
|
|
|
|
}
|
|
|
|
})
|
2021-06-13 13:24:03 +00:00
|
|
|
|
2021-06-11 22:06:09 +00:00
|
|
|
// Update camera for the new page state
|
|
|
|
document.documentElement.style.setProperty(
|
|
|
|
'--camera-zoom',
|
|
|
|
data.pageStates[data.currentPageId].camera.zoom.toString()
|
|
|
|
)
|
2021-06-11 10:11:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 22:06:09 +00:00
|
|
|
/* ------------------- File System ------------------ */
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-07-07 12:46:46 +00:00
|
|
|
reset = () => {
|
|
|
|
this.previousSaveHandle = undefined
|
|
|
|
}
|
|
|
|
|
2021-06-11 22:06:09 +00:00
|
|
|
saveToFileSystem = (data: Data) => {
|
2021-07-07 12:46:46 +00:00
|
|
|
this.saveDataToFileSystem(data, false)
|
2021-06-11 22:06:09 +00:00
|
|
|
}
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-11 22:06:09 +00:00
|
|
|
saveAsToFileSystem = (data: Data) => {
|
2021-07-07 12:46:46 +00:00
|
|
|
this.saveDataToFileSystem(data, true)
|
2021-06-11 22:06:09 +00:00
|
|
|
}
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-07-07 12:46:46 +00:00
|
|
|
saveDataToFileSystem = async (data: Data, saveAs: boolean) => {
|
|
|
|
const isSavingAs = saveAs || !this.previousSaveHandle
|
2021-06-16 12:09:45 +00:00
|
|
|
const document = this.getCompleteDocument(data)
|
|
|
|
|
|
|
|
// Then save to file system
|
|
|
|
const blob = new Blob(
|
|
|
|
[
|
|
|
|
compress(
|
|
|
|
JSON.stringify({
|
|
|
|
document,
|
|
|
|
pageState: data.pageStates[data.currentPageId],
|
|
|
|
})
|
|
|
|
),
|
|
|
|
],
|
|
|
|
{
|
|
|
|
type: 'application/vnd.tldraw+json',
|
|
|
|
}
|
|
|
|
)
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
const documentName = data.document.name
|
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
const fa = await import('browser-fs-access')
|
|
|
|
|
2021-06-11 22:06:09 +00:00
|
|
|
fa.fileSave(
|
|
|
|
blob,
|
|
|
|
{
|
|
|
|
fileName: `${
|
2021-07-07 12:46:46 +00:00
|
|
|
isSavingAs
|
|
|
|
? documentName
|
|
|
|
: this.previousSaveHandle?.name || 'My Document'
|
2021-06-11 22:06:09 +00:00
|
|
|
}.tldr`,
|
|
|
|
description: 'tldraw file',
|
|
|
|
extensions: ['.tldr'],
|
|
|
|
},
|
2021-07-07 12:46:46 +00:00
|
|
|
isSavingAs ? undefined : this.previousSaveHandle,
|
2021-06-11 22:06:09 +00:00
|
|
|
true
|
2021-06-07 11:18:50 +00:00
|
|
|
)
|
2021-06-11 22:06:09 +00:00
|
|
|
.then((handle) => {
|
|
|
|
this.previousSaveHandle = handle
|
|
|
|
state.send('SAVED_FILE_TO_FILE_SYSTEM')
|
2021-06-12 19:40:26 +00:00
|
|
|
idb.set('previous_handle', handle)
|
2021-06-11 22:06:09 +00:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
state.send('CANCELLED_SAVE', { reason: e.message })
|
|
|
|
})
|
2021-06-07 11:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
async loadDocumentFromFilesystem() {
|
|
|
|
const fa = await import('browser-fs-access')
|
|
|
|
|
2021-06-11 22:06:09 +00:00
|
|
|
fa.fileOpen({
|
|
|
|
description: 'tldraw files',
|
|
|
|
})
|
|
|
|
.then((blob) =>
|
2021-06-16 12:09:45 +00:00
|
|
|
getTextFromBlob(blob).then((json) => {
|
2021-06-11 22:06:09 +00:00
|
|
|
// Save blob for future saves
|
|
|
|
this.previousSaveHandle = blob.handle
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
state.send('LOADED_FROM_FILE', { json: decompress(json) })
|
2021-06-11 22:06:09 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.catch((e) => {
|
|
|
|
state.send('CANCELLED_SAVE', { reason: e.message })
|
|
|
|
})
|
2021-06-07 11:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const storage = new Storage()
|
|
|
|
|
|
|
|
export default storage
|
2021-06-11 10:11:42 +00:00
|
|
|
|
|
|
|
async function getTextFromBlob(blob: Blob): Promise<string> {
|
|
|
|
// Return blob as text if a text file.
|
|
|
|
if ('text' in Blob) return blob.text()
|
|
|
|
|
|
|
|
// Return blob as text if a text file.
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const reader = new FileReader()
|
|
|
|
|
|
|
|
reader.onloadend = () => {
|
|
|
|
if (reader.readyState === FileReader.DONE) {
|
|
|
|
resolve(reader.result as string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.readAsText(blob, 'utf8')
|
|
|
|
})
|
|
|
|
}
|