Skip to content

Commit 0af7a7d

Browse files
committed
fix: add error notice if invalid docx is opened, fallback to blank
1 parent ce3a737 commit 0af7a7d

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

packages/super-editor/src/components/SuperEditor.vue

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup>
22
import 'tippy.js/dist/tippy.css';
3-
import { NSkeleton } from 'naive-ui';
3+
import { NSkeleton, useMessage } from 'naive-ui';
44
import { ref, onMounted, onBeforeUnmount, shallowRef, reactive, markRaw } from 'vue';
55
import { Editor } from '@/index.js';
66
import { getStarterExtensions } from '@extensions/index.js';
@@ -43,6 +43,7 @@ const props = defineProps({
4343
4444
const editorReady = ref(false);
4545
const editor = shallowRef(null);
46+
const message = useMessage();
4647
4748
const editorWrapper = ref(null);
4849
const editorElem = ref(null);
@@ -99,10 +100,16 @@ const pollForMetaMapData = (ydoc, retries = 10, interval = 500) => {
99100
checkData();
100101
};
101102
103+
const setDefaultBlankFile = async () => {
104+
fileSource.value = await getFileObject(BlankDOCX, 'blank.docx', DOCX);
105+
};
106+
102107
const loadNewFileData = async () => {
103-
fileSource.value = props.fileSource;
108+
if (!fileSource.value) {
109+
fileSource.value = props.fileSource;
110+
}
104111
if (!fileSource.value || fileSource.value.type !== DOCX) {
105-
fileSource.value = await getFileObject(BlankDOCX, 'blank.docx', DOCX);
112+
await setDefaultBlankFile();
106113
}
107114
108115
try {
@@ -116,7 +123,12 @@ const loadNewFileData = async () => {
116123
const initializeData = async () => {
117124
// If we have the file, initialize immediately from file
118125
if (props.fileSource) {
119-
const fileData = await loadNewFileData();
126+
let fileData = await loadNewFileData();
127+
if (!fileData) {
128+
message.error('Unable to load the file. Please verify the .docx is valid and not password protected.');
129+
await setDefaultBlankFile();
130+
fileData = await loadNewFileData();
131+
}
120132
return initEditor(fileData);
121133
}
122134

packages/superdoc/src/SuperDoc.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
reactive,
1515
watch,
1616
} from 'vue';
17+
import { NMessageProvider } from 'naive-ui';
1718
import { storeToRefs } from 'pinia';
1819
1920
import PdfViewer from './components/PdfViewer/PdfViewer.vue';
@@ -627,14 +628,16 @@ watch(getFloatingComments, () => {
627628
@bypass-selection="handlePdfClick"
628629
/>
629630
630-
<SuperEditor
631-
v-if="doc.type === DOCX"
632-
:file-source="doc.data"
633-
:state="doc.state"
634-
:document-id="doc.id"
635-
:options="editorOptions(doc)"
636-
@pageMarginsChange="handleSuperEditorPageMarginsChange(doc, $event)"
637-
/>
631+
<n-message-provider>
632+
<SuperEditor
633+
v-if="doc.type === DOCX"
634+
:file-source="doc.data"
635+
:state="doc.state"
636+
:document-id="doc.id"
637+
:options="editorOptions(doc)"
638+
@pageMarginsChange="handleSuperEditorPageMarginsChange(doc, $event)"
639+
/>
640+
</n-message-provider>
638641
639642
<!-- omitting field props -->
640643
<HtmlViewer

0 commit comments

Comments
 (0)