@@ -27,7 +27,7 @@ export default class IndexRoute extends Route {
2727 console . log ( 'editing form' , form ) ;
2828 this . configureStorageLocations ( form ) ;
2929
30- await this . fetchGraphs ( ) ;
30+ await this . fetchGraphs ( form !== null ) ;
3131
3232 if ( form ) {
3333 this . loadForm ( ) ;
@@ -64,7 +64,13 @@ export default class IndexRoute extends Route {
6464 storageLocation ;
6565 }
6666
67- async fetchGraphs ( ) {
67+ async fetchGraphs ( removeN3Rules = false ) {
68+ // Remove all N3 rules from the resource.
69+ let matches ;
70+ if ( removeN3Rules ) {
71+ matches = await this . removeN3RulesFromResource ( ) ;
72+ }
73+
6874 await this . store . fetchGraphForType ( 'hydra-class' , true ) ;
6975 await this . store . fetchGraphForType ( 'rdf-form' , true ) ;
7076 await this . store . fetchGraphForType ( 'rdf-form-field' , true ) ;
@@ -76,6 +82,11 @@ export default class IndexRoute extends Route {
7682 await this . store . fetchGraphForType ( 'shacl-form' , true ) ;
7783 await this . store . fetchGraphForType ( 'shacl-form-field' , true ) ;
7884 await this . store . fetchGraphForType ( 'shacl-form-option' , true ) ;
85+
86+ // Add N3 rules back to the resource.
87+ if ( removeN3Rules ) {
88+ await this . addN3RulesToResource ( matches ) ;
89+ }
7990 }
8091
8192 initiateNewRdfForm ( ) {
@@ -116,4 +127,85 @@ export default class IndexRoute extends Route {
116127 console . log ( 'loaded form' , this . form ) ;
117128 console . log ( 'loaded supportedClass' , this . supportedClass ) ;
118129 }
130+
131+ async removeN3RulesFromResource ( ) {
132+ const fetch = this . solidAuth . session . fetch ;
133+
134+ const response = await fetch ( this . loadedFormUri , {
135+ method : 'GET' ,
136+ } ) ;
137+
138+ // Get content-type.
139+ const contentType = response . headers . get ( 'content-type' ) ;
140+
141+ // Get content.
142+ let text = await response . text ( ) ;
143+ console . log ( text ) ;
144+
145+ // Match prefixes.
146+ const prefixRegex = / ( @ p r e f i x | P R E F I X ) \s + [ ^ : ] * : \s * < [ ^ > ] * > \s * \. ? \n / g;
147+ const prefixes = text . match ( prefixRegex ) ;
148+
149+ // Match N3 rules.
150+ const rulesRegex =
151+ / \{ [ ^ { } ] * } \s * ( = > | [ ^ \s { } ] * i m p l i e s [ ^ \s { } ] * ) \s * { [ ^ { } ] * } \s * \. / g;
152+ const rules = text . match ( rulesRegex ) ;
153+ console . log ( rules ) ;
154+
155+ // Remove N3 rules.
156+ rules ?. forEach ( ( match ) => {
157+ text = text . replace ( match , '' ) ;
158+ } ) ;
159+
160+ // Save resource without N3 rules.
161+ await fetch ( this . loadedFormUri , {
162+ method : 'PUT' ,
163+ headers : {
164+ 'Content-Type' : contentType ,
165+ } ,
166+ body : text ,
167+ } ) ;
168+
169+ return { rules, prefixes } ;
170+ }
171+
172+ async addN3RulesToResource ( matches ) {
173+ const { rules, prefixes } = matches ;
174+ const fetch = this . solidAuth . session . fetch ;
175+
176+ const response = await fetch ( this . loadedFormUri , {
177+ method : 'GET' ,
178+ } ) ;
179+
180+ // Get content-type.
181+ const contentType = response . headers . get ( 'content-type' ) ;
182+
183+ // Get content.
184+ let text = await response . text ( ) ;
185+
186+ // Match prefixes.
187+ const prefixRegex = / ( @ p r e f i x | P R E F I X ) \s + [ ^ : ] * : \s * < [ ^ > ] * > \s * \. ? \n / g;
188+ const matchedPrefixes = text . match ( prefixRegex ) ;
189+
190+ // Add prefixes in front if not already present.
191+ prefixes ?. forEach ( ( prefix ) => {
192+ if ( ! matchedPrefixes ?. includes ( prefix ) ) {
193+ text = prefix + text ;
194+ }
195+ } ) ;
196+
197+ // Add N3 rules.
198+ rules . forEach ( ( match ) => {
199+ text += match ;
200+ } ) ;
201+
202+ // Save resource with N3 rules.
203+ await fetch ( this . loadedFormUri , {
204+ method : 'PUT' ,
205+ headers : {
206+ 'Content-Type' : contentType ,
207+ } ,
208+ body : text ,
209+ } ) ;
210+ }
119211}
0 commit comments