Added custom endnote back to standard conversion

This commit is contained in:
Georgy Litvinov 2021-06-08 14:52:46 +02:00
parent f889f0edb7
commit 1ee8b5f951
4 changed files with 344 additions and 37 deletions

View file

@ -45,15 +45,174 @@ Sub convertEndnotesExecution
EndIf
If (nativeEndnotesCounter = -1 ) Then
'only custom endnotes found. Window convert to native?
runEndnotesCustomToNativeDialog(foundEndNotes(1))
Exit Sub
EndIf
'both custom and native endnotes found
'convert to custom all or
'convert to native
runDualEndnotesDialog(foundEndNotes(0),foundEndNotes(1))
End Sub
Sub runDualEndnotesDialog(nativeEndNotes() As Object,customEndnotes() As Object)
Dim dialog As Object
waitingForDialog = true
dialog = notModalDialog("EndnotesConversion")
dialog.getControl("found").SetText(getTranslation("EndnotesNativeDialogFound") & CStr(UBound(nativeEndNotes)+1))
dialog.getControl("found2").SetText(getTranslation("EndnotesCustomDialogFound") & CStr(UBound(customEndnotes)+1))
dialog.getControl("found2").Model.EnableVisible = TRUE
dialog.getControl("description").SetText(getTranslation("EndnotesConversionTwoOptions"))
dialog.getControl("cancel").Label = getTranslation("buttonCancel")
dialog.getControl("start").Label = getTranslation("toNativeEndnotes")
dialog.getControl("option2").Label = getTranslation("toCustomEndnotes")
dialog.getControl("option2").Model.EnableVisible = TRUE
dialog.setvisible(true)
Do While waitingForDialog
If dialog.getControl("cancel").model.state = 1 then
exit Do
EndIf
If dialog.getControl("start").model.state = 1 then
convertCustomEndnotesToNative(customEndnotes)
exit Do
EndIf
If dialog.getControl("option2").model.state = 1 then
convertEndnotesToCustom(nativeEndNotes)
exit Do
EndIf
wait (100)
Loop
dialog.dispose
End Sub
Sub runEndnotesCustomToNativeDialog(customEndnotes() As Object)
Dim dialog As Object
waitingForDialog = true
dialog = notModalDialog("EndnotesConversion")
dialog.getControl("found").SetText(getTranslation("EndnotesCustomDialogFound") & CStr(UBound(customEndnotes)+1))
dialog.getControl("description").SetText(getTranslation("EndnotesCustomConversionOption"))
dialog.getControl("cancel").Label = getTranslation("buttonCancel")
dialog.getControl("start").Label = getTranslation("buttonOk")
dialog.setvisible(true)
Do While waitingForDialog
If dialog.getControl("cancel").model.state = 1 then
exit Do
EndIf
If dialog.getControl("start").model.state = 1 then
convertCustomEndnotesToNative(customEndnotes)
exit Do
EndIf
wait (100)
Loop
dialog.dispose
End Sub
Sub convertCustomEndnotesToNative(foundEndnotes() As Object)
Dim i As Long
Dim bodyAnchorName As String
Dim textAnchorName As String
Dim bookmarksName As String
Dim bookmarks As Object
bookmarksName = ThisComponent.Links.ElementNames(6)
bookmarks = ThisComponent.Links.getByName(bookmarksName)
For i = LBound(foundEndnotes) To UBound(foundEndnotes)
bodyAnchorName = getBodyAnchorName(foundEndnotes(i))
textAnchorName = getTextAnchorName(bodyAnchorName)
If bookmarks.hasByName(bodyAnchorName) And bookmarks.hasByName(textAnchorName) Then
convertLinksToEndnote(bodyAnchorName,textAnchorName)
EndIf
Next i
End Sub
Function getBodyAnchorName(textRange As Object) As String
getBodyAnchorName = ""
If (Len(textRange.HyperLinkURL) > 0) Then
getBodyAnchorName = Right(textRange.HyperLinkURL,Len(textRange.HyperLinkURL)-1)
EndIf
End Function
Function getTextAnchorName(bodyAnchorName As String) As String
getTextAnchorName = ""
If Len(bodyAnchorName) > Len(endnoteInBodyAnchorSuffix) Then
getTextAnchorName = Left(bodyAnchorName,Len(bodyAnchorName)-Len(endnoteInBodyAnchorSuffix)) & endnoteInTextAnchorSuffix
EndIf
End Function
Sub convertLinksToEndnote(forwardLink As String,backwardLink As String)
Dim bookMarkName As String
bookmarkName = ThisComponent.Links.ElementNames(6)
Dim bookmarks As Object
bookmarks = ThisComponent.Links.getByName(bookmarkName)
Dim forward As Object
Dim backward As Object
Dim oViewCursor As Object
Dim footNoteSign As String
oViewCursor = ThisComponent.CurrentController.getViewCursor()
Dim oTextCursor As Object
If NOT bookmarks.hasByName(forwardLink) OR NOT bookmarks.hasByName(backwardLink) Then
exit sub
EndIf
forward = bookmarks.getByName(forwardLink)
backward = bookmarks.getByName(backwardLink)
footNoteSign = forward.Anchor.String
oViewCursor.goToRange(forward.Anchor,false)
SendRM
SendRM
oTextCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor)
goToEndOfEndnoteBody(oTextCursor)
oTextCursor.goToRange(oViewCursor,true)
oViewCursor.goToRange(oTextCursor,true)
unoCut()
SendRM
oViewCursor.goToRange(backward.Anchor,false)
removeEndnoteSignInText()
createEndnote
unoPaste()
oViewCursor.getText.setLabel(footNoteSign)
forward.dispose()
backward.dispose()
End Sub
Sub goToEndOfEndnoteBody(oTextCursor As Object)
Dim position As Object
position = oTextCursor.End
oTextCursor.goToEndOfParagraph(false)
oTextCursor.goRight(1,false)
If oTextCursor.ParaStyleName <> "Endnote" OR oTextCursor.CharStyleName = "Endnote Symbol" Then
oTextCursor.goLeft(1,false)
Exit Sub
EndIf
'Got to the end of the document
If oTextCursor.Text.compareRegionEnds(position,oTextCursor.End) = 0 Then
Exit Sub
EndIf
goToEndOfEndnoteBody(oTextCursor)
End Sub
Sub removeEndnoteSignInText()
Dim oViewCursor As Object
Dim character As String
oViewCursor = ThisComponent.CurrentController.getViewCursor()
oViewCursor.String = ""
oViewCursor.HyperLinkURL=""
End Sub
sub createEndnote
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:InsertEndnote", "", 0, Array())
end Sub
Sub runEndnotesConversionDialog(foundEndNotes As Variant)
Dim dialog As Object
waitingForDialog = true
@ -62,6 +221,7 @@ Sub runEndnotesConversionDialog(foundEndNotes As Variant)
dialog.getControl("description").SetText(getTranslation("EndnotesNativeDialogDescriptionSelect"))
dialog.getControl("cancel").Label = getTranslation("buttonCancel")
dialog.getControl("start").Label = getTranslation("buttonOk")
Thiscomponent.CurrentController.select(ThisComponent.Text)
dialog.setvisible(true)
Do While waitingForDialog
If dialog.getControl("cancel").model.state = 1 then
@ -95,16 +255,14 @@ End Sub
Function createEndnoteFieldPrefix As String
Dim names() As String
Dim i As Integer
Dim prefix As String
names = ThisComponent.getTextFieldMasters().getElementNames()
For i = 0 To 10000
prefix = endnoteFieldPrefix & i & "_"
If NOT startsWithInArray(prefix, names) Then
createEndnoteFieldPrefix = prefix
Exit Function
EndIf
Next i
prefix = endnoteFieldPrefix & RND_String() & "_"
If NOT startsWithInArray(prefix, names) Then
createEndnoteFieldPrefix = prefix
Exit Function
EndIf
createEndnoteFieldPrefix = prefix
End Function
@ -278,6 +436,7 @@ Sub findEndNotesInParagraph(enum1Element As Object,nativeEndNotes As Variant, cu
Dim footnoteText As Object
Dim label As String
Dim labelNum As Long
Dim bookmarkName As String
If enum1Element.OutlineLevel = level Then
curNum = 1
EndIf
@ -286,9 +445,20 @@ Sub findEndNotesInParagraph(enum1Element As Object,nativeEndNotes As Variant, cu
thisPortion = textPortions.nextElement
If isTargetNote(thisPortion,1) Then
AddToArray(nativeEndNotes, thisPortion)
' ElseIf isCustomEndnote(thisPortion)
' AddToArray(customEndNotesd, thisPortion)
ElseIf isCustomEndnote(thisPortion) Then
AddToArray(customEndNotes, thisPortion)
EndIf
Wend
End Sub
Function isCustomEndnote(portion As Object) As Boolean
If InStr(portion.HyperLinkURL, "#" & endnoteFieldPrefix) = 1 And portion.String <> "" Then
If InStr(portion.HyperLinkURL, endnoteInBodyAnchorSuffix) > 0 Then
isCustomEndnote = true
Exit Function
EndIf
EndIf
isCustomEndnote = false
End Function
</script:module>