Skip to content

Commit 0eeed98

Browse files
committed
Prevent undo from erasing docs after page load
1 parent 5004b9e commit 0eeed98

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

components/DocEditor.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export default defineComponent({
170170
<CoreEditor
171171
v-if="isMounted"
172172
ref="coreEditor"
173+
:key="doc?.id"
173174
v-model="text"
174175
:max-width-in-chars="maxWidthInChars"
175176
:options="options"

pages/docs/[docId]/index.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,21 @@ export default defineComponent({
3131
const header = computed(() => doc.value.headers[0])
3232
const { public: { appTitle } } = useConfig()
3333
34+
watch(docId, () => {
35+
// If we don't create a new placeholder, then the same placeholder id
36+
// will be used when creating multiple docs back-to-back. This only
37+
// happens when the editor is not unmounted between new doc creations.
38+
placeholder.value = new Doc({ text: formatTags(store.state.context.tags, ' ') })
39+
})
40+
3441
const onInput = async (text: string) => {
3542
// Todo: Load a new doc before leaving the route so that Ink does not
3643
// send a new input event (or figure out how to prevent it from sending
3744
// an input event when data is pushed to the editor from the store).
3845
if (text && (!docId.value || docId.value === 'new')) {
3946
// When we receive input without an active doc, we should always create
4047
// a new doc with a unique id.
41-
const newDoc = new Doc({ text })
48+
const newDoc = new Doc({ id: placeholder.value.id, text })
4249
4350
store.commit(EDIT_DOCUMENT, newDoc)
4451

test/e2e/editor.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,25 @@ test.describe('editor', () => {
160160

161161
await expect(page.getByTestId('doc-backlinks').getByRole('link')).toHaveText('Test 2')
162162
})
163+
164+
test('does not undo initial doc load', async ({ page }) => {
165+
await page.goto('/docs/new')
166+
await page.waitForSelector('[data-is-mounted="true"]')
167+
168+
await page.keyboard.type('# My Test Doc')
169+
170+
// Wait for the data to be persisted.
171+
await page.waitForTimeout(200)
172+
173+
await page.reload()
174+
await page.waitForSelector('[data-is-mounted="true"]')
175+
176+
await expect(page.locator('.ink-mde-editor-content')).toHaveText('# My Test Doc')
177+
178+
await page.keyboard.down('Meta')
179+
await page.keyboard.press('z')
180+
await page.keyboard.up('Meta')
181+
182+
await expect(page.locator('.ink-mde-editor-content')).toHaveText('# My Test Doc')
183+
})
163184
})

0 commit comments

Comments
 (0)